houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.framework.security.config;
H 2
3 import com.iailab.framework.security.config.AuthorizeRequestsCustomizer;
4 import com.iailab.module.infra.enums.ApiConstants;
5 import org.springframework.beans.factory.annotation.Value;
6 import org.springframework.context.annotation.Bean;
7 import org.springframework.context.annotation.Configuration;
8 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9 import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
10
11 /**
12  * Infra 模块的 Security 配置
13  */
14 @Configuration(proxyBeanMethods = false, value = "infraSecurityConfiguration")
15 public class SecurityConfiguration {
16
17     @Value("${spring.boot.admin.context-path:''}")
18     private String adminSeverContextPath;
19
20     @Bean("infraAuthorizeRequestsCustomizer")
21     public AuthorizeRequestsCustomizer authorizeRequestsCustomizer() {
22         return new AuthorizeRequestsCustomizer() {
23
24             @Override
25             public void customize(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry) {
26                 // Swagger 接口文档
27                 registry.antMatchers("/v3/api-docs/**").permitAll() // 元数据
28                         .antMatchers("/swagger-ui.html").permitAll(); // Swagger UI
29                 // Spring Boot Actuator 的安全配置
30                 registry.antMatchers("/actuator").anonymous()
31                         .antMatchers("/actuator/**").anonymous();
32                 // Druid 监控
33                 registry.antMatchers("/druid/**").anonymous();
34                 // Spring Boot Admin Server 的安全配置
35                 registry.antMatchers(adminSeverContextPath).anonymous()
36                         .antMatchers(adminSeverContextPath + "/**").anonymous();
37                 // 文件读取
38                 registry.antMatchers(buildAdminApi("/infra/file/*/get/**")).permitAll();
39
40                 // TODO iailab:这个每个项目都需要重复配置,得捉摸有没通用的方案
41                 // RPC 服务的安全配置
42                 registry.antMatchers(ApiConstants.PREFIX + "/**").permitAll();
43             }
44
45         };
46     }
47
48 }