Springcloud Gateway 整合 Spring Security 配置与踩坑
Springcloud Gateway 整合 Spring Security 配置与踩坑1. pom配置<?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-instan
·
1. Security 配置与踩坑
由于 SpringCloud Gateway 基于WebFlux 并且不兼容SpringMVC,因此对于Security的配置方式也跟普通SpringBoot项目中的配置方式不同。
在Gateway项目中使用的WebFlux,是不能和Spring-Web混合使用的。
1.1 在Gateway中 Security 配置类不应该使用 @EnableWebSecurity 而是应该使用 @EnableWebFluxSecurity,并且配置方式也不同
-
例如: 常见的配置方式 @EnableWebSecurity
@EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) //启用方法级的权限认证 public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity httpSecurity) throws Exception { ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity .authorizeRequests(); registry.antMatchers(HttpMethod.OPTIONS) .permitAll(); // 任何请求需要身份认证 registry.antMatchers("/**").permitAll() .and().csrf().disable(); } }
-
例如:在Gateway中应该使用 WebFlux 的配置方式
@EnableWebSecurity @EnableWebFluxSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig2 { /** * 配置方式要换成 WebFlux的方式 */ @Bean public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity) { httpSecurity .authorizeExchange().pathMatchers(HttpMethod.OPTIONS).permitAll() // 任何请求需要身份认证 .pathMatchers("/**").permitAll().and() .csrf().disable(); return httpSecurity.build(); } }
1.2 Gateway 中导入 spring-boot-starter-web 也会报错
-
报错信息如下:
********************************************************** Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency. ********************************************************** *************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found. Action: Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration. Process finished with exit code 1
-
解决方案
<!--这个要注释掉,因为Gateway 不支持SpringMVC--> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> -->
1.3. 附: pom.xml 示例
<?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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test.example</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
更多推荐
已为社区贡献2条内容
所有评论(0)