-
Notifications
You must be signed in to change notification settings - Fork 1
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
refactor: JWT 에러 응답 리팩토링 및 인가 인증 예최 처리 코드 수정 #103
Changes from 4 commits
ad2bb35
4b3b953
73d6c2a
72bd335
f3b41ff
69702a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,37 @@ | ||
package net.teumteum.core.security.filter; | ||
|
||
import jakarta.servlet.ServletException; | ||
import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.teumteum.core.error.ErrorResponse; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; | ||
|
||
@Slf4j | ||
@Component | ||
public class JwtAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, | ||
HttpServletResponse response, | ||
AccessDeniedException accessDeniedException | ||
) throws IOException, ServletException { | ||
HttpServletResponse response, | ||
AccessDeniedException accessDeniedException | ||
) throws IOException { | ||
this.sendUnAuthorizedError(response, accessDeniedException); | ||
} | ||
|
||
private void sendUnAuthorizedError(HttpServletResponse response, | ||
Exception exception) throws IOException { | ||
Exception exception) throws IOException { | ||
response.setStatus(SC_FORBIDDEN); | ||
OutputStream os = response.getOutputStream(); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ObjectMapper 재사용 해주세요! ObjectMapper는 Thread safe하고, 비싼 객체라서 생성에 드는 비용이 큽니다. @Autowired
private final ObjectMapper objectMapper; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오호 굳굳 ~! |
||
log.error("Responding with unauthorized error. Message - {}", exception.getMessage()); | ||
response.sendError(SC_FORBIDDEN, exception.getMessage()); | ||
objectMapper.writeValue(os, ErrorResponse.of("인가 과정에서 오류가 발생했습니다.")); | ||
os.flush(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,10 @@ spring.security.oauth2.client.provider.kakao.user-name-attribute=https://kauth.k | |
spring.cloud.aws.credentials.access-key=12345678910 | ||
spring.cloud.aws.credentials.secret-key=12345678910 | ||
spring.cloud.aws.region.static=ap-northeast-2 | ||
spring.cloud.aws.s3.bucket: test-bucket | ||
spring.cloud.aws.s3.bucket=test-bucket | ||
### Redis ### | ||
spring.data.redis.host=localhost | ||
spring.data.redis.port=6378 | ||
### JWT ### | ||
jwt.bearer=Bearer | ||
jwt.secret=secret | ||
jwt.secret=a2FyaW10b2thcmltdG9rYXJpbXRva2FyaW10b2thcmltdG9rYXJpbXRva2FyaW10b2thcmltdG9rYXJpbXRva2FyaW10b2thcmltdG9rYXJpbXRvsdsadwsadasdwSDSAweasDSadwXJsecretsecretsecretsecretsecreetsecret | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓❓❓❓ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jwt 해싱 알고리즘때문에 512 바이트 문자열을 지정했습니다! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional 을 통해 값을 넘기거나 담은 코드는 지양하는게 좋을 것 같아요.
https://mangkyu.tistory.com/203