https://ksh-coding.tistory.com/59
Spring Security + JWT를 이용한 자체 Login & OAuth2 Login(구글, 네이버, 카카오) API 구현 (3) - JWT 관련 클래
이전에 JWT 정의를 살펴봤다면, 이번에는 JWT 관련 클래스를 직접 생성하여 구현해보려고 합니다! 들어가기 전 JWT 패키지 구조는 다음과 같습니다. JWT 서비스를 생성하기 위해 다음과 같은 오픈
ksh-coding.tistory.com
위 tistory 블로그를 참고해서 jwt와 OAuth2를 구현할 예정이다.
우리 프로젝트는 일반 로그인은 지원하지 않을 예정이기 때문에 일반 로그인 부분을 덜어낼 필요가 있다. 그리고 isPresentOrElse <- 이 함수가 java8에는 존재하지 않아서 대체하는 방법을 찾는데 살짝 애를 먹었다. 그리고
//JwtAuthenticationProcessingFilter 클래스
public void saveAuthentication(User myUser) {
String password = myUser.getPassword();
if (password == null) { // 소셜 로그인 유저의 비밀번호 임의로 설정 하여 소셜 로그인 유저도 인증 되도록 설정
password = PasswordUtil.generateRandomPassword();
}
UserDetails userDetailsUser = org.springframework.security.core.userdetails.User.builder()
.username(myUser.getEmail())
.password(password)
.roles(myUser.getRole().name())
.build();
Authentication authentication =
new UsernamePasswordAuthenticationToken(userDetailsUser, null,
authoritiesMapper.mapAuthorities(userDetailsUser.getAuthorities()));
SecurityContextHolder.getContext().setAuthentication(authentication);
}
UsernamePasswordAuthenticationToken에서 roles를 주지 않아도 되는지 모르겠다.
'ssafy 공통프로젝트' 카테고리의 다른 글
| react에서 axios interceptor를 사용해서 header에 token 삽입하기 (0) | 2023.08.06 |
|---|---|
| JWT에서 claim과 name을 추출하는 방법 (소셜로그인을 활용한 로그인 구현) (0) | 2023.08.06 |
| JWT 및 OAuth2 과정 (소셜로그인을 활용한 로그인 구현) (0) | 2023.08.06 |
| 컨벤션 (1) | 2023.07.26 |
| 프로젝트 환경 설정 (0) | 2023.07.25 |