Spring Security OAuth2 第三方登录(一)之流程说明

本文详细介绍了SpringBoot第三方登录的流程,重点讲解了OAuth2AuthenticationService如何获取code和token,以及SocialConfigurerAdapter在配置中的作用。通过SpringSecurity的FilterChain,配置SocialAuthenticationFilter实现第三方登录验证。同时,文章还展示了如何配置ConnectionFactory和UsersConnectionRepository,以及使用QQServiceProvider作为示例,说明了QQOAuth2Template和QQImpl在OAuth2协议中的应用。
摘要由CSDN通过智能技术生成

SpringBoot第三方登录流程

在这里插入图片描述

OAuth2AuthenticationService

该类执行获取code,token,创建connection,由SocailAuthenticationFilter调用

public class OAuth2AuthenticationService<S> extends AbstractSocialAuthenticationService<S> {
   

public SocialAuthenticationToken getAuthToken(HttpServletRequest request, HttpServletResponse response) throws SocialAuthenticationRedirectException {
   
        String code = request.getParameter("code");
        if (!StringUtils.hasText(code)) {
   
            OAuth2Parameters params = new OAuth2Parameters();
            //拼接redirectUrl
            params.setRedirectUri(this.buildReturnToUrl(request));
            this.setScope(request, params);
            params.add("state", this.generateState(this.connectionFactory, request));
            this.addCustomParameters(params);
            throw new SocialAuthenticationRedirectException(this.getConnectionFactory().getOAuthOperations().buildAuthenticateUrl(params));
        } else if (StringUtils.hasText(code)) {
   
            try {
   
                String returnToUrl = this.buildReturnToUrl(request);
                //获取token,并返回AccessGrant
                AccessGrant accessGrant = this.getConnectionFactory().getOAuthOperations().exchangeForAccess(code, returnToUrl, (MultiValueMap)null);
                //创建connection
                Connection<S> connection = this.getConnectionFactory().createConnection(accessGrant);
                return new SocialAuthenticationToken(connection, (Map)null);
            } catch (RestClientException var7) {
   
                this.logger.debug("failed to exchange for access", var7);
                return null;
            }
        } else {
   
            return null;
        }
    }

}

SocialConfigurerAdapter

为第三方登录添加一些组件到容器,比如SpringSocialConfigurer(只是添加到容器中,需在WebSecurityConfigurerAdapter应用才可生效),JdbcUsersConnectionRepository等

  • 添加Filter
    SpringSecurity在添加验证时都是通过在其FilterChain上添加Filter来实现,第三方登录需要配置的是AutenticationFilter
    代码演示
@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {
   

    @Autowired
    DataSource dataSource;
    @Autowired
    SecurityProperties securityProperties;
    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
   
        return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
    }
    
    //配置将 SpringSocialConfigurer添加到容器中, 
    //SpringSocialConfigurer的构造方法会在FilterChain上添加AutenticationFilter
    //配置自定义SpringSocialConfigurer,需要在
    @Bean
    public SpringSocialConfigurer imoocSocialSecurityConfig
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值