简单用过 SpringSecurity 安全框架后,再试试另一个安全框架——Shiro。
Apache Shiro 是一个强大且易用的 Java 安全框架:
Shiro 源码:https://github.com/apache/shiro

Shiro 的主要功能包括:
除此之外还有额外的功能,此处暂且略过。
在最高的概念层次上,Shiro 的架构有三个主要的概念:Subject,,SecurityManager 和 Realm。

Shiro 还有复杂的内部架构:

这里就先不管了,还没研究到这么深的程度。
通过 Shiro 提供的 Sample 中的 Quickstart 简单了解一下 Shiro 的 API。
public class Quickstart {
private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);
public static void main(String[] args) {
// 从工厂方法中获取 SecurityManager 实例! 此处 .ini 文件中保存了用户名、密码和权限的数据。
Factory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
// 将 SecurityManager 作为 JVM 单例使用
// 一般应用不会这样做,而是依赖于它们的容器配置,此处仅是为了快速开始
SecurityUtils.setSecurityManager(securityManager);
// 简单的 Shiro 环境就搭建好了,接下来可以:
// 获取当前的执行对象 Subject
Subject currentUser = SecurityUtils.getSubject();
// 使用会话 Session 做些事情!(不需要 web 或者 EJB 容器!)
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
log.info("Retrieved the correct value! [" + value + "]");
}
// 登录当前用户,以查看角色和权限
// currentUser.isAuthenticated() 检查是否认证(登录)
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
log.info("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
log.info("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
log.info("The account for username " + token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
// 通过不同的认证异常进行不同的提示
catch (AuthenticationException ae) {
// 抛出了最高的认证异常,是什么情况?错误?
}
}
// 登录成功,表明身份
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
// 查看是否具有权限 currentUser.hasRole()
if (currentUser.hasRole("schwartz")) {
log.info("May the Schwartz be with you!");
} else {
log.info("Hello, mere mortal.");
}
// 查看类型化权限(非实例级别) currentUser.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.");
}
// (强大的)实例级别权限 (有什么区别?)
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!");
}
// 完成,注销!
currentUser.logout();
System.exit(0);
}
}
通过注释说明了每句代码的作用(也是从原英文注释翻译过来的 = = ),看看其中 API 的使用即可,马上在 SpringBoot 中使用试试!