๋ฐ๋จ ๐ฆถ
์ธ์๋ฅผ RequestParam์ผ๋ก ๋ฐ์์ค๋ฉด์ PathVariable๋ก ์์ฒญํ๊ณ ์๋ ๋ฉ์ฒญํ ์ง์ ํ๋ค. ๋น์ฐํ Controller์์ ์๋ฌ๊ฐ ํฐ์ก๊ณ ExceptionHandler ์ค์์ Controller ๋จ์ ์๋ฌ๋ฅผ ํธ๋ค๋งํ๊ณ ์๋ ๋ก์ง์ด ์์ด์ Security ๊ธฐ๋ณธ ExceptionHandler์์ "๋จธ์ฌ? ์๋ฌ๋ฌ์ ?@.@ ๊ทธ๋ผ ๋ก๊ทธ์ธ ์ผ๋จ ํ ๋ฒ ํด๋ด~@.@" ๋ผ๋ฉฐ ๋ฆฌ๋ค์ด๋ ํธ ํด์ฃผ๋...
๋๋ ์ฒ์์๋
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String token = resolveToken(request);
if (StringUtils.hasText(token) && tokenProvider.validateToken(token)) {
Authentication authentication = tokenProvider.getAuthentication(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
filterChain.doFilter(request, response);
}
์ด ๋ถ๋ถ์์ Oauth์ ์ธ์ฆ๋ก์ง์ ๊ฑด๋๋ฐ์ง ๋ชปํ๋๊ฑด๊ฐ? ์์ ๋ค๋ฅธ ํจํค์ง์ธ๊ฐ? ํ๊ณ ์์๋๋ฐ ๊ทธ๊ฒ ์๋์๋ค. ๊ทธ๋ฅ ์ ์๋น์ด ์ ๊ฑธ๋ก ์ธ์ฆ ๋ค ๊ฑด๋๋ฐ๊ณ ๋์ ์๋ฌ๊ฐ ํฐ์ ธ๋ฒ๋ฆฌ๋ Redirect ์์ผ์ค ๊ฒ์ด์๋ค. (์ฌ๊ธฐ์ ํ 1~2์๊ฐ ์ฝ์งํจ)
๐ ์๋๋ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธ ์์ผ์ฃผ๋ ๊ฒฐ๊ณผ์ฐฝ
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
๊ผผ์๋ถ๋ฆฌ๊ธฐ
Spring Security Config์์
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(req -> {
// static Resource์ ๋ํด์๋ ๋ชจ๋ ํ๊ฐํจ
req.requestMatchers(
PathRequest.toStaticResources()
.atCommonLocations()
)
.permitAll()
.requestMatchers(it -> isJwtAuthenticatedRequest(it))
.permitAll()
...
private boolean isJwtAuthenticatedRequest(HttpServletRequest request) {
String token = jwtAuthorizationFilter.resolveToken(request); // Reuse if already have this method
if (StringUtils.hasText(token) && tokenHelper.validateToken(token)) {
return true;
}
return false;
}
token์ ์ธ์ฆํด๋ฒ๋ ค์ ๊ทธ๋ฅ ๊ฑด๋๋ฐ์ด๋ฒ๋ฆฌ๋ ๋ฐฉ๋ฒ์ผ๋ก ๊ตฌํํ๋ค. ๊ทผ๋ฐ ์ด๋๋ ์๋ฌ๊ฐ ๋๋ ๊ฒ์ด๋ค! ์์๋ ๊ธฐ์ ํ๋ฏ์ด ์ ์๋น์ด ์ธ์ฆ๋ก์ง์ ๋ฌธ์ ๊ฐ ์๋์๋ค...
์ ์์ ์ธ ์์ฒญ์ ๋ณด๋ด๊ธฐ
์ผ๋จ์
@GetMapping("/lol/{keyword}")
public List<RedisCachedGame> searchLol(
@RequestParam
String keyword) {
return searchService.cachedSearch(keyword);
}
์ ๋ถ๋ถ์
@GetMapping("/lol/{keyword}")
public List<RedisCachedGame> searchLol(
@PathVariable
String keyword) {
return searchService.cachedSearch(keyword);
}
์ด๋ ๊ฒ ๋ฐ๊ฟ์ฃผ๊ณ ๋์ ์ ์์ ์ผ๋ก ์๋ตํ๋ ๊ฒ์ ํ์ธํ๋ค.
๊ทผ๋ฐ ์ด๋ฌ๊ณ ๋๋ ์ ์๋น์ด ์ redirect ๋ก์ง ์์ฒด๋ฅผ ์์ ๊ณ ๊ทธ๋ฅ ์ธ์ฆ์ด ์๋์ด์ ์๋ชป๋ ์์ฒญ์ด๋ผ๋ ์๋ต์ ๋ณด๋ผ ๋ฐฉ๋ฒ์ ์๋? ๋ผ๋ ์๊ฐ์ด ๋ค์๋ค.
Redirect ๋์ ๋น์ธ๊ฐ ์ ๊ทผ ์๋ฌ์ฝ๋ ๋ฆฌํด
DefaultAuthenticationEntryPoint ํด๋์ค๋ฅผ ์์๋ฐ๋ ์ AuthenticationEntryPoint ํด๋์ค๋ฅผ ๋ง๋ค์ด์ Commence()
๋ฅผ ์ค๋ฒ๋ผ์ด๋ํด์ ์๋ฌ๋ฅผ ๋ฐํํ๋๋ก ๊ตฌํํ๋ ๊ฒ์ด๋ค.
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
}
}
๊ทธ๋ฆฌ๊ณ SpringSecurityConfig์ CustomEntryPoint๋ฅผ ๋ฑ๋กํด์ค๋ค.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.
...
/**
* Exception ๋ฐ์์ Redirect๋ฅผ ํ์ง ์๊ณ 401์ ๋ฐํ
*/
http.exceptionHandling(
exceptionHandlingConfigurer -> exceptionHandlingConfigurer.authenticationEntryPoint(new MyAuthenticationEntryPoint())
);
์ด์ ์๊น์ ๋์ผํ ์๋ฌ๋ฅผ ๋ฐ์์์ผ๋
์ ์์ ์ผ๋ก ์๋ฌ์๋ต์ด ์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
Reference
'Spring > Error & Review' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
QueryDSL Join Error ํด๊ฒฐ (0) | 2024.03.30 |
---|---|
[๊ฒ๋ฌธ์ฒ ] ๊ฐ๋ฐ Log (0) | 2024.03.23 |
ํตํฉ ํ ์คํธ์ ํ์์ฑ (0) | 2024.03.20 |
ํ์๊ฐ์ ๋ก์ง ์ญํ ๊ณผ ์ฑ ์์ ๋ง๊ฒ ๋ฆฌํฉํฐ๋ง (0) | 2024.03.19 |
Spring์์ JWT์ ์ข ๋ ์์ธํ ์์๋ณด์! (0) | 2024.02.24 |