上QQ阅读APP看书,第一时间看更新
The CalendarUserDetails object
We have successfully eliminated the need to manage both Spring Security users and our CalendarUser objects. However, it is still cumbersome for us to continually need to translate between the two objects. Instead, we will create a CalendarUserDetails object, which can be referred to as both UserDetails and CalendarUser. Update CalendarUserDetailsService to use CalendarUserDetails, as follows:
//src/main/java/com/packtpub/springsecurity/core/userdetails/
CalendarUserDetailsService.java
public UserDetails loadUserByUsername(String username) throws
UsernameNotFoundException {
...
return new CalendarUserDetails(user);
}
private final class CalendarUserDetails extends CalendarUser
implements UserDetails {
CalendarUserDetails(CalendarUser user) {
setId(user.getId());
setEmail(user.getEmail());
setFirstName(user.getFirstName());
setLastName(user.getLastName());
setPassword(user.getPassword());
}
public Collection<? extends GrantedAuthority>
getAuthorities() {
return CalendarUserAuthorityUtils.createAuthorities(this);
}
public String getUsername() {
return getEmail();
}
public boolean isAccountNonExpired() { return true; }
public boolean isAccountNonLocked() { return true; }
public boolean isCredentialsNonExpired() { return true; }
public boolean isEnabled() { return true; }
}
In the next section, we will see that our application can now refer to the principal authentication on the current CalendarUser object. However, Spring Security can continue to treat CalendarUserDetails as a UserDetails object.