SpringCloud Alibaba微服务实战十四 - SpringCloud Gateway集成Oauth2(1)

username: xxxxx

password: xxxxxxx

driver-class-name: com.mysql.jdbc.Driver

主要配置oauth2的数据库连接地址

自定义认证接口管理类

在webFlux环境下通过实现 ReactiveAuthenticationManager 接口 自定义认证接口管理,由于我们的token是存在jdbc中所以命名上就叫ReactiveJdbcAuthenticationManager

@Slf4j

public class ReactiveJdbcAuthenticationManager implements ReactiveAuthenticationManager {

private TokenStore tokenStore;

public JdbcAuthenticationManager(TokenStore tokenStore){

this.tokenStore = tokenStore;

}

@Override

public Mono authenticate(Authentication authentication) {

return Mono.justOrEmpty(authentication)

.filter(a -> a instanceof BearerTokenAuthenticationToken)

.cast(BearerTokenAuthenticationToken.class)

.map(BearerTokenAuthenticationToken::getToken)

.flatMap((accessToken ->{

log.info(“accessToken is :{}”,accessToken);

OAuth2AccessToken oAuth2AccessToken = this.tokenStore.readAccessToken(accessToken);

//根据access_token从数据库获取不到OAuth2AccessToken

if(oAuth2AccessToken == null){

return Mono.error(new InvalidTokenException(“invalid access token,please check”));

}else if(oAuth2AccessToken.isExpired()){

return Mono.error(new InvalidTokenException(“access token has expired,please reacquire token”));

}

OAuth2Authentication oAuth2Authentication =this.tokenStore.readAuthentication(accessToken);

if(oAuth2Authentication == null){

return Mono.error(new InvalidTokenException(“Access Token 无效!”));

}else {

return Mono.just(oAuth2Authentication);

}

})).cast(Authentication.class);

}

}

网关层的安全配置

@Configuration

public class SecurityConfig {

private static final String MAX_AGE = “18000L”;

@Autowired

private DataS

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值