框架模块说明 #03 登录验证

背景

在开始这个项目的时候网关服务还是zuul,不久后spring就发布了基于Spring WebFlux构建的以netty为底层服务器的网关服务,于是果断的换掉了zuul,拥抱这种这个响应式的网关。也学习了一些响应是编程的相关概念。

使用 WebFlux的原因

Spring Cloud Gateway 选择 WebFlux 的原因是:

  • 性能优越: WebFlux 的非阻塞模型更适合网关这种需要处理大量并发请求的场景。
  • 扩展性强: WebFlux 支持流式数据操作,方便处理请求过滤、修改响应等操作。
  • 兼容性好: 支持与非阻塞服务(如 Reactor、RxJava)和阻塞服务(如传统 Spring MVC 微服务)的无缝集成。

登录集成

考虑到登录是一个并发性很高的操作,他也要占用很多系统资源,对响应速度是有要求的,所以我决定将登录集成到Spring Cloud Gateway进行处理,Spring Security提供了很好的对响应式编程授权的支持,在设计上我们没有采用市面上一些成熟的验证模式,而是对Basic 认证进行一些扩展,下面是几个关于登录和授权的重要的接口和我们的实现部分进行介绍。

SecurityWebFilterChain
  • 作用: 定义 WebFlux 环境下的安全过滤器链,类似于 Servlet 环境中的 SecurityFilterChain
  • 核心方法:
    • 通过 @Bean 的方式配置安全规则(如认证、授权)。
  • 重要注解:
  • @EnableWebFluxSecurity
package com.longyou.gateway.security;


import com.longyou.gateway.security.handler.AuthenticationFailHandler;
import com.longyou.gateway.security.handler.AuthenticationSuccessHandler;
import com.longyou.gateway.security.handler.CustomHttpBasicServerAuthenticationEntryPoint;
import com.longyou.gateway.security.handler.CustomServerLogoutSuccessHandler;
import com.longyou.gateway.util.MD5PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.server.SecurityWebFilterChain;

@EnableWebFluxSecurity
public class SecurityConfig {


    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;
    @Autowired
    private AuthenticationFailHandler authenticationFailHandler;
    @Autowired
    private CustomHttpBasicServerAuthenticationEntryPoint customHttpBasicServerAuthenticationEntryPoint;
    @Autowired
    private CustomServerLogoutSuccessHandler customServerLogoutSuccessHandler;


    @Bean
    SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception {
//        List<LoginTypeEnum> services = discoveryClient.getServices();
//        services.forEach((value)->{
//            excludedAuthPages.add("/"+value.toUpperCase()+"/**");
//        });
        http.authorizeExchange().pathMatchers("/**").permitAll()  //这里只做登录和生成token,最终的强制登录校验由core里的SecurityFilter进行校验
            .pathMatchers(HttpMethod.OPTIONS).permitAll() //option 请求默认放行
            .anyExchange().authenticated().and().httpBasic().and().formLogin().loginPage("/auth/login")
            .authenticationSuccessHandler(authenticationSuccessHandler) //认证成功
            .authenticationFailureHandler(authenticationFailHandler) //登陆验证失败
            .and().exceptionHandling().authenticationEntryPoint(customHttpBasicServerAuthenticationEntryPoint)  //基于http的接口请求鉴权失败
            .and().csrf().disable()//必须支持跨域
            .logout().logoutSuccessHandler(customServerLogoutSuccessHandler).logoutUrl("/auth/logout");

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new MD5PasswordEncoder(); //默认
    }


}
ReactiveUserDetailsService

WebFilterChainServerAuthenticationSuccessHandler

总结:

以上只是简单的介绍了框架的授权体系其实总体来说就是对spring security的实现,具体实现可以根据上文的代码链接查看,接下来的文章中会对权限管理、用户后台菜单的查询和前台权限标签的使用进行介绍。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值