Springcloud Gateway 整合 Spring Security 配置与踩坑

导读:本篇文章讲解 Springcloud Gateway 整合 Spring Security 配置与踩坑,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15259.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!