Spring Security Oauth2 扩展登录方式

本文介绍了如何使用Spring Security Oauth2扩展登录方式,以手机验证码登录为例,详细步骤包括新建filter、provider、重写token、修改UserDetailService以及调整WebSecurityConfig配置。
摘要由CSDN通过智能技术生成

第一步:新建filter,这里以手机验证码登录为例子

/**
 * @Author: 朱维
 * @Date 16:52 2019/11/27
 * /phoneLogin?telephone=13000000000&smsCode=1000
 */
public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    /**
     * 验证码登录请求参数:手机号码
     */
    private static final String SPRING_SECURITY_RESTFUL_PHONE_KEY = "telephone";
    /**
     * 验证码登录请求参数:短信验证码
     */
    private static final String SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY = "smsCode";
    /**
     * 验证码登录请求参数:登录地址
     */
    private static final String SPRING_SECURITY_RESTFUL_LOGIN_URL = "/phone-login";
    private boolean postOnly = true;

    public PhoneLoginAuthenticationFilter() {
        super(new AntPathRequestMatcher(SPRING_SECURITY_RESTFUL_LOGIN_URL, "POST"));
    }


    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }

        AbstractAuthenticationToken authRequest;
        String principal;
        String credentials;

        // 手机验证码登陆
        principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_PHONE_KEY);
        credentials = obtainParameter(request, SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY);

        principal = principal.trim();
        authRequest = new PhoneAuthenticationToken(principal, credentials);

        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }

    private void setDetails(HttpServletRequest request,
                            AbstractAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }

    private String obtainParameter(HttpServletRequest request, String parameter) {
        String result =  request.getParameter(parameter);
        return result == null ? "" : result;
    }

第二步新建provider

/**
 * 手机验证码登录
 * @Author: 朱维
 * @Date 16:26 2019/11/27
 */
public class PhoneAuthenticationProvider extends MyAbstractUserDetailsAuthenticationProvider {

    private UserDetailsService userDetailsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    protected void additionalAuthenticationChecks(UserDetails var1, Authentication authentication) throws AuthenticationException {

        if(authentication.getCredentials() == null) {
            this.logger.debug("
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值