天天看点

springboot整合 shiro1. 添加整合依赖2. 创建自定义Realm对象3. 编写配置类4. 实现登录拦截 – 添加shiro内置过滤器5. 实现用户验证6. 请求授权实现7. 前端控制 – 根据权限显示功能

文章目录

  • 1. 添加整合依赖
  • 2. 创建自定义Realm对象
  • 3. 编写配置类
  • 4. 实现登录拦截 -- 添加shiro内置过滤器
  • 5. 实现用户验证
  • 6. 请求授权实现
  • 7. 前端控制 -- 根据权限显示功能
    • 添加依赖
    • 在配置类ShiroConfig中添加ShiroDialect -- 整合Shiro thymeleof
    • 前端导入命名空间 xmlns:sec="http://www.thymeleaforg/thymeleaf-extras-shiro"
    • 前端根据权限显示功能

1. 添加整合依赖

<dependency>
  <groupId>org.apache.shiro</groupId> 
  <artifactId>shiro-spring</artifactId> 
  <version>1.4.1</version> 
</dependency>
           

2. 创建自定义Realm对象

自定义的UserRealm extends AuthorizingRealm

public class UserRealm extends AuthorizingRealm { 
//授权 
@Override 
protected AuthorizationInfo doGetAuthorization Info(PrincipalCollection principa){
System.out.println("执行了=>授权doGetAuthorizationInfo"); 
return null; 
} 
//认证
@Override 
protected AuthenticationInfo doGetAuthentication Info(AuthenticationToken token) throws Authentication{
System.out.println("执行了=>认证doGetAuthorizationInfo"); 
return null; 
} 
}
           

3. 编写配置类

ShiroConfig

@Configuration
public class ShiroConfig {
//shiroFilterFactoryBean:3 
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean (@Qualifier("securityManager") DefaultWebWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); 
//设置安全管理器
bean.setSecurityManager(defaultWebSecurityManager); 
return bean;
}

//DafaultWebSecurityManager:2 
@Bean(name="securityManager") 
public DefaultWebSecurityManager getDefaultWebSecurityManager (@Qualifier ("userRealm") UserRealm userRealm ){
DefaultwebSecurityManager securityManager = new DefaultWebSecurityManager(); 
//关联UserRealm 
securityManager.setRealm(userRealm); 
return securityManager;
}

//创建 realm对象, 需要自定义类:1 
@Bean 
public UserRealm userRealm(){ 
return new UserRealm(); 
}
}
           

4. 实现登录拦截 – 添加shiro内置过滤器

在配置类ShiroConfig中的ShiroFilterFactoryBean 内添加

  • anon:无需认证就可以访问
  • authc:必须认证了才能让问
  • user:必须拥有记住我功能才能用
  • perms:拥有对某个资源的权限才能访问;
  • role:拥有某个角色权限才能访问
//shiroFilterFactoryBean:3 
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean (@Qualifier("securityManager") DefaultWebWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); 
//设置安全管理器
bean.setSecurityManager(defaultWebSecurityManager); 

    //添加shiro的内置过滤器
    Map<String, String> filterMap = new LinkedHashMap<>(); 
    //authc:必须认证了才能让问 
    filterMap.put("/user/add","authc"); 
    filterMap.put("/user/update","authc"); 
    bean.setFilterChainDefinitionMap(filterMap);

     //设置登录的请求
     bean.setLoginUrl("/toLogin");

return bean;
}
           

5. 实现用户验证

在控制器controller中添加登录功能

@RequestMapping("/1ogin")
public string login(String username,string password, Model model){
//获取当前的用户 
Subject subject = Securityutils.getSubject(); 
//封装用户的接录数据 
UsernamePasswordToken token = new UsernamePasswordToken(username, password); 
try { subject.login(token); //执行登录方法,如果没有异常就说明oK 
return "index":l 
}catch (UnknownAccountException e){ //用户名不存在
model.addAttribute("msg","用户名错误"); 
return "login"; 
}catch (IncorrectCredentialsException e){ //密码不存在
model.addAttribute("msg","密码错误"); 
return "1ogin";
}
}
           

在UserRealm中doGetAuthenticationInfo添加认证

//认证
@Override 
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws Authentication{
System.out.println("执行了=>认证doGetAuthorizationInfo"); 

//用户名,密码 从数据库中接收 
String name =root;
String password =123456;
UsernamePasswordToken userToken =(UsernamePasswordToken) token; 
if(!userToken getUsername()equals(name)){
 return nu1l;//抛出异常 UnknownAccountException 
 }
 //密码认证, shiro做 (当前用户认证,密码,认证名)
 return new SimpleAuthenticationInfo( "",password,"")

} 
           

6. 请求授权实现

在配置类ShiroConfig中的ShiroFilterFactoryBean 内添加

  • anon:无需认证就可以访问
  • authc:必须认证了才能让问
  • user:必须拥有记住我功能才能用
  • perms:拥有对某个资源的权限才能访问;
  • role:拥有某个角色权限才能访问

filterMap. put("/user/add",“perms [user: add]”); – 授权, 只有user用户存在add权限才能访问 ,否则跳转到未授权页面

//shiroFilterFactoryBean:3 
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean (@Qualifier("securityManager") DefaultWebWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); 
//设置安全管理器
bean.setSecurityManager(defaultWebSecurityManager); 

    //添加shiro的内置过滤器
    Map<String, String> filterMap = new LinkedHashMap<>(); 

     //授权, 只有user用户存在add权限才能访问 ,否则跳转到未授权页面
    filterMap. put("/user/add","perms [user: add]");

    //authc:必须认证了才能让问 
    filterMap.put("/user/add","authc"); 
    filterMap.put("/user/update","authc"); 
    bean.setFilterChainDefinitionMap(filterMap);

     //设置登录的请求
     bean.setLoginUrl("/toLogin");
   
     //未授权页面
     bean. setUnauthorizedUrl("/noauth");

return bean;
}
           

在UserRealm中doGetAuthenticationInfo添加授权

//授权 
@Override 
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){ 
System.out.println("执行了=>授权doGetAuthorizationInfo"); 
//SimpleAuthorizationInfo 
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(); 
//添加权限
info.addstringPermission("user:add"); 

//实际开发中权限包含在数据库中,从数据库中获取
//拿到当前登录的这个对象 
subiect subject = Securityutils.getsubiect(); 
User currentUser = (User) subject.getPrincipal(); //拿到User对象 
//设置当前用户的权限 
info. addstringPermission(currentUser.getPerms());


 return info; 
 }
           

7. 前端控制 – 根据权限显示功能

添加依赖

<dependency> 
  <groupId>com.github.theborakompanioni</groupId> 
  <artifactId>thymeleaf-extras-shiro</artifactId> 
  <version>2.0.0</version> 
</dependency>
           

在配置类ShiroConfig中添加ShiroDialect – 整合Shiro thymeleof

//整合ShiroDialect:用来整合 shiro thymeleaf 
@Bean 
public ShiroDialect getShiroDialect(){ 
return new ShiroDialect(); 
}
           

前端导入命名空间 xmlns:sec=“http://www.thymeleaforg/thymeleaf-extras-shiro”

<html lang="en"x mlns:th="http://www.thymeleaforg 
xmlns:sec="http://www.thymeleaforg/thymeleaf-extras-shiro">
           

前端根据权限显示功能

shiro:haspermission 判断是否有对应权限

<div shiro:haspermission="user:add"> 
<a th:href="@{/user/add}">add</a> 
</div>

<div shiro:haspermission="user :update"> 
<a th:href="@{/user/update}">update</a>
</div>