第20章 OAuth2LoginAuthenticationWebFilter 之ReactiveAuthenticationManager认证授权管理器

在上一篇我们分析了如何把请求转换成 Authentication 认证信息对象。接下来,我们将分析ReactiveAuthenticationManager如何来认证授权,它内部的工作流程是如何的。

初始化ReativeAuthenticationManager

在 ServerHttpSecurity 类的内部类 OAuth2LoginSpec 的 configure() 方法内,OAuth2LoginAuthenticationWebFilter 初始化了 ReativeAuthenticationManager。如果我们没有指定 ReativeAuthenticationManager,就创建默认的;否则使用指定的。ReativeAuthenticationManager会依赖两个类:ReactiveOAuth2AccessTokenResponseClient(用来获取Access Token)、ReactiveOAuth2UserService(用来获取第三方用户信息的)。源码如下所示:

private ReactiveAuthenticationManager getAuthenticationManager() {
   
    if (this.authenticationManager == null) {
   
        this.authenticationManager = this.createDefault();
    }

    return this.authenticationManager;
}

private ReactiveAuthenticationManager createDefault() {
   
    ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> client = this.getAccessTokenResponseClient();
    OAuth2LoginReactiveAuthenticationManager oauth2Manager = new OAuth2LoginReactiveAuthenticationManager(client, this.getOauth2UserService());
    GrantedAuthoritiesMapper authoritiesMapper = (GrantedAuthoritiesMapper)ServerHttpSecurity.this.getBeanOrNull(GrantedAuthoritiesMapper.class);
    if (authoritiesMapper != null) {
   
        oauth2Manager.setAuthoritiesMapper(authoritiesMapper);
    }

    boolean oidcAuthenticationProviderEnabled = ClassUtils.isPresent("org.springframework.security.oauth2.jwt.JwtDecoder", this.getClass().getClassLoader());
    if (!oidcAuthenticationProviderEnabled) {
   
        return oauth2Manager;
    } else {
   
        OidcAuthorizationCodeReactiveAuthenticationManager oidc = new OidcAuthorizationCodeReactiveAuthenticationManager(client, this.getOidcUserService());
        ResolvableType type = ResolvableType.forClassWithGenerics(ReactiveJwtDecoderFactory.class, new Class[]{
   ClientRegistration.class});
        ReactiveJwtDecoderFactory<ClientRegistration> jwtDecoderFactory = (ReactiveJwtDecoderFactory)ServerHttpSecurity.this.getBeanOrNull(type);
        if (jwtDecoderFactory != null) {
   
            oidc.setJwtDecoderFactory(jwtDecoderFactory);
        }

        if (authoritiesMapper != null) {
   
            oidc.setAuthoritiesMapper(authoritiesMapper);
        }
				// 创建一组认证管理器,认证失败则由下一个进行认证;认证成功直接返回
        return new DelegatingReactiveAuthenticationManager(new ReactiveAuthenticationManager[]{
   oidc, oauth2Manager});
    }
}

初始化ReactiveOAuth2AccessTokenResponseClient

在初始化 ReactiveOAuth2AccessTokenResponseClient时,首先会从Spring的容器内查找是否有ReactiveOAuth2AccessTokenResponseClient 的实例,如果有,则使用找到的实例;否则,创建默认类 WebClientReactiveAuthorizationCodeTokenResponseClient。源码如下所示:

private ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> getAccessTokenResponseClient() {
   
    ResolvableType type = ResolvableType.forClassWithGenerics(ReactiveOAuth2AccessTokenResponseClient.class, new Class[]{
   OAuth2AuthorizationCodeGrantRequest.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OAuth2认证服务器和授权服务器是OAuth2协议中的两个重要组件。 认证服务器(Authorization Server)的主要作用是验证用户的身份和颁发访问令牌。当用户通过登录认证后,认证服务器会对用户进行身份验证,验证通过后会生成一个访问令牌(Access Token),并将其返回给客户端应用程序。认证服务器通常保存有用户的账号密码等敏感信息,并根据不同的授权模式(如授权码模式、密码模式等)进行验证,确保只有合法的用户能够获取到访问令牌。 授权服务器(Authorization Server)的主要作用是授予资源访问权限。当客户端应用程序希望获取资源时,需要向授权服务器发送授权请求,并提供之前获得的访问令牌。授权服务器验证访问令牌的有效性,并根据客户端应用程序的权限配置决定是否授予对资源的访问权限。如果授权成功,授权服务器会将请求的资源返回给客户端应用程序。 认证服务器和授权服务器之间通常是相互协作的关系。当用户进行登录认证时,认证服务器负责验证用户的身份,并在验证通过后生成访问令牌。然后,客户端应用程序将访问令牌传递给授权服务器,请求对资源的访问权限。授权服务器负责验证访问令牌的有效性,并根据权限配置决定是否授予访问权限。 总之,OAuth2认证服务器和授权服务器在OAuth2协议中起到了身份验证和授权的作用,保护了用户的敏感信息和资源的安全性。这两个服务器相互配合,实现了安全和灵活的用户认证和资源访问机制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值