-
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.
Merge pull request #88 from Alpha-Damyo/develop
merge to main
- Loading branch information
Showing
19 changed files
with
240 additions
and
85 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 |
---|---|---|
|
@@ -6,8 +6,10 @@ | |
|
||
@Schema(description = "회원가입 시 요청으로 오는 DTO") | ||
public record SignUpRequest ( | ||
@Schema(description = "소셜 로그인 후 인증서버에서 받아오는 이메일", example = "[email protected]") | ||
@Email String email, | ||
@Schema(description = "소셜 로그인 후 발급 받은 토큰", example = "이거 지우고 소셜에서 받은 토큰 복붙하기") | ||
@Email String token, | ||
@Schema(description = "소셜 로그인 인증 제공자 (google, naver, kakao)", example = "kakao") | ||
String provider, | ||
@Schema(description = "사용자의 이름, 실명은 아니고 서비스에서 사용할 이름", example = "홍길동") | ||
@NotBlank String name, | ||
@Schema(description = "사용자의 성별", example = "남자") | ||
|
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/damyo/alpha/api/auth/exception/TokenException.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,9 @@ | ||
package com.damyo.alpha.api.auth.exception; | ||
|
||
import io.jsonwebtoken.JwtException; | ||
|
||
public class TokenException extends JwtException { | ||
public TokenException(String message) { | ||
super(message); | ||
} | ||
} |
33 changes: 24 additions & 9 deletions
33
src/main/java/com/damyo/alpha/api/auth/jwt/JwtAuthenticationEntryPoint.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 |
---|---|---|
@@ -1,28 +1,43 @@ | ||
package com.damyo.alpha.api.auth.jwt; | ||
|
||
import com.damyo.alpha.api.auth.exception.AuthErrorCode; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import net.minidev.json.JSONObject; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerExceptionResolver; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.damyo.alpha.api.auth.exception.AuthErrorCode.*; | ||
|
||
@Slf4j | ||
@Component | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
private final HandlerExceptionResolver resolver; | ||
|
||
public JwtAuthenticationEntryPoint(@Qualifier("handlerExceptionResolver") HandlerExceptionResolver resolver) { | ||
this.resolver = resolver; | ||
} | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
resolver.resolveException(request, response, null, (Exception) request.getAttribute("exception")); | ||
AuthErrorCode errorCode = (AuthErrorCode) request.getAttribute("exception"); | ||
if (errorCode.equals(EXPIRED_TOKEN)) { | ||
setResponse(response, EXPIRED_TOKEN); | ||
} else if (errorCode.equals(INVALID_TOKEN)) { | ||
setResponse(response, INVALID_TOKEN); | ||
} else { | ||
log.info("unknown error message: " + errorCode.getMessage()); | ||
setResponse(response, UNKNOWN_ERROR); | ||
} | ||
} | ||
|
||
private void setResponse(HttpServletResponse response, AuthErrorCode errorCode) throws IOException { | ||
response.setContentType("application/json;charset=UTF-8"); | ||
response.setStatus(errorCode.getHttpStatus().value()); | ||
|
||
JSONObject responseJson = new JSONObject(); | ||
responseJson.put("code", errorCode.getExceptionCode()); | ||
responseJson.put("message", errorCode.getMessage()); | ||
|
||
response.getWriter().print(responseJson); | ||
} | ||
} |
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
28 changes: 17 additions & 11 deletions
28
...api/auth/jwt/JwtAuthenticationFilter.java → ...h/jwt/filter/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 |
---|---|---|
@@ -1,48 +1,54 @@ | ||
package com.damyo.alpha.api.auth.jwt; | ||
package com.damyo.alpha.api.auth.jwt.filter; | ||
|
||
import com.damyo.alpha.api.auth.exception.AuthException; | ||
import com.damyo.alpha.api.auth.exception.TokenException; | ||
import com.damyo.alpha.api.auth.jwt.JwtProvider; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.MalformedJwtException; | ||
import io.jsonwebtoken.security.SecurityException; | ||
import io.jsonwebtoken.security.SignatureException; | ||
import io.jsonwebtoken.UnsupportedJwtException; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.web.access.ExceptionTranslationFilter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
import static com.damyo.alpha.api.auth.exception.AuthErrorCode.*; | ||
import static com.damyo.alpha.global.exception.error.CommonErrorCode.INTERNAL_SERVER_ERROR; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@Component | ||
@Slf4j | ||
@Component | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtProvider jwtProvider; | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
String token = jwtProvider.resolveToken(request); | ||
log.info(request.getRequestURI()); | ||
try { | ||
String email = jwtProvider.validateTokenAndGetEmail(token); | ||
Authentication authentication = jwtProvider.createAuthentication(email); | ||
String id = jwtProvider.validateTokenAndGetId(token); | ||
Authentication authentication = jwtProvider.createAuthentication(UUID.fromString(id)); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} catch (ExpiredJwtException e) { | ||
request.setAttribute("exception", new AuthException(EXPIRED_TOKEN)); | ||
request.setAttribute("exception", EXPIRED_TOKEN); | ||
} catch (SecurityException | MalformedJwtException | UnsupportedJwtException | IllegalArgumentException e) { | ||
request.setAttribute("exception", new AuthException(INVALID_TOKEN)); | ||
log.info(e.getMessage()); | ||
request.setAttribute("exception", INVALID_TOKEN); | ||
} catch (Exception e) { | ||
request.setAttribute("exception", UNKNOWN_ERROR); | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
Oops, something went wrong.