Springcloud Gateway 整合 Spring Security 配置与踩坑

1. Security 配置与踩坑

由于 SpringCloud Gateway 基于WebFlux 并且不兼容SpringMVC,因此对于Security的配置方式也跟普通SpringBoot项目中的配置方式不同。

在Gateway项目中使用的WebFlux,是不能和Spring-Web混合使用的。

在这里插入图片描述

1.1 在Gateway中 Security 配置类不应该使用 @EnableWebSecurity 而是应该使用 @EnableWebFluxSecurity,并且配置方式也不同

  1. 例如: 常见的配置方式 @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();
       
           }
       }
    
  2. 例如:在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 也会报错

  1. 报错信息如下:

     **********************************************************
    
        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
    
  2. 解决方案

     <!--这个要注释掉,因为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>

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值