Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat Security Oauth2.0 Pull Request #8

Merged
merged 19 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Access Token 검증 구현 (#7)
  • Loading branch information
toychip committed Nov 29, 2023
commit 8e53e0521b06fd2c8f2a1c14f9a84a8b9061577e
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

@Component
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {

private final JwtProvider jwtProvider;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
Expand All @@ -23,11 +29,11 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

if (authorizationHeader != null) {
// ToDo Access Token 검증
// jwtProvider.isValidToken(authorizationHeader);

filterChain.doFilter(request, response);
jwtProvider.isValidToken(authorizationHeader);
}

filterChain.doFilter(request, response);

}

private boolean isPublicUri(String requestURI) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/api/TaveShot/global/jwt/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.api.TaveShot.global.constant.OauthConstant.ACCESS_TOKEN_VALID_TIME;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
Expand Down Expand Up @@ -32,10 +33,12 @@ public String generateAccessToken(String id) {
.compact();
}

// JWT claims 생성
private Claims createClaims(String id) {
return Jwts.claims().setSubject(id);
}

// JWT 만료 시간 계산
private long calculateExpirationDate(Date now) {
return now.getTime() + ACCESS_TOKEN_VALID_TIME;
}
Expand All @@ -44,4 +47,19 @@ private SecretKey generateKey() {
return Keys.hmacShaKeyFor(SECRET_KEY.getBytes(StandardCharsets.UTF_8));
}

// 토큰의 유효성 검사
public void isValidToken(String token) {
try {
SecretKey key = generateKey();
Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token);

} catch (ExpiredJwtException e) { // 어세스 토큰 만료
throw new IllegalArgumentException("Access Token expired");
} catch (Exception e) {
throw new IllegalArgumentException("User Not Authorized");
}
}
}