SpringBoot2.* GateWay网关中关闭security验证

该博客介绍了如何在使用WebFlux技术的Spring Cloud Gateway项目中配置Spring Security,允许所有请求并禁用基本认证、CSRF、登出和表单登录功能。强调了WebFlux环境与传统Servlet环境下的安全配置差异。
摘要由CSDN通过智能技术生成
package com.njcb.corp.gateway.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @author Lei Ji Hui
 * @version V1.0
 * @date 2021/8/20 17:19
 * @description: 注意:webflux环境下要生效必须用注解@EnableWebFluxSecurity使其生效
 * cloud gateway采用的webflux技术(此处与web不同)
 * @className WebSecurityConfig
 **/
@Configuration
@EnableWebFluxSecurity
@Slf4j
public class WebSecurityConfig {

    @Bean
    SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception {
        http.authorizeExchange()
                .anyExchange()
                .permitAll();
        // 一些配置
        http.csrf().disable()//必须支持跨域
                .httpBasic().disable()
                .logout().disable()
                .formLogin().disable();
        return http.build();
    }

    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new HiddenHttpMethodFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
                return chain.filter(exchange);
            }
        };
    }
}

注意:gateway网关项目和普通springboot项目关闭security验证的方式不同。原因是gateway采用的webflux技术,不是servlet。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值