1、所需jar包
shiro-all-1.4.1.jar、log4j-1.2.17.jar、slf4j-api-1.7.28.jar、slf4j-log4j12-1.7.28.jar
2、导入以下两个文件(文件位于shiro-root-source-release\shiro-root\samples\quickstart\src\main\resources文件夹下)
3、Quickstart.java代码
package com.hern.shiro;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Simple Quickstart application showing how to use Shiro's API.
*
* @since 0.9 RC2
*/
public class Quickstart {
private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);
public static void main(String[] args) {
//创建Shiro SecurityManager的最简单方法
//领域、用户、角色和权限是使用简单的ini配置。
//我们将使用一个可以接收.ini文件
//返回SecurityManager实例:
//使用类路径根目录下的shiro.ini文件
//(file:和url:分别从文件和url加载前缀):
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
// for this simple example quickstart, make the SecurityManager
// accessible as a JVM singleton. Most applications wouldn't do this
// and instead rely on their container configuration or web.xml for
// webapps. That is outside the scope of this simple quickstart, so
// we'll just do the bare minimum so you can continue to get a feel
// for things.
SecurityUtils.setSecurityManager(securityManager);
//获取当前正在执行的用户:
//获取当前的Subject,调用SecurityUtils.getSubject();
Subject currentUser = SecurityUtils.getSubject();
//在会话中做一些事情(不需要Web或EJB容器!!!!)
//测试使用Session。首先获取Session,通过Subject的getSession()
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
log.info("检索到正确的值! [" + value + "]");
}
//让我们登录当前用户,以便检查角色和权限:
//测试当前的用户是否已经被认证,即是否已经登录
//调用Subject的isAuthenticated()
if (!currentUser.isAuthenticated()) {
//把用户名和密码封装为UsernamePasswordToken对象
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
//RememberME
token.setRememberMe(true);
try {
//执行登录
currentUser.login(token);
} catch (UnknownAccountException uae) {//若没有指定的账户,则Shiro将会抛出UnknownAccountException异常
log.info("没有用户名为 " + token.getPrincipal() + " 的用户");
return;
} catch (IncorrectCredentialsException ice) {//若账户存在,但密码不匹配,则Shiro将会抛出IncorrectCredentialsException异常
log.info("账户的密码 " + token.getPrincipal() + " 是不正确的!");
return;
} catch (LockedAccountException lae) {//用户锁定的异常
log.info("账户的用户名" + token.getPrincipal() + " 已锁定。 " +
"请与管理员联系以解锁!");
}
// ... catch more exceptions here (maybe custom ones specific to your application?
catch (AuthenticationException ae) {//所有认证时异常的父类
//unexpected condition? error?
}
}
//say who they are:
//print their identifying principal (in this case, a username):
log.info("用户 [" + currentUser.getPrincipal() + "] 登录成功!");
//测试一个角色,调用Subject的hasRole()
if (currentUser.hasRole("schwartz")) {
log.info("May the Schwartz be with you!");
} else {
log.info("你好,你仅仅是普通用户角色");
}
//测试是否具有权限(不是实例级别),调用Subject的isPermitted方法
if (currentUser.isPermitted("lightsaber:wield")) {
log.info("You may use a lightsaber ring. Use it wisely.");
} else {
log.info("Sorry, lightsaber rings are for schwartz masters only.");
}
//测试是否具有权限,更加具体,调用Subject的isPermitted方法
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
"Here are the keys - have fun!");
} else {
log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
//all done - log out!
//执行登出
currentUser.logout();
System.exit(0);
}
}