Spring Boot JWT Tutorial - 목차
01. JWT 소개, 프로젝트 생성 02. Security 설정, Data 설정 03. JWT 코드, Security 설정 추가 04. DTO, Repository, 로그인 05. 회원가입, 권한검증
01. JWT 소개, 프로젝트 생성 02. Security 설정, Data 설정 03. JWT 코드, Security 설정 추가 04. DTO, Repository, 로그인 05. 회원가입, 권한검증
회원가입 API 생성 간단한 유틸리티 메소드를 만들기 위해 SecurityUtil 클래스 생성 // main/java/me/gagyeong/tutorial/util/SecurityUtil.java public class SecurityUtil { // 간단한 유틸리티 메소드를 만들기 위한 클래스 private static final Logger logger = LoggerFactory.getLogger(SecurityUtil.class); private SecurityUtil() { } // SecurityContext의 Authentication 객체를 이용해 username을 리턴해주는 유틸성 메소드 // SecurityContext에 Authentication 객체가 저장되는 시점은 JwtFilte..
외부와의 통신에 사용할 DTO 클래스 생성 로그인 시 사용할 LoginDto 클래스 생성 // main/java/me/gagyeong/tutorial/dto/LoginDto.java @Getter // Lombok 어노테이션들 @Setter @Builder @AllArgsConstructor @NoArgsConstructor public class LoginDto { // 로그인 시 사용할 클래스 생성 @NotNull @Size(min = 3, max = 50) // @valid 관련 어노테이션 추가 private String username; @NotNull @Size(min = 3, max = 100) // @valid 관련 어노테이션 추가 private String password; } 토큰 정보를..
JWT 설정 추가 HS512 알고리즘을 사용하기 때문에 Secret Key는 64Byte 이상이 되도록 하며, 토큰 만료 시간은 86400초로 설정 (하루) // main/resources/application.yml jwt: header: Authorization # HS512 알고리즘을 사용할 것이기 때문에 512bit, 즉 64byte 이상의 secret key를 사용해야 한다. # 밑의 문자열을 base64로 인코딩한 값을 이용 # echo 'silvernine-tech-spring-boot-jwt-tutorial-secret-silvernine-tech-spring-boot-jwt-tutorial-secret'|base64 secret: c2lsdmVybmluZS10ZWNoLXNwcmluZy1i..
401 Unauthorized 해결을 위한 Security 설정 기본적인 Security 설정을 위한 SecurityConfig 클래스 생성 // main/java/me/gagyeong/tutorial/config/SecurityConfig.java @EnableWebSecurity // 기본적인 web 보안 활성을 위한 어노테이션 public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override // WebSecurityConfigurerAdapter의 configure 메소드를 오버라이드 protected void configure(HttpSecurity http) throws Exception { http .authorizeRe..
JWT 소개 JSON 객체를 사용해서 토큰 자체에 정보들을 저장하고 있는 Web Token Header, Payload, Signature 3개의 부분으로 구성 1) Header : Signature를 해싱하기 위한 알고리즘 정보들이 담겨 있음 2) Payload : 서버와 클라이언트가 주고받는, 시스템에서 실제로 사용될 정보에 대한 내용들이 담겨 있음 3) Signature : 서버에서 토큰의 유효성 검증을 위한 문자열 JWT는 중앙의 인증서버와 데이터 스토어에 대한 의존성이 없기 때문에 시스템 수평 확장에 유리한 장점을 가지며 Base64 URL Safe Encoding을 이용하기 때문에 URL, Cookie, Header 등 어디에서든 모두 사용 가능한 범용성을 가짐 Payload의 정보가 많아지면..