MaxKeyMvcConfig.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.EnableWebMvc;
  36. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  37. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  38. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  39. import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
  40. @Configuration
  41. @EnableWebMvc
  42. public class MaxKeyMvcConfig implements WebMvcConfigurer {
  43. private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMvcConfig.class);
  44. @Autowired
  45. @Qualifier("applicationConfig")
  46. ApplicationConfig applicationConfig;
  47. @Autowired
  48. @Qualifier("authenticationProvider")
  49. AbstractAuthenticationProvider authenticationProvider ;
  50. @Autowired
  51. @Qualifier("remeberMeService")
  52. AbstractRemeberMeService remeberMeService;
  53. @Autowired
  54. @Qualifier("kerberosService")
  55. KerberosService kerberosService;
  56. @Autowired
  57. PermissionAdapter permissionAdapter;
  58. @Autowired
  59. HistoryLogsAdapter historyLogsAdapter;
  60. @Autowired
  61. LocaleChangeInterceptor localeChangeInterceptor;
  62. @Autowired
  63. PreLoginAppAdapter preLoginAppAdapter;
  64. @Autowired
  65. HistoryLoginAppAdapter historyLoginAppAdapter;
  66. @Value("${maxkey.login.httpheader.enable:false}")
  67. private boolean httpHeaderEnable;
  68. @Value("${maxkey.login.httpheader.headername:iv-user}")
  69. private String httpHeaderName;
  70. @Value("${maxkey.login.basic.enable:false}")
  71. private boolean basicEnable;
  72. @Override
  73. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  74. _logger.debug("addResourceHandlers");
  75. _logger.debug("add statics");
  76. registry.addResourceHandler("/static/**")
  77. .addResourceLocations("classpath:/static/");
  78. _logger.debug("add templates");
  79. registry.addResourceHandler("/templates/**")
  80. .addResourceLocations("classpath:/templates/");
  81. _logger.debug("add swagger");
  82. registry.addResourceHandler("swagger-ui.html")
  83. .addResourceLocations("classpath:/META-INF/resources/");
  84. registry.addResourceHandler("/webjars/**")
  85. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  86. _logger.debug("add knife4j");
  87. registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
  88. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  89. _logger.debug("addResourceHandler finished .");
  90. }
  91. @Override
  92. public void addInterceptors(InterceptorRegistry registry) {
  93. //addPathPatterns 用于添加拦截规则 , 先把所有路径都加入拦截, 再一个个排除
  94. //excludePathPatterns 表示改路径不用拦截
  95. _logger.debug("add HttpRemeberMeEntryPoint");
  96. registry.addInterceptor(new HttpRemeberMeEntryPoint(
  97. authenticationProvider,remeberMeService,applicationConfig,true))
  98. .addPathPatterns("/login");
  99. _logger.debug("add HttpKerberosEntryPoint");
  100. registry.addInterceptor(new HttpKerberosEntryPoint(
  101. authenticationProvider,kerberosService,applicationConfig,true))
  102. .addPathPatterns("/login");
  103. if(httpHeaderEnable) {
  104. registry.addInterceptor(new HttpHeaderEntryPoint(httpHeaderName,httpHeaderEnable))
  105. .addPathPatterns("/*");
  106. _logger.debug("add HttpHeaderEntryPoint");
  107. }
  108. if(basicEnable) {
  109. registry.addInterceptor(new BasicEntryPoint(basicEnable))
  110. .addPathPatterns("/*");
  111. _logger.debug("add BasicEntryPoint");
  112. }
  113. registry.addInterceptor(permissionAdapter)
  114. .addPathPatterns("/index/**")
  115. .addPathPatterns("/logs/**")
  116. .addPathPatterns("/userinfo/**")
  117. .addPathPatterns("/profile/**")
  118. .addPathPatterns("/safe/**")
  119. .addPathPatterns("/historys/**")
  120. .addPathPatterns("/session/**")
  121. .addPathPatterns("/session/**/**")
  122. .addPathPatterns("/appList")
  123. .addPathPatterns("/appList/**")
  124. .addPathPatterns("/socialsignon/**")
  125. .addPathPatterns("/authz/basic/*")
  126. .addPathPatterns("/authz/ltpa/*")
  127. //Form based
  128. .addPathPatterns("/authz/formbased/*")
  129. //Token based
  130. .addPathPatterns("/authz/tokenbased/*")
  131. //JWT
  132. .addPathPatterns("/authz/jwt/*")
  133. //SAML
  134. .addPathPatterns("/authz/saml20/idpinit/*")
  135. .addPathPatterns("/authz/saml20/assertion")
  136. .addPathPatterns("/authz/saml20/assertion/")
  137. //CAS
  138. .addPathPatterns("/authz/cas/*")
  139. .addPathPatterns("/authz/cas/*/*")
  140. .addPathPatterns("/authz/cas/login")
  141. .addPathPatterns("/authz/cas/login/")
  142. .addPathPatterns("/authz/cas/granting/*")
  143. //cas1.0 validate
  144. .excludePathPatterns("/authz/cas/validate")
  145. //cas2.0 Validate
  146. .excludePathPatterns("/authz/cas/serviceValidate")
  147. .excludePathPatterns("/authz/cas/proxyValidate")
  148. .excludePathPatterns("/authz/cas/proxy")
  149. //cas3.0 Validate
  150. .excludePathPatterns("/authz/cas/p3/serviceValidate")
  151. .excludePathPatterns("/authz/cas/p3/proxyValidate")
  152. .excludePathPatterns("/authz/cas/p3/proxy")
  153. //rest
  154. .excludePathPatterns("/authz/cas/v1/tickets")
  155. .excludePathPatterns("/authz/cas/v1/tickets/*")
  156. //OAuth
  157. .addPathPatterns("/authz/oauth/v20/authorize")
  158. .addPathPatterns("/authz/oauth/v20/authorize/*")
  159. //online ticket Validate
  160. .excludePathPatterns("/onlineticket/ticketValidate")
  161. .excludePathPatterns("/onlineticket/ticketValidate/*")
  162. ;
  163. _logger.debug("add PermissionAdapter");
  164. registry.addInterceptor(historyLogsAdapter)
  165. .addPathPatterns("/safe/changePassword/**")
  166. ;
  167. _logger.debug("add HistoryLogsAdapter");
  168. registry.addInterceptor(preLoginAppAdapter)
  169. .addPathPatterns("/authz/basic/*")
  170. .addPathPatterns("/authz/ltpa/*")
  171. //Form based
  172. .addPathPatterns("/authz/formbased/*")
  173. //Token based
  174. .addPathPatterns("/authz/tokenbased/*")
  175. //JWT
  176. .addPathPatterns("/authz/jwt/*")
  177. //SAML
  178. .addPathPatterns("/authz/saml20/idpinit/*")
  179. .addPathPatterns("/authz/saml20/assertion")
  180. //CAS
  181. .addPathPatterns("/authz/cas/login")
  182. .addPathPatterns("/authz/cas/granting")
  183. ;
  184. _logger.debug("add PreLoginAppAdapter");
  185. registry.addInterceptor(historyLoginAppAdapter)
  186. .addPathPatterns("/authz/basic/*")
  187. .addPathPatterns("/authz/ltpa/*")
  188. //Extend api
  189. .addPathPatterns("/authz/api/*")
  190. //Form based
  191. .addPathPatterns("/authz/formbased/*")
  192. //Token based
  193. .addPathPatterns("/authz/tokenbased/*")
  194. //JWT
  195. .addPathPatterns("/authz/jwt/*")
  196. //SAML
  197. .addPathPatterns("/authz/saml20/idpinit/*")
  198. .addPathPatterns("/authz/saml20/assertion")
  199. //CAS
  200. .addPathPatterns("/authz/cas/granting")
  201. //OAuth
  202. .addPathPatterns("/authz/oauth/v20/approval_confirm")
  203. ;
  204. _logger.debug("add HistoryLoginAppAdapter");
  205. registry.addInterceptor(localeChangeInterceptor);
  206. _logger.debug("add LocaleChangeInterceptor");
  207. }
  208. }