springcloud整合oauth2-----鉴权服务篇

鉴权服务

和授权服务,许多程序都是围绕着配置类进行的,所以我们直接看配置类

【1】AccessTokenConfig 令牌的一些配置

  • 和授权服务一致,因为令牌要从授权服务到网关,再到客户端,不管是为了现在的还是后续的操作,最好配置,当然,用不到也可以不配,看你用不用得到。

【2】JwtAuthenticationManager token认证管理器

/**
 * @author 公众号:码猿技术专栏
 * JWT认证管理器,主要的作用就是对携带过来的token进行校验,比如过期时间,加密方式等
 * 一旦token校验通过,则交给鉴权管理器进行鉴权
 */
@Override
public Mono<Authentication> authenticate(Authentication authentication) {

    System.out.println("第六步***来到JWT认证管理器 检验token");

    return Mono.justOrEmpty(authentication)
            .filter(a -> a instanceof BearerTokenAuthenticationToken)
            .cast(BearerTokenAuthenticationToken.class)
            .map(BearerTokenAuthenticationToken::getToken)
            .flatMap((accessToken -> {
                OAuth2AccessToken oAuth2AccessToken = this.tokenStore.readAccessToken(accessToken);
                //根据access_token从数据库获取不到OAuth2AccessToken
                if (oAuth2AccessToken == null) {
                    return Mono.error(new InvalidTokenException("无效的token!"));
                } else if (oAuth2AccessToken.isExpired()) {
                    return Mono.error(new InvalidTokenException("token已过期!"));
                }
                OAuth2Authentication oAuth2Authentication = this.tokenStore.readAuthentication(accessToken);
                if (oAuth2Authentication == null) {
                    return Mono.error(new InvalidTokenException("无效的token!"));
                } else {
                    return Mono.just(oAuth2Authentication);
                }
            })).cast(Authentication.class);
}

【3】JwtAccessManagerV2-------认证管理器自定义

  • 作用:认证管理的作用就是获取传递过来的令牌,对其进行解析、验签、过期时间判定。就是作为的鉴权
  • 获取调用方法【get/post…】+uri.getPath() 合成完整路径例如:【uri.getPath()】
  • 从redis里面获取获取所有的uri->角色对应关系
  • 去链接里面找到和自己这次申请链接完全一致的那一个键值对,而这个键值对的值恰恰就是【权限集合】
  • 把自己的权限先从mono中解析出来,然后匹配,如果超级管理员,放行;如果存在交集,则通过;否则失败

异常

  • RequestAuthenticationEntryPoint

    • 用于处理没有登录或token过期时的自定义返回结果
  • RequestAccessDeniedHandler

    • 自定义返回结果:没有权限访问时
  • RequestAuthenticationEntryPoint

    • 用于处理没有登录或token过期时的自定义返回结果

【4】 SecurityConfig 在webflux中使用security

1、webFluxSecurityFilterChain------把之前的配置整合链路

  • 这里使用的是webFluxSecurityFilterChain
  • http下的功能可以借鉴http://events.jianshu.io/p/8ad366b97e18
        SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception{
        //认证过滤器,放入认证管理器tokenAuthenticationManager
        AuthenticationWebFilter authenticationWebFilter = new AuthenticationWebFilter(tokenAuthenticationManager);
        System.out.println("认证过滤器,放入认证管理器tokenAuthenticationManager");
        authenticationWebFilter.setServerAuthenticationConverter(new ServerBearerTokenAuthenticationConverter());

        http
                .httpBasic().disable()
                .csrf().disable()
                .authorizeExchange()
                //白名单直接放行
                .pathMatchers(ArrayUtil.toArray(sysConfig.getIgnoreUrls(),String.class)).permitAll()
                //其他的请求必须鉴权,使用鉴权管理器
                .anyExchange().access(accessManager)
                //鉴权的异常处理,权限不足,token失效
                .and().exceptionHandling()
                .authenticationEntryPoint(requestAuthenticationEntryPoint)
                .accessDeniedHandler(requestAccessDeniedHandler)
                .and()
                // 跨域过滤器
                .addFilterAt(corsFilter, SecurityWebFiltersOrder.CORS)
                //token的认证过滤器,用于校验token和认证
                .addFilterAt(authenticationWebFilter, SecurityWebFiltersOrder.AUTHENTICATION);
        return http.build();
    }
一、加入了【2】的自定义令牌认证管理器
二、加入了【3】的鉴权管理器

**2、白名单放行 **

//白名单直接放行
.pathMatchers(ArrayUtil.toArray(sysConfig.getIgnoreUrls(),String.class)).permitAll()
//其他的请求必须鉴权,使用鉴权管理器
.anyExchange().access(accessManager)

3、鉴权的异常处理

//鉴权的异常处理,权限不足,token失效
.and().exceptionHandling()
.authenticationEntryPoint(requestAuthenticationEntryPoint)
.accessDeniedHandler(requestAccessDeniedHandler)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值