SpringSecurity 免密登录方法

需求:做微信公众号扫码登录的时候发现,我们通过微信用户的openID获取后台用户的账号,但是密码是加密放到数据库且不能逆向解密,这时就应该跳过密码认证。

添加自定义校验 # MyAuthenticationProvider

package com.spark.security.config;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.spark.biz.admin.service.impl.SysLoginService.checkPwdThreadLocal;

/**
 * @author Jerry
 * @date 2024-01-26 18:15
 * 自定义密码验证
 */
@Component
public class MyAuthenticationProvider extends DaoAuthenticationProvider {
    
    private PasswordEncoder passwordEncoder;


    /**
     * 构造初始化
     * @param userDetailsService
     * @param passwordEncoder
     */
    public MyAuthenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
        super();
        // 这个地方一定要对userDetailsService赋值,不然userDetailsService是null (这个坑有点深)
        setUserDetailsService(userDetailsService);
        //passwordEncoder由于父类是private,这里需要自定义初始化后才能使用
        this.passwordEncoder = passwordEncoder;
    }




    /**
     * 重写该方法
     * @param userDetails
     * @param authentication
     * @throws AuthenticationException
     */
    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        if (authentication.getCredentials() == null) {
            this.logger.debug("Failed to authenticate since no credentials provided");
            throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        } else {
            // 线程变量
            Boolean checkPwd = checkPwdThreadLocal.get();
            checkPwdThreadLocal.remove();
            checkPwd = checkPwd == null || checkPwd;
            // 如果不需要密码直接跳过
            if (!checkPwd) {
                return;
            }
            String presentedPassword = authentication.getCredentials().toString();
            if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
                this.logger.debug("Authentication failed: password does not match stored value");
                throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
            }

        }
    }
}

 

 /**
     * 线程参数
     */
    public static ThreadLocal<Boolean> checkPwdThreadLocal = new ThreadLocal<>();

    public static void checkPwd(boolean checkPwd) {
        checkPwdThreadLocal.set(checkPwd);
    }

     public String autoLogin(String username, String password) {
        // 用户验证
        Authentication authentication;
        try {
            boolean check = false;
            checkPwd(check);
            authentication = authenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(username, password));
        } catch (Exception e) {
            if (e instanceof BadCredentialsException) {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
                throw new UserPasswordNotMatchException();
            } else {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
                throw new CustomException(e.getMessage());
            }
        }
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        // 生成token
        return tokenService.createToken(loginUser);
    }

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值