【问题标题】:Spring cache try and cache userSpring缓存尝试和缓存用户
【发布时间】:2014-11-07 23:24:06
【问题描述】:

我正在尝试缓存 UserDetails loadUserByUsername(String username) 问题是缓存后的结果是正确的用户,但是 密码始终设置为空,但缓存时不为空

@Service

公共类 MyUserDetailsS​​ervice 实现 UserDetailsS​​ervice {

@Autowired
UserRepository userRepository;

@Cacheable(value="usersLogged" ,key="#username" ,unless="#result.password==null")
@Override
public org.springframework.security.core.userdetails.User loadUserByUsername(
        String username) throws UsernameNotFoundException {

    try {
        // User user = userRepository.getUserByEmail(username); Switch to id
        // token base
        User user = userRepository.findOne(username);
        if (user == null) {

            throw new UsernameNotFoundException(
                    "Invalid username/password.");
        }

        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = user.isActive();

        String userN = user.getId(); // the suer is in the system

        String pass = user.getPassword();

        Collection<? extends GrantedAuthority> authorities = AuthorityUtils
                .createAuthorityList(user.getRole().toString());

        org.springframework.security.core.userdetails.User userBuild = new org.springframework.security.core.userdetails.User(
                userN, pass, user.isEnabled(), accountNonExpired,
                credentialsNonExpired, accountNonLocked, authorities);
        return userBuild;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
        // throw new
        // UsernameNotFoundException("Invalid username/password.");
    }
}

}

【问题讨论】:

    标签: spring caching


    【解决方案1】:

    似乎弹簧缓存在公共可见性时缓存有问题 密码受保护 在手册中 使用代理时,您应该仅将缓存注释应用于具有公共可见性的方法。如果您使用这些注释对受保护的、私有的或包可见的方法进行注释,则不会引发错误,但带注释的方法不会显示配置的缓存设置。如果您需要在更改字节码本身时注释非公共方法,请考虑使用 AspectJ(见下文)

    【讨论】:

    • 有谁知道我通过它的方法吗?
    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多