网上貌似webflux这一套的SpringSecurity操作资料貌似很少。
自己研究了一波,记录下来做一点备忘,如果能帮到也在迷惑的人一点点,就更好了。
新项目是前后端分离的项目,前台vue,后端SpringCloud2.0,采用oauth2.0机制来获得用户,权限框架用的gateway。
一,前台登录
大概思路前台主要是配合项目中配置的clientId,clientSecret去第三方服务器拿授权码code,然后拿这个code去后端交互,后端根据code去第三方拿用户信息,由于第三方只保存用户信息,不管具体的业务权限,所以我会在本地保存一份用户副本,用来做权限关联。用户登录成功后,会把一些用户基本信息(脱敏)生成jwt返回给前端放到head中当Authorization,同时后端把一些相关联的菜单,权限等数据放到redis里做关联,为后面的权限控制做准备。
二,SpringSecurity的webflux应用
如果用过SpringSecurity,HttpSecurity应该是比较熟悉的,基于Web允许为特定的http请求配置安全性。
WebFlux中ServerHttpSecurity与HttpSecurity提供的相似的类似,但仅适用于WebFlux。默认情况下,它将应用于所有请求,但可以使用
securityMatcher(ServerWebExchangeMatcher)或其他类似方法进行限制。
项目比较特殊,就不能全展示了,大概写一写,开启Security如下:
@EnableWebFluxSecurity
public class MyExplicitSecurityConfiguration {
@Bean
SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception {
http.securityContextRepository(new NoOpServerSecurityContextAutoRepository(tokenProvider)).httpBasic().disable()
.formLogin().disable()
.csrf().disable()
.logout().disable();
http.addFilterAt(corsFilter(), SecurityWebFiltersOrder.CORS)
.authorizeExchange()
.matchers(EndpointRequest.to("health", "info"))
.permitAll()
.and()
.authorizeExchange()
.pathMatchers(HttpMethod.OPTIONS)
.permitAll()
.and()
.authorizeExchange()
.pathMatchers(HttpMethod.PUT)
.denyAll()
.and()
.authorizeExchange()
.pathMatchers(HttpMethod.DELETE)
.denyAll()
.and()
.authorizeExchange()
.pathMatchers(HttpMethod.HEAD)
.denyAll()
.and()
.authorizeExchange()
.pathMatchers(HttpMethod.PATCH)
.denyAll()
.and()
.authorizeExchange()
.pathMatchers(HttpMethod.TRACE)
.denyAll()
.and()
.authorizeExchange()
.pathMatchers(excludedAuthPages).permitAll()
.and()
.authorizeExchange()
.pathMatchers(authentica