SpringSecurityOAuth2 redis deserialization failure problem

It can be used when token acquisition and token resource authentication are performed at the same time in the authentication service. Then the resource service is integrated into other resources, and at the same time, UserDetails is customized and the interface method is rewritten. When the resource server authenticates the token, there will be a problem of redis serialization failure, for example:


/**
 * @Description 自定义实现UserDetails
 * @Author wwz
 * @Date 2019/07/28
 * @Param
 * @Return
 */
@Data
public class MyUserDetails implements UserDetails {
    private AuthUser user;

    private Collection<? extends GrantedAuthority> authorities;

    public MyUserDetails(AuthUser user, Collection<? extends GrantedAuthority> authorities) {
        this.user = user;
        this.authorities = authorities;
    }

    public MyUserDetails() {
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
	其他省略......
/**
 * @Description 自定义用户验证数据
 * @Author wwz
 * @Date 2019/07/28
 * @Param
 * @Return
 */

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private AuthUserMapper authUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 自定义用户权限数据
        AuthUser authUser = authUserMapper.selectByUsername(username);
        if (authUser == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        if (!authUser.getValid()) {
            throw new UsernameNotFoundException("用户不可用");
        }
        Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
 // 中间逻辑省略。。。。。。
        MyUserDetails userDetails = new MyUserDetails(authUser, grantedAuthorities);
        return userDetails;
    }
}
org.springframework.data.redis.serializer.SerializationException

The reason for the problem is that when the token is generated, the custom UserDetails is used to serialize the token. When deserializing, the custom implementation cannot be found in the new project, and the default implementation can only be used. The solution is to copy the UserDetails of the custom implementation to the resource project.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324093704&siteId=291194637