Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。
本文介绍了springboot整合shiro实现简单的权限案例。
功能
shiro流程图
三大对象
Subject:当前用户的对象
SecurityManager:管理subject
Realm:操作数据
代码演示
配置application.yml,启动test类测试
导入shiro.jar包
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.1</version>
</dependency>
创建一个UserRealm类继承 AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行了==>授权");
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("执行了==>认证");
return null;
}
}
创建一个ShiroConfig添加注解@Configuration,然后生成三个类的方法交给spring托管
@Configuration
public class ShiroConfig {
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager")DefaultWebSecurityManager securityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//设置安全管理器
bean.setSecurityManager(securityManager);
return bean;
}
@Bean(name="securityManager")
public DefaultWebSecurityManager getSecurityManager(@Qualifier("userRealm")UserRealm userRealm){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(userRealm);
return securityManager;
}
@Bean
public UserRealm userRealm(){
return new UserRealm();
}
}
写一个登录流程
在这里就不展示HTML以及service和mapper
Controller中调service的方法去掉
@RequestMapping("/login")
public String login(String username, String password,Model model){
//获取当前的用户
Subject subject = SecurityUtils.getSubject();
//封装用户的数据,生成token令牌
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
try {
subject.login(token);//执行登录流程
return "index";
} catch (UnknownAccountException e) {
model.addAttribute("msg","用户名错误");
return "login";
}catch (IncorrectCredentialsException e) {
model.addAttribute("msg","密码错误");
return "login";
}
}
当执行subject.login(token)后会去认证身份,调用 UserRealm里面的认证方法
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("执行了==>认证");
/*String username = "root";
String password = "123456";*/
UsernamePasswordToken userToken = (UsernamePasswordToken)token;
//查询数据库,返回user对象
User user = userService.queryByName(userToken.getUsername());
if(user == null){ //true,则返回UnknownAccountException异常
return null;
}
//检验密码是否正确
return new SimpleAccount(user,user.getPassword(),"");
}
当然shiro里面也会有过滤器,在ShiroConfig中 getShiroFilterFactoryBean的方法中
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(
@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean =new ShiroFilterFactoryBean();
//设置安全管理器
bean.setSecurityManager(defaultWebSecurityManager);
/**
* 添加shiro的内置过滤器
* anon: 无需认证就可以访问
* authc: 需要认证才可以访问
* user: 必须拥有记住我才能用
* perms: 必须拥有对某个资源的权限才可以访问
* role: 拥有某个角色才可以访问
*/
Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/user/add","perms[add]");
filterMap.put("/user/update","perms[update]");
filterMap.put("/user/*","authc");
bean.setFilterChainDefinitionMap(filterMap);
//设置登录的请求
bean.setLoginUrl("/toLogin");
return bean;
}
设置后,用户有权限才能访问,那怎么才能得到用户的权限呢,我们需要在 UserRealm的授权方法中授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行了==>授权");
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//得到当前的用户
Subject subject = SecurityUtils.getSubject();
User user = (User) subject.getPrincipal();
//授权
info.addStringPermission(user.getPerms());
return info;
}
```最后的返回值原本是null,别忘记改成info
数据库表,当前admin只能访问add页面,root用户只能访问update页面,模板引擎用的thymeleaf,shiro判断当前是否有权限jar包
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
以及在ShiroConfig中需添加
@Bean
public ShiroDialect getShiroDialect(){
return new ShiroDialect();
}
HTML的命名空间
<html lang="en" xmlns:th="http://www.themleaf.org"
xmlns:shiro="http://www.themleaf.org/thymeleaf-extras-shiro">
于是springboot整合shiro的认证,授权案例得以实现