MaxKeyMvcConfig.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey;
  17. import org.maxkey.authn.AbstractAuthenticationProvider;
  18. import org.maxkey.authn.support.basic.BasicEntryPoint;
  19. import org.maxkey.authn.support.httpheader.HttpHeaderEntryPoint;
  20. import org.maxkey.authn.support.kerberos.HttpKerberosEntryPoint;
  21. import org.maxkey.authn.support.kerberos.KerberosService;
  22. import org.maxkey.authn.support.rememberme.AbstractRemeberMeService;
  23. import org.maxkey.authn.support.rememberme.HttpRemeberMeEntryPoint;
  24. import org.maxkey.configuration.ApplicationConfig;
  25. import org.maxkey.web.interceptor.HistoryLoginAppAdapter;
  26. import org.maxkey.web.interceptor.HistoryLogsAdapter;
  27. import org.maxkey.web.interceptor.PermissionAdapter;
  28. import org.maxkey.web.interceptor.PreLoginAppAdapter;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.beans.factory.annotation.Qualifier;
  33. import org.springframework.beans.factory.annotation.Value;
  34. import org.springframework.context.annotation.Configuration;
  35. import org.springframework.web.servlet.config.annotation.*;
  36. import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
  37. @Configuration
  38. @EnableWebMvc
  39. public class MaxKeyMvcConfig implements WebMvcConfigurer {
  40. private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMvcConfig.class);
  41. @Autowired
  42. @Qualifier("applicationConfig")
  43. ApplicationConfig applicationConfig;
  44. @Autowired
  45. @Qualifier("authenticationProvider")
  46. AbstractAuthenticationProvider authenticationProvider ;
  47. @Autowired
  48. @Qualifier("remeberMeService")
  49. AbstractRemeberMeService remeberMeService;
  50. @Autowired
  51. @Qualifier("kerberosService")
  52. KerberosService kerberosService;
  53. @Autowired
  54. PermissionAdapter permissionAdapter;
  55. @Autowired
  56. HistoryLogsAdapter historyLogsAdapter;
  57. @Autowired
  58. LocaleChangeInterceptor localeChangeInterceptor;
  59. @Autowired
  60. PreLoginAppAdapter preLoginAppAdapter;
  61. @Autowired
  62. HistoryLoginAppAdapter historyLoginAppAdapter;
  63. @Value("${maxkey.login.httpheader.enable:false}")
  64. private boolean httpHeaderEnable;
  65. @Value("${maxkey.login.httpheader.headername:iv-user}")
  66. private String httpHeaderName;
  67. @Value("${maxkey.login.basic.enable:false}")
  68. private boolean basicEnable;
  69. @Override
  70. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  71. _logger.debug("addResourceHandlers");
  72. _logger.debug("add statics");
  73. registry.addResourceHandler("/static/**")
  74. .addResourceLocations("classpath:/static/");
  75. _logger.debug("add templates");
  76. registry.addResourceHandler("/templates/**")
  77. .addResourceLocations("classpath:/templates/");
  78. _logger.debug("add swagger");
  79. registry.addResourceHandler("swagger-ui.html")
  80. .addResourceLocations("classpath:/META-INF/resources/");
  81. registry.addResourceHandler("/webjars/**")
  82. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  83. _logger.debug("add knife4j");
  84. registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
  85. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  86. _logger.debug("addResourceHandler finished .");
  87. }
  88. @Override
  89. public void addInterceptors(InterceptorRegistry registry) {
  90. //addPathPatterns 用于添加拦截规则 , 先把所有路径都加入拦截, 再一个个排除
  91. //excludePathPatterns 表示改路径不用拦截
  92. _logger.debug("add HttpRemeberMeEntryPoint");
  93. registry.addInterceptor(new HttpRemeberMeEntryPoint(
  94. authenticationProvider,remeberMeService,applicationConfig,true))
  95. .addPathPatterns("/login");
  96. _logger.debug("add HttpKerberosEntryPoint");
  97. registry.addInterceptor(new HttpKerberosEntryPoint(
  98. authenticationProvider,kerberosService,applicationConfig,true))
  99. .addPathPatterns("/login");
  100. if(httpHeaderEnable) {
  101. registry.addInterceptor(new HttpHeaderEntryPoint(httpHeaderName,httpHeaderEnable))
  102. .addPathPatterns("/*");
  103. _logger.debug("add HttpHeaderEntryPoint");
  104. }
  105. if(basicEnable) {
  106. registry.addInterceptor(new BasicEntryPoint(basicEnable))
  107. .addPathPatterns("/*");
  108. _logger.debug("add BasicEntryPoint");
  109. }
  110. registry.addInterceptor(permissionAdapter)
  111. .addPathPatterns("/index/**")
  112. .addPathPatterns("/logs/**")
  113. .addPathPatterns("/userinfo/**")
  114. .addPathPatterns("/profile/**")
  115. .addPathPatterns("/safe/**")
  116. .addPathPatterns("/historys/**")
  117. .addPathPatterns("/session/**")
  118. .addPathPatterns("/session/**/**")
  119. .addPathPatterns("/appList")
  120. .addPathPatterns("/appList/**")
  121. .addPathPatterns("/socialsignon/**")
  122. .addPathPatterns("/authz/basic/*")
  123. .addPathPatterns("/authz/ltpa/*")
  124. //Form based
  125. .addPathPatterns("/authz/formbased/*")
  126. //Token based
  127. .addPathPatterns("/authz/tokenbased/*")
  128. //JWT
  129. .addPathPatterns("/authz/jwt/*")
  130. //SAML
  131. .addPathPatterns("/authz/saml20/idpinit/*")
  132. .addPathPatterns("/authz/saml20/assertion")
  133. .addPathPatterns("/authz/saml20/assertion/")
  134. //CAS
  135. .addPathPatterns("/authz/cas/*")
  136. .addPathPatterns("/authz/cas/*/*")
  137. .addPathPatterns("/authz/cas/login")
  138. .addPathPatterns("/authz/cas/login/")
  139. .addPathPatterns("/authz/cas/granting/*")
  140. //cas1.0 validate
  141. .excludePathPatterns("/authz/cas/validate")
  142. //cas2.0 Validate
  143. .excludePathPatterns("/authz/cas/serviceValidate")
  144. .excludePathPatterns("/authz/cas/proxyValidate")
  145. .excludePathPatterns("/authz/cas/proxy")
  146. //cas3.0 Validate
  147. .excludePathPatterns("/authz/cas/p3/serviceValidate")
  148. .excludePathPatterns("/authz/cas/p3/proxyValidate")
  149. .excludePathPatterns("/authz/cas/p3/proxy")
  150. //rest
  151. .excludePathPatterns("/authz/cas/v1/tickets")
  152. .excludePathPatterns("/authz/cas/v1/tickets/*")
  153. // third login
  154. .excludePathPatterns("/thirdLogin/*")
  155. // 注册
  156. .excludePathPatterns("/registration/registeron")
  157. //OAuth
  158. .addPathPatterns("/authz/oauth/v20/authorize")
  159. .addPathPatterns("/authz/oauth/v20/authorize/*")
  160. //OAuth TENCENT_IOA
  161. .addPathPatterns("/oauth2/authorize")
  162. .addPathPatterns("/oauth2/authorize/*")
  163. //online ticket Validate
  164. .excludePathPatterns("/onlineticket/ticketValidate")
  165. .excludePathPatterns("/onlineticket/ticketValidate/*")
  166. ;
  167. _logger.debug("add PermissionAdapter");
  168. registry.addInterceptor(historyLogsAdapter)
  169. .addPathPatterns("/safe/changePassword/**")
  170. ;
  171. _logger.debug("add HistoryLogsAdapter");
  172. registry.addInterceptor(preLoginAppAdapter)
  173. .addPathPatterns("/authz/basic/*")
  174. .addPathPatterns("/authz/ltpa/*")
  175. //Form based
  176. .addPathPatterns("/authz/formbased/*")
  177. //Token based
  178. .addPathPatterns("/authz/tokenbased/*")
  179. //JWT
  180. .addPathPatterns("/authz/jwt/*")
  181. //SAML
  182. .addPathPatterns("/authz/saml20/idpinit/*")
  183. .addPathPatterns("/authz/saml20/assertion")
  184. //CAS
  185. .addPathPatterns("/authz/cas/login")
  186. .addPathPatterns("/authz/cas/granting")
  187. ;
  188. _logger.debug("add PreLoginAppAdapter");
  189. registry.addInterceptor(historyLoginAppAdapter)
  190. .addPathPatterns("/authz/basic/*")
  191. .addPathPatterns("/authz/ltpa/*")
  192. //Extend api
  193. .addPathPatterns("/authz/api/*")
  194. //Form based
  195. .addPathPatterns("/authz/formbased/*")
  196. //Token based
  197. .addPathPatterns("/authz/tokenbased/*")
  198. //JWT
  199. .addPathPatterns("/authz/jwt/*")
  200. //SAML
  201. .addPathPatterns("/authz/saml20/idpinit/*")
  202. .addPathPatterns("/authz/saml20/assertion")
  203. //CAS
  204. .addPathPatterns("/authz/cas/granting")
  205. //OAuth
  206. .addPathPatterns("/authz/oauth/v20/approval_confirm")
  207. ;
  208. _logger.debug("add HistoryLoginAppAdapter");
  209. registry.addInterceptor(localeChangeInterceptor);
  210. _logger.debug("add LocaleChangeInterceptor");
  211. }
  212. @Override
  213. public void addCorsMappings(CorsRegistry registry) {
  214. //添加映射路径
  215. registry.addMapping("/**")
  216. //是否发送Cookie
  217. .allowCredentials(true)
  218. //设置放行哪些原始域 SpringBoot2.4.4下低版本使用.allowedOrigins("*")
  219. .allowedOriginPatterns("*")
  220. //放行哪些请求方式
  221. .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
  222. //.allowedMethods("*") //或者放行全部
  223. //放行哪些原始请求头部信息
  224. .allowedHeaders("*")
  225. //暴露哪些原始请求头部信息
  226. .exposedHeaders("*");
  227. }
  228. }