MaxKeyMgtMvcConfig.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright [2022] [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 java.util.List;
  18. import org.maxkey.authn.AbstractAuthenticationProvider;
  19. import org.maxkey.authn.support.jwt.HttpJwtEntryPoint;
  20. import org.maxkey.authn.support.jwt.JwtLoginService;
  21. import org.maxkey.authn.web.CurrentUserMethodArgumentResolver;
  22. import org.maxkey.authn.web.interceptor.PermissionInterceptor;
  23. import org.maxkey.configuration.ApplicationConfig;
  24. import org.maxkey.web.interceptor.HistoryLogsAdapter;
  25. import org.maxkey.web.interceptor.RestApiPermissionAdapter;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.context.annotation.Bean;
  30. import org.springframework.context.annotation.Configuration;
  31. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  32. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  33. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  34. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  35. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
  36. @Configuration
  37. @EnableWebMvc
  38. public class MaxKeyMgtMvcConfig implements WebMvcConfigurer {
  39. private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtMvcConfig.class);
  40. @Autowired
  41. ApplicationConfig applicationConfig;
  42. @Autowired
  43. AbstractAuthenticationProvider authenticationProvider ;
  44. @Autowired
  45. JwtLoginService jwtLoginService;
  46. @Autowired
  47. PermissionInterceptor permissionInterceptor;
  48. @Autowired
  49. HistoryLogsAdapter historyLogsAdapter;
  50. @Autowired
  51. RestApiPermissionAdapter restApiPermissionAdapter;
  52. @Override
  53. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  54. _logger.debug("addResourceHandlers");
  55. _logger.debug("add statics");
  56. registry.addResourceHandler("/static/**")
  57. .addResourceLocations("classpath:/static/");
  58. _logger.debug("add templates");
  59. registry.addResourceHandler("/templates/**")
  60. .addResourceLocations("classpath:/templates/");
  61. _logger.debug("add swagger");
  62. registry.addResourceHandler("swagger-ui.html")
  63. .addResourceLocations("classpath:/META-INF/resources/");
  64. registry.addResourceHandler("/webjars/**")
  65. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  66. _logger.debug("add knife4j");
  67. registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
  68. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  69. _logger.debug("addResourceHandler finished .");
  70. }
  71. @Override
  72. public void addInterceptors(InterceptorRegistry registry) {
  73. //addPathPatterns 用于添加拦截规则 , 先把所有路径都加入拦截, 再一个个排除
  74. //excludePathPatterns 表示改路径不用拦截
  75. _logger.debug("add HttpJwtEntryPoint");
  76. registry.addInterceptor(new HttpJwtEntryPoint(
  77. authenticationProvider,jwtLoginService,applicationConfig,true))
  78. .addPathPatterns("/login");
  79. permissionInterceptor.setMgmt(true);
  80. registry.addInterceptor(permissionInterceptor)
  81. .addPathPatterns("/dashboard/**")
  82. .addPathPatterns("/orgs/**")
  83. .addPathPatterns("/users/**")
  84. .addPathPatterns("/apps/**")
  85. .addPathPatterns("/accounts/**")
  86. .addPathPatterns("/access/**")
  87. .addPathPatterns("/access/**/**")
  88. .addPathPatterns("/permissions/**")
  89. .addPathPatterns("/permissions/**/**")
  90. .addPathPatterns("/config/**")
  91. .addPathPatterns("/config/**/**")
  92. .addPathPatterns("/historys/**")
  93. .addPathPatterns("/historys/**/**")
  94. .addPathPatterns("/institutions/**")
  95. .addPathPatterns("/localization/**")
  96. .addPathPatterns("/file/upload/")
  97. ;
  98. _logger.debug("add PermissionAdapter");
  99. registry.addInterceptor(historyLogsAdapter)
  100. .addPathPatterns("/userinfo/**")
  101. .addPathPatterns("/enterprises/**")
  102. .addPathPatterns("/employees/**")
  103. .addPathPatterns("/authInfo/**")
  104. .addPathPatterns("/usercenter/**")
  105. .addPathPatterns("/retrievePassword/**")
  106. .addPathPatterns("/roles/**")
  107. .addPathPatterns("/apps/**")
  108. .addPathPatterns("/approles/**")
  109. ;
  110. _logger.debug("add HistoryLogsAdapter");
  111. /*
  112. * api
  113. * idm
  114. * scim
  115. * */
  116. registry.addInterceptor(restApiPermissionAdapter)
  117. .addPathPatterns("/api/**")
  118. .addPathPatterns("/api/idm/**")
  119. .addPathPatterns("/api/idm/scim/**")
  120. ;
  121. _logger.debug("add RestApiPermissionAdapter");
  122. }
  123. @Override
  124. public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
  125. argumentResolvers.add(currentUserMethodArgumentResolver());
  126. }
  127. @Bean
  128. public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver() {
  129. return new CurrentUserMethodArgumentResolver();
  130. }
  131. }