spring oauth2实现社交登录返回token

介绍

上一章到的是app传了providerId和openId来实现登录,但这种登录一般是事先需要在数据库中存在数。如果用户一开始注册过社交登录,就无法实现。本章节就讲了另外一种方式,app传code过来,实现登录并自动注册,返回token。

实现功能

社交登录方式2,app传code实现登录并自动注册,返回token。

步骤
实现登录过程中接口类以及实现类
/**
 * @author lvhaibao
 * @description
 * @date 2019/1/8 0008 15:15
 */
public interface SocialAuthenticationFilterPostProcessor {

    void process(SocialAuthenticationFilter socialAuthenticationFilter);
}

/**
 * @author lvhaibao
 * @description
 * @date 2019/1/8 0008 15:15
 */
@Component
public class SocialAuthenticationFilterPostProcessorImpl implements SocialAuthenticationFilterPostProcessor {

    @Autowired
    private AuthenticationSuccessHandler myAuthenticationSuccessHandler;
    
    //登录成功之后,调用成功处理器
    @Override
    public void process(SocialAuthenticationFilter socialAuthenticationFilter) {
        socialAuthenticationFilter.setAuthenticationSuccessHandler(myAuthenticationSuccessHandler);
    }
}

在自定义处理社交登录中添加filter
/**
 * @author lvhaibao
 * @description 自定义生成自己的处理的URL
 * @date 2019/1/3 0003 10:27
 */
@Data
public class MySpringSocialConfigurer  extends SpringSocialConfigurer {

    private String filterProcessesUrl;

    private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;

    MySpringSocialConfigurer(String filterProcessesUrl) {
        this.filterProcessesUrl = filterProcessesUrl;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected <T> T postProcess(T object) {
        SocialAuthenticationFilter filter = (SocialAuthenticationFilter) super.postProcess(object);
        filter.setFilterProcessesUrl(filterProcessesUrl);
        //在自定义处理社交登录中添加filter
        if(socialAuthenticationFilterPostProcessor != null){
            socialAuthenticationFilterPostProcessor.process(filter);
        }

        return (T) filter;
    }
}

生成这个filter的bean
/**
 * @author lvhaibao
 * @description bean初始化之前和初始化之后都会经过这个
 * @date 2018/12/18 0018 11:07
 */
@Component
public class SpringSocialConfigurePostProcessor implements BeanPostProcessor {


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if(StringUtils.equals(beanName, "mySocialSecurityConfig")){
            System.out.println("找到mySocialSecurityConfig");
            MySpringSocialConfigurer configurer = (MySpringSocialConfigurer)bean;
            //app注册页
            configurer.signupUrl("/social/signUp");
            return configurer;
        }

        return bean;
    }
}
在社交登录中添加登录过程中的处理
**
 * @author lvhaibao
 * @description 关于社交的的配置
 * @date 2019/1/3 0003 10:21
 */
@Configuration
@EnableSocial
@Order(1)
public class SocialConfig extends SocialConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private SecurityProperties securityProperties;
    @Autowired(required = false)
    private ConnectionSignUp connectionSignUp;

    @Autowired(required = false)
    private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;


    /**
     * 创建UsersConnectionRepository
     *
     * @param connectionFactoryLocator connectionFactoryLocator
     * @return repository
     */
    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,
                connectionFactoryLocator, Encryptors.noOpText());
        //如果不为空
        if (connectionSignUp != null) {
            repository.setConnectionSignUp(connectionSignUp);
        }
        return repository;
    }


    /**
     * 生成自己的mySocialSecurityConfig的bean
     *
     * @return configurer
     */
    @Bean
    public SpringSocialConfigurer mySocialSecurityConfig() {
        String filterProcessesUrl = securityProperties.getSocial().getFilterProcessesUrl();
        //配置处理的url
        MySpringSocialConfigurer configurer = new MySpringSocialConfigurer(filterProcessesUrl);
        //配置注册页
        configurer.signupUrl("/defaultSignUp.html");
        //处理
        configurer.setSocialAuthenticationFilterPostProcessor(socialAuthenticationFilterPostProcessor);
        return configurer;
    }


    @Bean
    public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator) {
        return new ProviderSignInUtils(connectionFactoryLocator,
                getUsersConnectionRepository(connectionFactoryLocator)) {
        };
    }


}

测试

为了模拟app登录,首先需要获取code。首先需要在OAuth2AuthenticationService这个类中打断点。如图所示:
在这里插入图片描述

然后用debug运行项目。在登录页面中点击微信登录,然后关闭项目,页面中就出现如下请求:

在这里插入图片描述

继续重启项目,这次不需要debug调试。然后使用post请求如下请求:
在这里插入图片描述

项目源码

https://gitee.com/lvhaibao/spring-lhbauth/tree/f15183a593f64d6a9494f1800f9b8ce4498111ae/

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值