MaxKeyMgtMvcConfig.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.autoconfigure;
  17. import java.util.List;
  18. import org.maxkey.authn.provider.AbstractAuthenticationProvider;
  19. import org.maxkey.authn.web.CurrentUserMethodArgumentResolver;
  20. import org.maxkey.authn.web.interceptor.PermissionInterceptor;
  21. import org.maxkey.configuration.ApplicationConfig;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.boot.autoconfigure.AutoConfiguration;
  26. import org.springframework.context.annotation.Bean;
  27. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  28. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  29. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  30. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  31. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
  32. @EnableWebMvc
  33. @AutoConfiguration
  34. public class MaxKeyMgtMvcConfig implements WebMvcConfigurer {
  35. private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtMvcConfig.class);
  36. @Autowired
  37. ApplicationConfig applicationConfig;
  38. @Autowired
  39. AbstractAuthenticationProvider authenticationProvider ;
  40. @Autowired
  41. PermissionInterceptor permissionInterceptor;
  42. @Override
  43. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  44. _logger.debug("add Resource Handlers");
  45. _logger.debug("add statics");
  46. registry.addResourceHandler("/static/**")
  47. .addResourceLocations("classpath:/static/");
  48. _logger.debug("add templates");
  49. registry.addResourceHandler("/templates/**")
  50. .addResourceLocations("classpath:/templates/");
  51. _logger.debug("add swagger");
  52. registry.addResourceHandler("swagger-ui.html")
  53. .addResourceLocations("classpath:/META-INF/resources/");
  54. registry.addResourceHandler("/webjars/**")
  55. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  56. _logger.debug("add knife4j");
  57. registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
  58. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  59. _logger.debug("add Resource Handler finished .");
  60. }
  61. @Override
  62. public void addInterceptors(InterceptorRegistry registry) {
  63. //addPathPatterns 用于添加拦截规则 , 先把所有路径都加入拦截, 再一个个排除
  64. //excludePathPatterns 表示改路径不用拦截
  65. _logger.debug("add Interceptors");
  66. permissionInterceptor.setMgmt(true);
  67. registry.addInterceptor(permissionInterceptor)
  68. .addPathPatterns("/dashboard/**")
  69. .addPathPatterns("/orgs/**")
  70. .addPathPatterns("/users/**")
  71. .addPathPatterns("/apps/**")
  72. .addPathPatterns("/session/**")
  73. .addPathPatterns("/accounts/**")
  74. .addPathPatterns("/access/**")
  75. .addPathPatterns("/access/**/**")
  76. .addPathPatterns("/permissions/**")
  77. .addPathPatterns("/permissions/**/**")
  78. .addPathPatterns("/config/**")
  79. .addPathPatterns("/config/**/**")
  80. .addPathPatterns("/historys/**")
  81. .addPathPatterns("/historys/**/**")
  82. .addPathPatterns("/institutions/**")
  83. .addPathPatterns("/localization/**")
  84. .addPathPatterns("/file/upload/")
  85. .addPathPatterns("/logout")
  86. .addPathPatterns("/logout/**")
  87. ;
  88. _logger.debug("add Permission Adapter");
  89. }
  90. @Override
  91. public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
  92. argumentResolvers.add(currentUserMethodArgumentResolver());
  93. }
  94. @Bean
  95. public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver() {
  96. return new CurrentUserMethodArgumentResolver();
  97. }
  98. }