|
| 1 | +package com.study.config.shiro; |
| 2 | + |
| 3 | +import org.apache.shiro.authc.credential.HashedCredentialsMatcher; |
| 4 | +import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; |
| 5 | +import org.apache.shiro.spring.web.ShiroFilterFactoryBean; |
| 6 | +import org.springframework.context.annotation.Bean; |
| 7 | +import org.springframework.context.annotation.Configuration; |
| 8 | +import org.apache.shiro.web.mgt.DefaultWebSecurityManager; |
| 9 | +import org.apache.shiro.mgt.SecurityManager; |
| 10 | + |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * Created by yangqj on 2017年4月23日. |
| 16 | + */ |
| 17 | +@Configuration |
| 18 | +public class ShiroConfig { |
| 19 | + |
| 20 | + /** |
| 21 | + * ShiroFilterFactoryBean 处理拦截资源文件问题。 |
| 22 | + * 注意:单独一个ShiroFilterFactoryBean配置是或报错的,因为在 |
| 23 | + * 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager |
| 24 | + * |
| 25 | + Filter Chain定义说明 |
| 26 | + 1、一个URL可以配置多个Filter,使用逗号分隔 |
| 27 | + 2、当设置多个过滤器时,全部验证通过,才视为通过 |
| 28 | + 3、部分过滤器可指定参数,如perms,roles |
| 29 | + * |
| 30 | + */ |
| 31 | + @Bean |
| 32 | + public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager){ |
| 33 | + System.out.println("ShiroConfiguration.shirFilter()"); |
| 34 | + ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); |
| 35 | + |
| 36 | + // 必须设置 SecurityManager |
| 37 | + shiroFilterFactoryBean.setSecurityManager(securityManager); |
| 38 | + //拦截器. |
| 39 | + Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); |
| 40 | + |
| 41 | + //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 |
| 42 | + filterChainDefinitionMap.put("/logout", "logout"); |
| 43 | + filterChainDefinitionMap.put("/static/**","anon"); |
| 44 | + //<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; |
| 45 | + //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> |
| 46 | + filterChainDefinitionMap.put("/user", "authc"); |
| 47 | + filterChainDefinitionMap.put("/**", "authc"); |
| 48 | + // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 |
| 49 | + shiroFilterFactoryBean.setLoginUrl("/login"); |
| 50 | + // 登录成功后要跳转的链接 |
| 51 | + shiroFilterFactoryBean.setSuccessUrl("/user"); |
| 52 | + //未授权界面; |
| 53 | + shiroFilterFactoryBean.setUnauthorizedUrl("/403"); |
| 54 | + |
| 55 | + shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); |
| 56 | + return shiroFilterFactoryBean; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + @Bean |
| 61 | + public SecurityManager securityManager(){ |
| 62 | + DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); |
| 63 | + //设置realm. |
| 64 | + securityManager.setRealm(myShiroRealm()); |
| 65 | + return securityManager; |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + public MyShiroRealm myShiroRealm(){ |
| 70 | + MyShiroRealm myShiroRealm = new MyShiroRealm(); |
| 71 | + myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());; |
| 72 | + return myShiroRealm; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * 凭证匹配器 |
| 77 | + * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 |
| 78 | + * 所以我们需要修改下doGetAuthenticationInfo中的代码; |
| 79 | + * ) |
| 80 | + * @return |
| 81 | + */ |
| 82 | + @Bean |
| 83 | + public HashedCredentialsMatcher hashedCredentialsMatcher(){ |
| 84 | + HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); |
| 85 | + |
| 86 | + hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; |
| 87 | + hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); |
| 88 | + |
| 89 | + return hashedCredentialsMatcher; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * 开启shiro aop注解支持. |
| 95 | + * 使用代理方式;所以需要开启代码支持; |
| 96 | + * @param securityManager |
| 97 | + * @return |
| 98 | + */ |
| 99 | + @Bean |
| 100 | + public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ |
| 101 | + AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); |
| 102 | + authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); |
| 103 | + return authorizationAttributeSourceAdvisor; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments