Tho*_*ich 33 spring-security spring-boot
我目前正在将 REST 应用程序从 Spring Boot 2.7.5 迁移到 3.0.0-RC2。我希望除了 Open API URL 之外的所有内容都是安全的。在 Spring Boot 2.7.5 中,我们曾经这样做:
@Named
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/openapi/openapi.yml").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
Run Code Online (Sandbox Code Playgroud)
效果很好。在 Spring Boot 3 中,我不得不将其更改为
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/openapi/openapi.yml").permitAll()
.anyRequest()
.authenticated())
.httpBasic();
return http.build();
}
}
Run Code Online (Sandbox Code Playgroud)
由于 WebSecurityConfigurerAdapter 已被删除。但它不起作用。Open API URL 也通过基本身份验证得到保护。我在升级代码时是否犯了错误,或者这可能是 Spring Boot 3 RC 2 中的问题?
更新 由于大多数新 API 已在 2.7.5 中提供,我已将 2.7.5 代码库中的代码更新为以下内容:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeHttpRequests((requests) -> requests
.antMatchers(OPTIONS).permitAll() // allow CORS option calls for Swagger UI
.antMatchers("/openapi/openapi.yml").permitAll()
.anyRequest().authenticated())
.httpBasic();
return http.build();
}
}
Run Code Online (Sandbox Code Playgroud)
在我们的 3.0.0-RC2 分支中,代码现在如下:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeHttpRequests((requests) -> requests
.requestMatchers(OPTIONS).permitAll() // allow CORS option calls for Swagger UI
.requestMatchers("/openapi/openapi.yml").permitAll()
.anyRequest().authenticated())
.httpBasic();
return http.build();
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,唯一的区别是我调用 requestMatchers 而不是 antMatchers。该方法似乎已被重命名。antMatchers 方法不再可用。但最终的效果还是一样。在我们的 3.0.0-RC2 分支上,Spring Boot 要求对 OpenAPI URL 进行基本身份验证。在 2.7.5 上仍然可以正常工作。
nhu*_*uvy 24
作者:https: //github.com/wilkinsona
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(requests -> requests
.requestMatchers(new AntPathRequestMatcher("/openapi/openapi.yml")).permitAll()
.anyRequest().authenticated())
.httpBasic();
return http.build();
}
Run Code Online (Sandbox Code Playgroud)
来源:https ://github.com/spring-projects/spring-boot/issues/33357#issuecomment-1327301183
我建议您现在使用 Spring Boot 3.0.0 (GA),而不是 RC 版本。
小智 10
在我的 WebSecurityConfig 中,我这样做了:
private static final String[] AUTH_WHITELIST = {
// -- Swagger UI v2
"/v2/api-docs",
"v2/api-docs",
"/swagger-resources",
"swagger-resources",
"/swagger-resources/**",
"swagger-resources/**",
"/configuration/ui",
"configuration/ui",
"/configuration/security",
"configuration/security",
"/swagger-ui.html",
"swagger-ui.html",
"webjars/**",
// -- Swagger UI v3
"/v3/api-docs/**",
"v3/api-docs/**",
"/swagger-ui/**",
"swagger-ui/**",
// CSA Controllers
"/csa/api/token",
// Actuators
"/actuator/**",
"/health/**"
};
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests( auth -> auth
.requestMatchers(AUTH_WHITELIST).permitAll()
.anyRequest().authenticated()
)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.httpBasic(withDefaults())
.addFilterBefore(authenticationJwtTokenFilter, UsernamePasswordAuthenticationFilter.class)
//.addFilterAfter(authenticationJwtTokenFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
@Bean
public SecurityFilterChain configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeHttpRequests((requests) -> requests
.requestMatchers( new AntPathRequestMatcher("swagger-ui/**")).permitAll()
.requestMatchers( new AntPathRequestMatcher("/swagger-ui/**")).permitAll()
.requestMatchers( new AntPathRequestMatcher("v3/api-docs/**")).permitAll()
.requestMatchers( new AntPathRequestMatcher("/v3/api-docs/**")).permitAll()
.anyRequest().authenticated())
.httpBasic();
return httpSecurity.build();
}
Run Code Online (Sandbox Code Playgroud)
这和使用 Dockerfile(从 Docker 执行mvn clean package并运行 .jar)使我在 swagger ui 内的身份验证没有问题。
希望这可以帮到你 :)
| 归档时间: |
|
| 查看次数: |
80901 次 |
| 最近记录: |