@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/css/**", "/images/**", "/js/**", "/favicon.*", "/*/icon-*").permitAll() .requestMatchers("/").permitAll()
.anyRequest().authenticated())
.formLogin(form -> form.loginPage("/login").permitAll())
.userDetailsService(userDetailsService) // 커스텀 UserDetailService 설정;
return http.build();
}
@Service("userDetailsService")
@RequiredArgsConstructor
public class FormUserDetailsService implements UserDetailsService {
private final UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username (사용자가 입력한 username)) throws UsernameNotFoundException {
Account account = userRepository.findByUsername(username);
if (account == null) {
if (userRepository.countByUsername(username) == 0) {
throw new UsernameNotFoundException("No user found with username: " + username);
}
}
List<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority(account.getRoles())); // 권한 설정 ModelMapper mapper = new ModelMapper();
AccountDto accountDto = mapper.map(account, AccountDto.class);
}
return new AccountContext(accountDto, authorities);
// AccountContext 는 UserDetails 를 구현한 클래스로서 AccountDto 를 Wrapping 함
}
AuthenticationProvider안에 UserDetails가 포함되어있는 것으로
AuthenticationProvider를 구현하여 적용하면 UserDetails를 설정하지 않아도 된다
@Override
public boolean support(Class<?> authentication){
return authentication.isAssignablefrom(UsernamePasswordAuthenticationToken.class);
}
: 사용자가 클라이언트에서 서버로 인증요청을 할 때 아이디와 비빌번호를 입력해서 보내주는데
이때 인증객체 Authentication에서 인증 작업을 하고, Details 객체에는 사용자의 IP 주소와 세션 ID와 같은 정보를 가지고 있다. 이 Details에 저장되어있는 객체가 WebAuthenticationDetails 객체이다