一、環境搭建
- 創建java project項目。
- 導入Quickstart必須的Jar包。
shiro-all-1.3.2.jar
log4j-1.2.15.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
- 加入log配置文件,以及用於shiro測試的配置文件。
- 新建包,把Quickstart.java文件放入包。
- 整個java環境目錄。
二、Quickstart類講解
public class Quickstart {
public static void main(String[] args) {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
System.out.println("==================>>>>>>>>>>>Retrieved the correct value! [" + value + "]");
}
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
System.out.println("==================>>>>>>>>>>>There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
System.out.println("==================>>>>>>>>>>>Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
System.out.println("==================>>>>>>>>>>>The account for username " + token.getPrincipal() + " is locked.Please contact your administrator to unlock it.");
} catch (AuthenticationException ae) {
}
}
System.out.println("==================>>>>>>>>>>>User [" + currentUser.getPrincipal() + "] logged in successfully.");
if (currentUser.hasRole("schwartz")) {
System.out.println("==================>>>>>>>>>>>May the Schwartz be with you!");
} else {
System.out.println("==================>>>>>>>>>>>Hello, mere mortal.");
}
if (currentUser.isPermitted("lightsaber:weild")) {
System.out.println("==================>>>>>>>>>>>You may use a lightsaber ring. Use it wisely.");
} else {
System.out.println("==================>>>>>>>>>>>Sorry, lightsaber rings are for schwartz masters only.");
}
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
System.out.println("==================>>>>>>>>>>>You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.Here are the keys - have fun!");
} else {
System.out.println("==================>>>>>>>>>>>Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
currentUser.logout();
System.exit(0);
}
}