Spring的Gateway与Security在项目如何使用

网上貌似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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值