用 spring security oauth2进行用户认证,如何更新Redis中的用户信息

本文介绍如何使用Spring Security OAuth2实现用户认证流程。详细解释了如何通过配置获取access_token和refresh_token,并展示了如何自定义认证过程及验证用户信息的具体实现。
摘要由CSDN通过智能技术生成

用 spring security oauth2进行用户认证,首次登录后得到access_token和refresh_token,然后修改SecurityContextHolder.getContext().getAuthentication()中的Principal信息,但没有修改Redis中的数据,

oauth2配置:

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.requestMatchers().antMatchers(HttpMethod.OPTIONS, "/oauth/token").and().csrf().disable();
}

认证:

@Component
public class PasswdAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider
{
    @Autowired
    private LoginUserDetailsService loginUserDetailsService;


    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException
    {
  
        String UserName = authentication.getName();
        String Password = (String) authentication.getCredentials();

    }

    @Override
    protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException
    {
        UserDetails userDetails = null;
        String UserName = authentication.getName();
        String Password = (String) authentication.getCredentials();

        userDetails = (UserDetails) loginUserDetailsService.loadUserByUsername(UserName, Password);
        if (userDetails == null)
        {
            throw new UsernameNotFoundException("用户: " + username + " 验证失败.");
        }
        return userDetails;


    }
}

获取UserDetails:

public static SecuserEntity getUserInfo() {
    SecuserEntity secuserentity = null;
    try {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication == null ? null : authentication.getPrincipal();
        if (principal instanceof SecuserEntity) {
            secuserentity = (SecuserEntity) principal;
        }
    } catch (Exception e) {
    }
    return secuserentity;
}


                
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值