Skip to content

Commit

Permalink
Merge branch 'develop' into feature/135
Browse files Browse the repository at this point in the history
  • Loading branch information
Hwang-Kyu-Cheol authored Jan 22, 2024
2 parents 0363e3d + d519496 commit d41902c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.List;
import kr.co.fastcampus.yanabada.common.jwt.filter.JwtAuthFilter;
import kr.co.fastcampus.yanabada.common.jwt.filter.JwtAuthenticationEntryPoint;
import kr.co.fastcampus.yanabada.common.jwt.filter.JwtExceptionFilter;
import kr.co.fastcampus.yanabada.common.security.oauth.Oauth2LoginFailureHandler;
import kr.co.fastcampus.yanabada.common.security.oauth.Oauth2LoginSuccessHandler;
Expand Down Expand Up @@ -36,9 +37,12 @@ public class SecurityConfig {
private final Oauth2UserService oauth2UserService;
private final Oauth2LoginSuccessHandler oauth2LoginSuccessHandler;
private final Oauth2LoginFailureHandler oauth2LoginFailureHandler;
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;


private static final String[] PERMIT_PATHS = {
"/auth", "/auth/**", "/oauth2/**"
"/auth", "/auth/**", "/login/**",
"/oauth2/**", "/signin/**", "/error/**"
};

private static final String[] PERMIT_PATHS_POST_METHOD = {
Expand Down Expand Up @@ -67,6 +71,10 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.anyRequest().authenticated()
);

http.exceptionHandling(exceptionHandling -> {
exceptionHandling.authenticationEntryPoint(jwtAuthenticationEntryPoint);
});

http.oauth2Login(oauth2 -> oauth2
.userInfoEndpoint(
userInfoEndpoint -> userInfoEndpoint.userService(oauth2UserService))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import kr.co.fastcampus.yanabada.common.exception.MemberNotFoundException;
import kr.co.fastcampus.yanabada.common.exception.TokenCannotBeEmptyException;
import kr.co.fastcampus.yanabada.common.exception.TokenExpiredException;
import kr.co.fastcampus.yanabada.common.exception.TokenNotExistAtCacheException;
import kr.co.fastcampus.yanabada.common.exception.TokenNotValidatedException;
import kr.co.fastcampus.yanabada.common.jwt.service.TokenService;
Expand All @@ -21,7 +19,6 @@
import kr.co.fastcampus.yanabada.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -38,13 +35,6 @@ public class JwtAuthFilter extends OncePerRequestFilter {
private final MemberRepository memberRepository;
private final TokenService tokenService;

@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
/* 토큰 로그인, 회원가입 경우 해당 필터 실행 안됨 */
return request.getRequestURI().contains("/sign-up")
|| request.getRequestURI().contains("/login");
}

@Override
protected void doFilterInternal(
HttpServletRequest request,
Expand All @@ -55,11 +45,8 @@ protected void doFilterInternal(
String token = extractTokenFromRequest(request);

if (!StringUtils.hasText(token)) {
throw new TokenCannotBeEmptyException();
}

if (!jwtProvider.verifyToken(token)) {
throw new TokenExpiredException();
filterChain.doFilter(request, response);
return;
}

String email = jwtProvider.getEmail(token);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package kr.co.fastcampus.yanabada.common.jwt.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import kr.co.fastcampus.yanabada.common.exception.JsonProcessFailedException;
import kr.co.fastcampus.yanabada.common.response.ResponseBody;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

private final ObjectMapper objectMapper;

@Override
public void commence(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException
) {
sendResponse(authException);
}

private void sendResponse(AuthenticationException authException) {
if (authException instanceof BadCredentialsException) {
throw new BadCredentialsException(authException.getMessage());
} else if (authException instanceof InternalAuthenticationServiceException) {
throw new InsufficientAuthenticationException(authException.getMessage());
} else if (authException instanceof InsufficientAuthenticationException) {
throw new InsufficientAuthenticationException(authException.getMessage());
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import kr.co.fastcampus.yanabada.common.exception.TokenCannotBeEmptyException;
import kr.co.fastcampus.yanabada.common.exception.TokenExpiredException;
import kr.co.fastcampus.yanabada.common.exception.TokenNotExistAtCacheException;
import kr.co.fastcampus.yanabada.common.jwt.dto.TokenExpiredResponse;
import kr.co.fastcampus.yanabada.common.response.ResponseBody;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

Expand All @@ -36,7 +36,7 @@ protected void doFilterInternal(

try {
filterChain.doFilter(request, response);
} catch (TokenCannotBeEmptyException e) {
} catch (AuthenticationException e) {
ResponseBody<Void> responseBody
= ResponseBody.fail(e.getMessage());
completeResponse(response, e, responseBody, UNAUTHORIZED.value());
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ spring:

login:
oauth2-password: oauth2-password
oauth2-redirect-url: /redirect_url
oauth2-redirect-url: /signin/3
root-url: http://localhost:8080

jwt:
Expand Down

0 comments on commit d41902c

Please sign in to comment.