JWT의 특성
JWT의 특성은 stateless, 즉 무상태성이다.
JWT는 HTTP 통신을 통해 주고받기 때문에 기본적으로 HTTP의 특성을 가지게 된다.
무상태성은 서버가 클라이언트의 상태를 가지고 있지 않는 것을 말한다.
인증 처리 리팩토링
리팩토링을 하게 된 계기는, 로그인한 유저가 로그인 인증 처리를 할 때, Database를 걸쳐 나오는 것 때문에, 이를 수정하고자 했다.
// 인증 처리
public void setAuthentication(String username) {
SecurityContext context = SecurityContextHolder.createEmptyContext();
Authentication authentication = createAuthentication(username);
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
}
// 인증 객체 생성
private Authentication createAuthentication(String username) {
UserDetails userDetails = new UserDetailsImpl(username);
return new UsernamePasswordAuthenticationToken(userDetails, null, null);
}
인증 객체를 생성할 때, loadUserByUsername을 통해서 UserDetails를 만들어 객체를 생성하면, 필터를 통해 모든 API 요청시 계속 DB를 건드리게 되었다. 건드리지 않기 위해, username만을 가지는 User를 생성하도록 하여 인증 객체를 생성하였다.
더 좋은 방법이 있겠지만, 지금은 이렇게 구현하였다.
전공이 보안이다보니 Spring Security에 대해서 조금 더 깊게 파보고 싶어졌다. 추후에 파보고 포스팅해야지.
'기타 > 개발일기' 카테고리의 다른 글
[Redis 캐싱 처리] Could not write JSON: Java 8 date/time type `java.time.LocalDateTime` not supported by default (0) | 2024.03.24 |
---|---|
[Spring Boot] Redis로 캐싱 처리 적용하기 (0) | 2024.03.24 |
Spring Boot에서 Redis 사용하기 (0) | 2024.03.15 |
Spring Boot S3에 이미지 업로드 (0) | 2024.03.13 |
[TIL] Jackson, Controller, DTO, JDBC (0) | 2024.01.18 |