-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 로그인 시 jwt 발급 api 구현 - swagger 토큰 기능 추가 - security jwt 설정
- Loading branch information
Showing
7 changed files
with
218 additions
and
5 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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/j9/bestmoments/auth/jwt/JwtAuthenticationFilter.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,40 @@ | ||
package com.j9.bestmoments.auth.jwt; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
|
||
@RequiredArgsConstructor | ||
public class JwtAuthenticationFilter extends GenericFilterBean { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
String token = resolveToken((HttpServletRequest) request); | ||
|
||
if (token != null && jwtTokenProvider.validateToken(token)) { | ||
Authentication authentication = jwtTokenProvider.getAuthentication(token); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
chain.doFilter(request, response); | ||
} | ||
|
||
private String resolveToken(HttpServletRequest request) { | ||
String bearerToken = request.getHeader("Authorization"); | ||
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer")) { | ||
return bearerToken.substring(7); | ||
} | ||
return null; | ||
} | ||
|
||
|
||
} |
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,9 @@ | ||
package com.j9.bestmoments.auth.jwt; | ||
|
||
public record JwtToken( | ||
String grantType, | ||
String accessToken, | ||
String refreshToken | ||
) { | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/com/j9/bestmoments/auth/jwt/JwtTokenProvider.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,106 @@ | ||
package com.j9.bestmoments.auth.jwt; | ||
|
||
import com.j9.bestmoments.member.Member; | ||
import com.sun.security.auth.UserPrincipal; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.MalformedJwtException; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.UnsupportedJwtException; | ||
import io.jsonwebtoken.io.Decoders; | ||
import io.jsonwebtoken.security.Keys; | ||
import java.security.Key; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
public class JwtTokenProvider { | ||
|
||
private final Key key; | ||
|
||
@Value("${jwt.accessTokenExpirationMs}") | ||
private long accessTokenExpirationMs; | ||
|
||
@Value("${jwt.refreshTokenExpirationMs}") | ||
private long refreshTokenExpirationMs; | ||
|
||
// secret 값을 암호화 (SHA 키 생성) | ||
public JwtTokenProvider(@Value("${jwt.secret}") String secretKey) { | ||
byte[] keyBytes = Decoders.BASE64.decode(secretKey); | ||
this.key = Keys.hmacShaKeyFor(keyBytes); | ||
} | ||
|
||
public JwtToken generateToken(Member member) { | ||
|
||
Date now = new Date(); | ||
Date accessTokenExpiresIn = new Date(now.getTime() + accessTokenExpirationMs); | ||
Date refreshTokenExpiresIn = new Date(now.getTime() + refreshTokenExpirationMs); | ||
|
||
// 액세스 토큰 생성 | ||
String accessToken = Jwts.builder() | ||
.claim("id", member.getId()) | ||
.claim("role", member.getRole()) | ||
.setIssuedAt(now) | ||
.setExpiration(accessTokenExpiresIn) | ||
.signWith(key, SignatureAlgorithm.HS256) | ||
.compact(); | ||
|
||
// 리프레쉬 토큰 생성 | ||
String refreshToken = Jwts.builder() | ||
.claim("id", member.getId()) | ||
.setIssuedAt(now) | ||
.setExpiration(refreshTokenExpiresIn) | ||
.signWith(key, SignatureAlgorithm.HS256) | ||
.compact(); | ||
|
||
return new JwtToken("Bearer", accessToken, refreshToken); | ||
} | ||
|
||
// 토큰을 복호화하여 인증 정보 추출 | ||
public Authentication getAuthentication(String accessToken) { | ||
Claims claims = Jwts.parserBuilder() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(accessToken) | ||
.getBody(); | ||
|
||
if (claims.get("id") == null || claims.get("role") == null) { | ||
throw new RuntimeException("권한 정보가 없는 토큰입니다."); | ||
} | ||
|
||
String id = claims.get("id").toString(); | ||
String role = claims.get("role").toString(); | ||
|
||
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role); | ||
return new UsernamePasswordAuthenticationToken(new UserPrincipal(id), "", Collections.singletonList(authority)); | ||
} | ||
|
||
// 토큰 정보 검증 | ||
public boolean validateToken(String token) { | ||
try { | ||
Jwts.parserBuilder() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(token); | ||
return true; | ||
} catch (SecurityException | MalformedJwtException e) { | ||
log.info("Invalid JWT Token", e); | ||
} catch (ExpiredJwtException e) { | ||
log.info("Expired JWT Token", e); | ||
} catch (UnsupportedJwtException e) { | ||
log.info("Unsupported JWT Token", e); | ||
} catch (IllegalArgumentException e) { | ||
log.info("JWT claims string is empty.", e); | ||
} | ||
return false; | ||
} | ||
|
||
} |
26 changes: 24 additions & 2 deletions
26
src/main/java/com/j9/bestmoments/config/SecurityConfig.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
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