-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 로그인 성공 시 JWT 생성 및 front로 redirect 기능 추
- login 및 회원가입 후 로직 실행되는OAuth2AthenticationSuccessHandler 추가 - JwtProvider에 JWT생성 기능 추가
- Loading branch information
Showing
6 changed files
with
148 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.anywayclear.config.jwt; | ||
|
||
public interface JwtProperties { | ||
int EXPIRATION_TIME = 864000000; // 10일 (1/1000초) | ||
String TOKEN_PREFIX = "Bearer "; | ||
String HEADER_STRING = "Authorization"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.anywayclear.config.jwt; | ||
|
||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.security.Keys; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.security.Key; | ||
import java.util.Date; | ||
import io.jsonwebtoken.Jwts; | ||
|
||
import static com.anywayclear.config.jwt.JwtProperties.*; | ||
|
||
@Component | ||
public class JwtProvider { | ||
private static final Logger logger = LoggerFactory.getLogger(JwtProvider.class); | ||
private Key SECRET_KEY = Keys.secretKeyFor(SignatureAlgorithm.HS512); | ||
|
||
// 인증 정보를 기반으로 JWT 토큰 생성하는 메서드 | ||
public String createToken(Authentication authentication) { | ||
System.out.println(">>>>>>>>>>>> "); | ||
|
||
// 사용자 정보 가져오기 | ||
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal(); | ||
System.out.println("Provider ================== "); | ||
System.out.println("oAuth2User.getAttributes() = " + oAuth2User.getAttributes()); | ||
|
||
// 현재 시간과 토큰 만료 시간 설정 | ||
Date now = new Date(); | ||
Date expiryDate = new Date(now.getTime() + EXPIRATION_TIME); | ||
|
||
// JWT 토큰 생성 | ||
return Jwts.builder() | ||
.setSubject((String) oAuth2User.getAttributes().get("userId")) // userId를 토큰의 subject로 설정 | ||
.setIssuedAt(new Date()) // 토큰 발행 시간 설정 | ||
.setExpiration(expiryDate) // 토큰 만료 시간 설정 | ||
.signWith(SECRET_KEY, SignatureAlgorithm.HS512) // 토큰 서명에 사용할 비밀키 설정 | ||
.compact(); // 최종적으로 JWT 토큰 문자열로 변환 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/anywayclear/config/oauth/OAuth2AuthenticationSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.anywayclear.config.oauth; | ||
|
||
import com.anywayclear.config.jwt.JwtProperties; | ||
import com.anywayclear.config.jwt.JwtProvider; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component | ||
public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { | ||
|
||
@Autowired | ||
private JwtProvider jwtProvider; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { | ||
|
||
String jwt = jwtProvider.createToken(authentication); | ||
String redirectUrl = "http://localhost:3000/oauth2/redirect"; | ||
|
||
response.setHeader(JwtProperties.HEADER_STRING, JwtProperties.TOKEN_PREFIX + jwt); | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
|
||
if (response.isCommitted()) { | ||
logger.debug(redirectUrl + "로 리다이렉트 불가"); | ||
return; | ||
} | ||
|
||
getRedirectStrategy().sendRedirect(request, response, redirectUrl); | ||
|
||
} | ||
} |