【发布时间】:2014-11-07 23:24:06
【问题描述】:
我正在尝试缓存 UserDetails loadUserByUsername(String username) 问题是缓存后的结果是正确的用户,但是 密码始终设置为空,但缓存时不为空
@Service
公共类 MyUserDetailsService 实现 UserDetailsService {
@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.");
}
}
}
【问题讨论】: