SpringBoot使用Shiro

[java]  view plain  copy
  1. <span style="font-family:Arial, Helvetica, sans-serif;background-color:rgb(255,255,255);">使用shiro重点在于两个类</span>  

1.ShiroConfiguration

下面这个方法是设置访问时,如果想直接访问index以及后面的界面必须先登录,所以会自动跳转到login界面

[html]  view plain  copy
  1. @Bean  
  2.     public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager){  
  3.         System.out.println("ShiroConfiguration.shirFilter()");  
  4.         ShiroFilterFactoryBean shiroFilterFactoryBean  = new ShiroFilterFactoryBean();  
  5.           
  6.          // 必须设置 SecurityManager    
  7.         shiroFilterFactoryBean.setSecurityManager(securityManager);  
  8.           
  9.         //拦截器.  
  10.         Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();  
  11.           
  12.         //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了  
  13.         filterChainDefinitionMap.put("/logout", "logout");  
  14.           
  15.           
  16.         //配置记住我或认证通过可以访问的地址  
  17.         filterChainDefinitionMap.put("/index", "user");  
  18.         filterChainDefinitionMap.put("/", "user");  
  19.           
  20.           
  21.         //<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;  
  22.         //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->  
  23.         filterChainDefinitionMap.put("/**", "authc");  
  24.           
  25.         // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面  
  26.         shiroFilterFactoryBean.setLoginUrl("/login");  
  27.         // 登录成功后要跳转的链接  
  28.         shiroFilterFactoryBean.setSuccessUrl("/index");  
  29.         //未授权界面;  
  30.         shiroFilterFactoryBean.setUnauthorizedUrl("/403");  
  31.           
  32.         shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);  
  33.         return shiroFilterFactoryBean;  
  34.     }  


这个方法是登录时我们必须做验证,登录名以及密码是否正确,所以在这我们自己定义的有MyShiroRealm,需要注入

[html]  view plain  copy
  1. <span style="font-size:24px;">//将我们自定义的Realm注入到SecurityManager中。  
  2.     @Bean  
  3.     public SecurityManager securityManager(){  
  4.         DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();  
  5.         //设置realm.  
  6.         securityManager.setRealm(myShiroRealm());   //做验证是通过Reaml来做的,所以在这必须通过securityManager来设置Reaml  
  7.         //注入缓存管理器  
  8.         securityManager.setCacheManager(ehCacheManager());  
  9.         return securityManager;  
  10.     }  
  11.       
  12.       
  13.   
  14.     @Bean  
  15.     public MyShiroRealm myShiroRealm(){  
  16.         MyShiroRealm myShiroRealm = new MyShiroRealm();//这里实例化一个我们自己写的MyShiroRealm类  
  17.         myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());;  
  18.         return myShiroRealm;  
  19.     }  
  20.       
  21.   
  22.     @Bean  
  23.     public HashedCredentialsMatcher hashedCredentialsMatcher(){  
  24.         HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();  
  25.           
  26.         hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法;  
  27.         hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5(""));  
  28.           
  29.         return hashedCredentialsMatcher;  
  30.     }  
  31.     //开启shiro aop注解支持  
  32.     @Bean  
  33.     public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){  
  34.        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();  
  35.        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);  
  36.        return authorizationAttributeSourceAdvisor;  
  37.     }  
  38.     //注入缓存  
  39.     @Bean  
  40.     public EhCacheManager ehCacheManager(){  
  41.         System.out.println("ShiroConfiguration.getEhCacheManager()执行");  
  42.         EhCacheManager cacheManager=new EhCacheManager();  
  43.         cacheManager.setCacheManagerConfigFile("classpath:config/ehcache-shiro.xml");  
  44.         return cacheManager;  
  45.     }</span>  

2.MyShiroRealm.java

[java]  view plain  copy
  1. //这个reaml是身份验证 的核心  
  2. public class MyShiroRealm extends AuthorizingRealm {  
  3.   
  4.     @Resource  
  5.     private UserInfoService userInfoService;  
  6.   
  7.     @Override  
  8.     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {  
  9.         System.out.println("MyShiroRealm.doGetAuthenticationInfo()");  
  10.   
  11.         // 获取用户的输入的账号.  
  12.         // token获取底层request对象来取得页面用户输入的用户名等值  
  13.         String username = (String) token.getPrincipal();  
  14.         System.out.println(token.getCredentials());  
  15.   
  16.         // 通过username从数据库中查找 User对象,如果找到,没找到.  
  17.         // 实际项目中,这里可以根据实际情况做缓存,如果不做,Shiro自己也是有时间间隔机制,2分钟内不会重复执行该方法  
  18.         UserInfo userInfo = userInfoService.findByUsername(username);  
  19.         System.out.println("----->>userInfo=" + userInfo);  
  20.         if (userInfo == null) {  
  21.             return null;  
  22.         }  
  23.         // 上面只是通过查询数据库找有没有输入用户名的数据,这里是如果存在则需要比较密码是否相同  
  24.         // 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配  
  25.         SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userInfo, // 用户名  
  26.                 userInfo.getPassword(), // 密码  
  27.                 // 这里用到了加密  
  28.                 ByteSource.Util.bytes(userInfo.getCredentialsSalt()), // salt=username+salt  
  29.                 getName() // realm name  
  30.         );  
  31.   
  32.         return authenticationInfo;  
  33.     }  
  34.   
  35.     @Override  
  36.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  
  37.         System.out.println("权限配置-->MyShiroRealm.doGetAuthorizationInfo");  
  38.         SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();  
  39.         UserInfo userInfo = (UserInfo) principals.getPrimaryPrincipal();  
  40.   
  41.         for (SysRole role : userInfo.getRoleList()) {  
  42.             authorizationInfo.addRole(role.getRole());//遍历用户下面的角色信息,添加角色  
  43.             for (SysPermission p : role.getPermissions()) {  
  44.                 authorizationInfo.addStringPermission(p.getPermission());//遍历角色的权限信息,添加权限   
  45.             }  
  46.         }  
  47.         return authorizationInfo;  
  48.     }  
  49.   
  50. }  

猜你喜欢

转载自blog.csdn.net/architect_csdn/article/details/80253549