argument resolver가 있다.
로그인한 사용자의 정보를 controller의 파라미터로 받고 싶을 때
Principal 객체로 받아서 사용했다.
또는 SecurityContextHolder에서 꺼내서 사용할 수가 있는데 이 둘을 다르다.
Principal객체는 제한적이다.
보통 Principal이 아닌 도메인의 유저 객체를 사용하고 싶다.
UserDetailsService 에서 리턴하는 객체를 바꾸면 바꿀 수 있다.
public class UserAccount extends User {
private Account account;
public UserAccount(Account account) {
super(
account.getUsername(),
account.getPassword(),
List.of(new SimpleGrantedAuthority("ROLE" + account.getRole()))
);
this.account = account;
}
public Account getAccount() {
return account;
}
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Account account = accountRepository.findByUsername(username);
if (account == null) {
throw new UsernameNotFoundException(username);
}
return new UserAccount(account);
}
User를 상속 받는 객체이거나 UserDetails를 구현해야 한다.
UserAccount는 Account와 Principal을 잇는 어댑터이다.
기존 Account에 User를 상속 시키기에는 엔티티가 더러워 진다..?