ShiroConfig.kt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package cn.gygxzc.envir.config.shiro
  2. import cn.gygxzc.envir.config.shiro.cache.RedisConfiguration
  3. import cn.gygxzc.envir.config.shiro.filter.AjaxAuthorizationFilter
  4. import cn.gygxzc.envir.config.shiro.filter.UrlPermissionsFilter
  5. import cn.gygxzc.envir.config.shiro.filter.ZenCorsAnonymousFilter
  6. import cn.gygxzc.envir.config.shiro.filter.ZenCorsPathMatchingFilter
  7. import cn.gygxzc.envir.config.shiro.realm.ZenShiroRealm
  8. import org.apache.shiro.authc.credential.HashedCredentialsMatcher
  9. import org.apache.shiro.cache.CacheManager
  10. import org.apache.shiro.mgt.SecurityManager
  11. import org.apache.shiro.realm.AuthorizingRealm
  12. import org.apache.shiro.spring.LifecycleBeanPostProcessor
  13. import org.apache.shiro.spring.web.ShiroFilterFactoryBean
  14. import org.apache.shiro.web.filter.authc.AnonymousFilter
  15. import org.apache.shiro.web.mgt.DefaultWebSecurityManager
  16. import org.apache.shiro.web.session.mgt.ServletContainerSessionManager
  17. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
  18. import org.springframework.boot.web.servlet.FilterRegistrationBean
  19. import org.springframework.context.annotation.Bean
  20. import org.springframework.context.annotation.Configuration
  21. import org.springframework.context.annotation.DependsOn
  22. import org.springframework.core.annotation.Order
  23. import org.springframework.web.filter.DelegatingFilterProxy
  24. import javax.servlet.DispatcherType
  25. import javax.servlet.Filter
  26. /**
  27. * Shiro配置
  28. * @author NorthLan
  29. * @date 2018/4/21
  30. * @url https://noahlan.com
  31. */
  32. @Configuration
  33. @ConditionalOnClass(value = [RedisConfiguration::class])
  34. class ShiroConfig {
  35. @Bean("shiroLifecycleBeanPostProcessor")
  36. fun lifecycleBeanPostProcessor(): LifecycleBeanPostProcessor {
  37. return LifecycleBeanPostProcessor()
  38. }
  39. @Bean
  40. @DependsOn(value = ["shiroLifecycleBeanPostProcessor", "shrioRedisCacheManager"])
  41. fun userRealm(cacheManager: CacheManager): ZenShiroRealm {
  42. val shiroRealm = ZenShiroRealm()
  43. .apply {
  44. isCachingEnabled = true
  45. isAuthenticationCachingEnabled = true
  46. isAuthorizationCachingEnabled = true
  47. //加密验证方法
  48. credentialsMatcher = HashedCredentialsMatcher().also {
  49. it.hashAlgorithmName = "md5"
  50. it.hashIterations = 2 // 两次md5
  51. }
  52. }
  53. shiroRealm.cacheManager = cacheManager
  54. return shiroRealm
  55. }
  56. @Bean(name = ["securityManager"])
  57. fun securityManager(userRealm: AuthorizingRealm,
  58. redisCacheManager: CacheManager): DefaultWebSecurityManager {
  59. return DefaultWebSecurityManager().apply {
  60. setRealm(userRealm)
  61. cacheManager = redisCacheManager
  62. sessionManager = ServletContainerSessionManager()
  63. }
  64. }
  65. @Bean("shiroFilterRegistrationBean")
  66. @DependsOn("shiroFilter")
  67. fun filterRegistrationBean(): FilterRegistrationBean<DelegatingFilterProxy> {
  68. return FilterRegistrationBean<DelegatingFilterProxy>().apply {
  69. filter = DelegatingFilterProxy("shiroFilter")
  70. isEnabled = true
  71. addUrlPatterns("/*")
  72. setDispatcherTypes(DispatcherType.REQUEST)
  73. }
  74. }
  75. @Bean(name = ["shiroFilter"])
  76. @Order(2)
  77. fun shiroFilter(securityManager: SecurityManager): ShiroFilterFactoryBean {
  78. return ShiroFilterFactoryBean().apply {
  79. setSecurityManager(securityManager)
  80. filters = hashMapOf<String, Filter>(
  81. "canon" to ZenCorsAnonymousFilter(),
  82. "cors" to ZenCorsPathMatchingFilter(),
  83. "perms" to UrlPermissionsFilter(),
  84. "authc" to AjaxAuthorizationFilter(),
  85. "anon" to AnonymousFilter()
  86. )
  87. /**
  88. * anon(匿名) org.apache.shiro.web.filter.authc.AnonymousFilter
  89. * authc(身份验证) org.apache.shiro.web.filter.authc.FormAuthenticationFilter
  90. * authcBasic(http基本验证) org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
  91. * logout(退出) org.apache.shiro.web.filter.authc.LogoutFilter
  92. * noSessionCreation(不创建session) org.apache.shiro.web.filter.session.NoSessionCreationFilter
  93. * perms(许可验证) org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
  94. * port(端口验证) org.apache.shiro.web.filter.authz.PortFilter
  95. * rest (rest方面) org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
  96. * roles(权限验证) org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
  97. * ssl (ssl方面) org.apache.shiro.web.filter.authz.SslFilter
  98. * member (用户方面) org.apache.shiro.web.filter.authc.UserFilter
  99. * user 表示用户不一定已通过认证,只要曾被Shiro记住过登录状态的用户就可以正常发起请求,比如rememberMe
  100. */
  101. filterChainDefinitionMap = linkedMapOf<String, String>( // BUG 此处一定要使用 linkedHashMap 否则顺序有问题
  102. "/auth/setcookie" to "canon", // 设置cookie
  103. "/auth/check" to "canon", // 检查登录状态
  104. "/auth/logout" to "canon", // 登出
  105. "/test/**" to "canon", // 测试 免登录
  106. "/upload/**" to "canon", // 上传免登录
  107. "/fdfsupload/**" to "canon", // 上传免登录
  108. "/api/**" to "canon", // api 免登陆
  109. "/ws/**" to "canon", // websocket 免登陆
  110. "/" to "canon",
  111. "/v1/login/**" to "canon",
  112. ////////////////////// 静态资源 /////////////////////
  113. "/v2/api-docs" to "canon",
  114. "/swagger-resources/**" to "anon",
  115. "/swagger-ui.html" to "anon",
  116. "/webjars*" to "anon",
  117. "/webjars/**" to "anon",
  118. "/druid/**" to "anon",
  119. "/druid/sql.json" to "anon",
  120. "/**/favicon.*" to "anon",
  121. "/eureka*" to "anon",
  122. "/eureka/**" to "anon",
  123. "/v1/sys" to "anon",
  124. ////////////////////// 静态资源 /////////////////////
  125. "/**" to "cors,authc" // 对于其他未配置的所有url 先设置cors头 再进行登陆判定 最后判定权限
  126. )
  127. }
  128. }
  129. }