Skip to content

Commit

Permalink
feat: JwtAccessDeniedHandler, JwtAuthenticationEntryPoint 구현 및 적용 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Aug 14, 2023
1 parent e5af44a commit d626cf4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/java/com/project/mapdagu/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.project.mapdagu.domain.oauth2.handler.OAuth2LoginFailureHandler;
import com.project.mapdagu.domain.oauth2.handler.OAuth2LoginSuccessHandler;
import com.project.mapdagu.domain.oauth2.service.CustomOAuth2UserService;
import com.project.mapdagu.jwt.JwtAccessDeniedHandler;
import com.project.mapdagu.jwt.JwtAuthenticationEntryPoint;
import com.project.mapdagu.jwt.filter.JwtAuthenticationProcessingFilter;
import com.project.mapdagu.jwt.service.JwtService;
import com.project.mapdagu.util.RedisUtil;
Expand Down Expand Up @@ -41,6 +43,8 @@ public class SecurityConfig {
private final MemberRepository memberRepository;
private final ObjectMapper objectMapper;
private final LoginService loginService;
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private final JwtAccessDeniedHandler jwtAccessDeniedHandler;
private final OAuth2LoginSuccessHandler oAuth2LoginSuccessHandler;
private final OAuth2LoginFailureHandler oAuth2LoginFailureHandler;
private final CustomOAuth2UserService customOauth2UserService;
Expand Down Expand Up @@ -76,7 +80,9 @@ public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospe
.failureHandler(oAuth2LoginFailureHandler)
.userInfoEndpoint(userInfoEndPoint -> userInfoEndPoint.userService(customOauth2UserService)))
.addFilterAfter(customJsonUsernamePasswordAuthenticationFilter(), LogoutFilter.class)
.addFilterBefore(jwtAuthenticationProcessingFilter(), CustomJsonAuthenticationFilter.class);
.addFilterBefore(jwtAuthenticationProcessingFilter(), CustomJsonAuthenticationFilter.class)
.exceptionHandling(exception -> exception.accessDeniedHandler(jwtAccessDeniedHandler)
.authenticationEntryPoint(jwtAuthenticationEntryPoint));

return http.build();
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/project/mapdagu/jwt/JwtAccessDeniedHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.project.mapdagu.jwt;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
* 필요한 권한이 존재하지 않는 경우에 403 Forbidden 에러를 리턴
*/
@Slf4j
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
//필요한 권한이 없이 접근하려 할때 403
log.info("허가 받지 않은 사용자의 접근입니다.");
response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.project.mapdagu.jwt;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
* 유효한 자격 증명을 제공하지 않고 접근하려 할 때, 401 UnAuthorized 에러를 리턴
*/
@Slf4j
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
log.info("인증되지 않은 요청입니다.");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}

0 comments on commit d626cf4

Please sign in to comment.