背景
在开始这个项目的时候网关服务还是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
- 作用: 提供用户详情信息的服务接口。
- 在这个类中会根据url进行登录校验,如果是auth/login我们就会进行用户名的获取成功后,框架会根据配置好的密码校验,如果不是会根据token获取缓存中的数据,其中具体的实现部分是在UserLoginServiceImpl类中。
- GitCode - 全球开发者的开源社区,开源代码托管平台GitCode是面向全球开发者的开源社区,包括原创博客,开源代码托管,代码协作,项目管理等。与开发者社区互动,提升您的研发效率和质量。
https://gitcode.com/YouYouLongLong/springcloud-framework/blob/master/springcloud-tester/springcloud-gateway-tester/src/main/java/com/longyou/gateway/security/SecurityUserDetailsService.java
WebFilterChainServerAuthenticationSuccessHandler
总结:
以上只是简单的介绍了框架的授权体系其实总体来说就是对spring security的实现,具体实现可以根据上文的代码链接查看,接下来的文章中会对权限管理、用户后台菜单的查询和前台权限标签的使用进行介绍。
- 作用: 登录成功后的一些处理,将登录信息缓存到Redis和一些权限加载的处理。
- 源代码GitCode - 全球开发者的开源社区,开源代码托管平台GitCode是面向全球开发者的开源社区,包括原创博客,开源代码托管,代码协作,项目管理等。与开发者社区互动,提升您的研发效率和质量。
https://gitcode.com/YouYouLongLong/springcloud-framework/blob/master/springcloud-tester/springcloud-gateway-tester/src/main/java/com/longyou/gateway/security/handler/AuthenticationSuccessHandler.java