spring-boot - 将 Spring Security 与 Spring Gateway 一起使用时出现“无法访问 javax.servlet.Filter”错误
问题描述
我将 Spring Gateway 和 Spring Security 放在一起,因为我想保护我的网关。但是在实现以下类扩展WebSecurityConfigurerAdapter
后,项目抛出
java:无法访问 javax.servlet.Filter
@Configuration
@EnableWebSecurity
public class SecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
InMemoryUserDetailsManager userDetails = new InMemoryUserDetailsManager();
UserDetails user = User.withUsername("foo").password(passwordEncoder().encode("bar")).build();
userDetails.createUser(user);
auth.userDetailsService(userDetails).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic();
http.authorizeRequests().anyRequest().authenticated();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(8);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<groupId>com.gabriel</groupId>
<artifactId>gateway-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>16</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
解决方案
来自 Spring Cloud Gateway文档:
Spring Cloud Gateway 需要 Spring Boot 和 Spring Webflux 提供的 Netty 运行时。它不适用于传统的 Servlet 容器或构建为 WAR 时。
扩展WebSecurityConfigurerAdapter
适用于基于 servlet 的应用程序。
您应该为响应式应用程序配置 Spring Security。
@Configuration
@EnableWebFluxSecurity
public class HelloWebfluxSecurityConfig {
@Bean
public MapReactiveUserDetailsService userDetailsService() {
UserDetails user = User.withUsername("foo").password(passwordEncoder().encode("bar")).build();
return new MapReactiveUserDetailsService(user);
}
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges -> exchanges
.anyExchange().authenticated()
)
.httpBasic(withDefaults());
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(8);
}
}
推荐阅读
- intellij-idea - 如何更改左侧区域的背景颜色(不是编辑器,这很容易!)
- typescript - 从模板中排除属性
- ios - Swift:在只下载一次的同时使用多个托管 JSON 文件内容的最佳方式
- ms-access - 如何使用 sql 管理工具或任何其他工具连接到 ms access 数据库?
- python - 熊猫不会在空列上合并
- python - 生成基于文本的直方图
- properties - Kotlin - 可空属性默认为空值
- java - 在 apache flink 中的 Flink 连接流中面临竞争条件
- javascript - 从后面的代码触发 JavaScript Confirm()
- java - 使用最少数量的矩形覆盖“曼哈顿天际线”