redis缓存主要对象问题

daupos2t  于 2021-06-08  发布在  Redis
关注(0)|答案(1)|浏览(398)

在使用redis时,我无法更改用户的属性,因为主体对象是缓存的。
我有一个服务,这是改变用户的属性。为此,我创建了customuserdetails类并实现了userdetails接口。customuserdetails类有2个字段;属性ID,当前属性ID( transient )。参见实施:

public class CustomUserDetails implements UserDetails {
    private Long propertyid;
    private Long currentPropertyid;

    public Long getPropertyid() {
        if(getCurrentPropertyid() != null){
            return getCurrentPropertyid();
        }
        return propertyid;
    }

    public void setPropertyid(Long propertyid) {
        this.propertyid = propertyid;
    }

    @Transient
    public Long getCurrentPropertyid() {
        return currentPropertyid;
    }

    public void setCurrentPropertyid(Long propertyid) {
        this.currentPropertyid = propertyid;
    }
}

服务实施:

@PutMapping(value="change/property")
public void changeProperty(Long propertyid) {
    CustomUserDetails user = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
    user.setCurrentPropertyid(propertyid);
}

因此,服务的基本目标是更改propertyid以查看其他属性的数据。这工作正常,但当我启用redis时,它就不工作了。redis如何缓存主体对象。我没有在这里添加我的redis实现,因为在我的redis实现中,我没有任何缓存userdetails对象的实现。无论如何,我已经注解掉了我在项目中实现的redis模板,但是redis没有被禁用,同样的问题。
有办法解决这个问题吗?

dluptydi

dluptydi1#

我发现redis通过oauth2accesstoken缓存主体对象。因此,我需要重写oauth2accesstoken。这是我最新的服务方法;

@Autowired
@Qualifier("customRedisTokenStore")
CustomRedisTokenStore mCustomRedisTokenStore;

@PutMapping(value="change/property")
public void changeProperty(Long propertyid) {
    CustomUserDetails user = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
    user.setCurrentPropertyid(propertyid);
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
        OAuth2AccessToken accessToken = mCustomRedisTokenStore.getAccessToken(oAuth2Authentication);
        mCustomRedisTokenStore.storeAccessToken(accessToken, oAuth2Authentication);
}

相关问题