ShiroConfig.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package net.mingsoft.config;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import java.util.Properties;
  5. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
  6. import org.apache.shiro.mgt.SecurityManager;
  7. import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
  8. import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
  9. import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
  14. import com.mingsoft.basic.configurer.ShiroTagFreeMarkderConfigurer;
  15. import net.mingsoft.basic.security.BaseAuthRealm;
  16. @Configuration
  17. public class ShiroConfig {
  18. @Value("${ms.manager.path}")
  19. private String managerPath;
  20. @Bean
  21. public ShiroTagFreeMarkderConfigurer freemarkerConfig() {
  22. return new ShiroTagFreeMarkderConfigurer();
  23. }
  24. @Bean
  25. public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
  26. ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
  27. shiroFilterFactoryBean.setSecurityManager(securityManager);
  28. // 拦截器.
  29. Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
  30. // 配置不会被拦截的链接 顺序判断,因为前端模板采用了thymeleaf,这里不能直接使用 ("/static/**", "anon")来配置匿名访问,必须配置到每个静态目录
  31. filterChainDefinitionMap.put("/static/**", "anon");
  32. filterChainDefinitionMap.put("/html/**", "anon");
  33. filterChainDefinitionMap.put(managerPath+"/checkLogin.do", "anon");
  34. filterChainDefinitionMap.put(managerPath+"/login.do", "anon");
  35. // 配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了
  36. filterChainDefinitionMap.put("/logout", "logout");
  37. // <!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
  38. // <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
  39. filterChainDefinitionMap.put("/**", "anon");
  40. // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
  41. shiroFilterFactoryBean.setLoginUrl("/login");
  42. // 登录成功后要跳转的链接
  43. //shiroFilterFactoryBean.setSuccessUrl("/index");
  44. // 未授权界面;
  45. shiroFilterFactoryBean.setUnauthorizedUrl("/403");
  46. shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
  47. return shiroFilterFactoryBean;
  48. }
  49. /**
  50. * 凭证匹配器 (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 )
  51. *
  52. * @return
  53. */
  54. @Bean
  55. public HashedCredentialsMatcher hashedCredentialsMatcher() {
  56. HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
  57. hashedCredentialsMatcher.setHashAlgorithmName("md5");// 散列算法:这里使用MD5算法;
  58. hashedCredentialsMatcher.setHashIterations(2);// 散列的次数,比如散列两次,相当于
  59. // md5(md5(""));
  60. return hashedCredentialsMatcher;
  61. }
  62. @Bean
  63. public BaseAuthRealm myShiroRealm() {
  64. BaseAuthRealm myShiroRealm = new BaseAuthRealm();
  65. myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
  66. return myShiroRealm;
  67. }
  68. @Bean
  69. public SecurityManager securityManager() {
  70. DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
  71. securityManager.setRealm(myShiroRealm());
  72. return securityManager;
  73. }
  74. /**
  75. * 开启shiro aop注解支持. 使用代理方式;所以需要开启代码支持;
  76. *
  77. * @param securityManager
  78. * @return
  79. */
  80. @Bean
  81. public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
  82. AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
  83. authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
  84. return authorizationAttributeSourceAdvisor;
  85. }
  86. @Bean(name = "simpleMappingExceptionResolver")
  87. public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() {
  88. SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver();
  89. Properties mappings = new Properties();
  90. mappings.setProperty("DatabaseException", "databaseError");// 数据库异常处理
  91. mappings.setProperty("UnauthorizedException", "/user/403");
  92. r.setExceptionMappings(mappings); // None by default
  93. r.setDefaultErrorView("error"); // No default
  94. r.setExceptionAttribute("exception"); // Default is "exception"
  95. // r.setWarnLogCategory("example.MvcLogger"); // No default
  96. return r;
  97. }
  98. }