-
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.
Browse files
Browse the repository at this point in the history
Feat #14 인증 인가 로직 구현
- Loading branch information
Showing
26 changed files
with
491 additions
and
25 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
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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/gachtaxi/global/auth/jwt/annotation/CurrentMemberId.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,19 @@ | ||
package com.gachtaxi.global.auth.jwt.annotation; | ||
|
||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PARAMETER) | ||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : id") | ||
public @interface CurrentMemberId { | ||
/* | ||
* AuthenticationPrincipal의 id 필드를 반환 | ||
* 즉, JwtUserDetails의 id 필드를 반환 | ||
* JwtUserDetails의 id는 Userid | ||
* */ | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/gachtaxi/global/auth/jwt/authentication/CustomAccessDeniedHandler.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,39 @@ | ||
package com.gachtaxi.global.auth.jwt.authentication; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gachtaxi.global.common.response.ApiResponse; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.JWT_TOKEN_FORBIDDEN; | ||
|
||
@Slf4j | ||
@Component | ||
public class CustomAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
private final static String LOG_FORMAT = "ExceptionClass: {}, Message: {}"; | ||
private final static String CONTENT_TYPE = "application/json"; | ||
private final static String CHAR_ENCODING = "UTF-8"; | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, org.springframework.security.access.AccessDeniedException accessDeniedException) throws IOException, ServletException { | ||
setResponse(response); | ||
log.error(LOG_FORMAT, accessDeniedException.getClass().getSimpleName(), accessDeniedException.getMessage()); | ||
} | ||
|
||
private void setResponse(HttpServletResponse response) throws IOException { | ||
response.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
response.setContentType(CONTENT_TYPE); | ||
response.setCharacterEncoding(CHAR_ENCODING); | ||
|
||
String body = new ObjectMapper().writeValueAsString(ApiResponse.response(HttpStatus.FORBIDDEN, JWT_TOKEN_FORBIDDEN.getMessage())); | ||
response.getWriter().write(body); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...main/java/com/gachtaxi/global/auth/jwt/authentication/CustomAuthenticationEntryPoint.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,47 @@ | ||
package com.gachtaxi.global.auth.jwt.authentication; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage; | ||
import com.gachtaxi.global.common.response.ApiResponse; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@Component | ||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
private final static String LOG_FORMAT = "ExceptionClass: {}, Message: {}"; | ||
private final static String JWT_ERROR = "jwtError"; | ||
private final static String CONTENT_TYPE = "application/json"; | ||
private final static String CHAR_ENCODING = "UTF-8"; | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
JwtErrorMessage jwtError = (JwtErrorMessage) request.getAttribute(JWT_ERROR); | ||
|
||
if (jwtError != null) { | ||
setResponse(response, jwtError.getMessage()); | ||
log.error(LOG_FORMAT, jwtError, jwtError.getMessage()); | ||
} else { | ||
setResponse(response, authException.getMessage()); | ||
log.error(LOG_FORMAT, authException.getClass().getSimpleName(), authException.getMessage()); | ||
} | ||
} | ||
|
||
private void setResponse(HttpServletResponse response, String message) throws IOException { | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
response.setContentType(CONTENT_TYPE); | ||
response.setCharacterEncoding(CHAR_ENCODING); | ||
|
||
String body = new ObjectMapper().writeValueAsString(ApiResponse.response(HttpStatus.UNAUTHORIZED, message)); | ||
response.getWriter().write(body); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gachtaxi/global/auth/jwt/exception/CookieNotFoundException.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,12 @@ | ||
package com.gachtaxi.global.auth.jwt.exception; | ||
|
||
import com.gachtaxi.global.common.exception.BaseException; | ||
|
||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.COOKIE_NOT_FOUND; | ||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
|
||
public class CookieNotFoundException extends BaseException { | ||
public CookieNotFoundException() { | ||
super(BAD_REQUEST, COOKIE_NOT_FOUND.getMessage()); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gachtaxi/global/auth/jwt/exception/RefreshTokenNotFoundException.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,12 @@ | ||
package com.gachtaxi.global.auth.jwt.exception; | ||
|
||
import com.gachtaxi.global.common.exception.BaseException; | ||
|
||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.REDIS_NOT_FOUND; | ||
import static org.springframework.http.HttpStatus.UNAUTHORIZED; | ||
|
||
public class RefreshTokenNotFoundException extends BaseException { | ||
public RefreshTokenNotFoundException() { | ||
super(UNAUTHORIZED, REDIS_NOT_FOUND.getMessage()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/gachtaxi/global/auth/jwt/exception/TokenExpiredException.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,13 @@ | ||
package com.gachtaxi.global.auth.jwt.exception; | ||
|
||
import com.gachtaxi.global.common.exception.BaseException; | ||
|
||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.JWT_TOKEN_EXPIRED; | ||
import static org.springframework.http.HttpStatus.UNAUTHORIZED; | ||
|
||
public class TokenExpiredException extends BaseException { | ||
public TokenExpiredException() { | ||
super(UNAUTHORIZED, JWT_TOKEN_EXPIRED.getMessage()); | ||
} | ||
} | ||
|
7 changes: 4 additions & 3 deletions
7
src/main/java/com/gachtaxi/global/auth/jwt/exception/TokenInvalidException.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,11 +1,12 @@ | ||
package com.gachtaxi.global.auth.jwt.exception; | ||
|
||
import com.gachtaxi.global.common.exception.BaseException; | ||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.JWT_TOKEN_UN_VALID; | ||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
|
||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.JWT_TOKEN_INVALID; | ||
import static org.springframework.http.HttpStatus.UNAUTHORIZED; | ||
|
||
public class TokenInvalidException extends BaseException { | ||
public TokenInvalidException() { | ||
super(BAD_REQUEST, JWT_TOKEN_UN_VALID.getMessage()); | ||
super(UNAUTHORIZED, JWT_TOKEN_INVALID.getMessage()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gachtaxi/global/auth/jwt/exception/UserEmailNotFoundException.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,12 @@ | ||
package com.gachtaxi.global.auth.jwt.exception; | ||
|
||
import com.gachtaxi.global.common.exception.BaseException; | ||
|
||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.USER_NOT_FOUND_EMAIL; | ||
import static org.springframework.http.HttpStatus.UNAUTHORIZED; | ||
|
||
public class UserEmailNotFoundException extends BaseException { | ||
public UserEmailNotFoundException() { | ||
super(UNAUTHORIZED, USER_NOT_FOUND_EMAIL.getMessage()); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/gachtaxi/global/auth/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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.gachtaxi.global.auth.jwt.filter; | ||
|
||
import com.gachtaxi.global.auth.jwt.user.JwtUserDetails; | ||
import com.gachtaxi.global.auth.jwt.util.JwtExtractor; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.JWT_TOKEN_EXPIRED; | ||
import static com.gachtaxi.global.auth.jwt.exception.JwtErrorMessage.JWT_TOKEN_NOT_FOUND; | ||
|
||
@RequiredArgsConstructor | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtExtractor jwtExtractor; | ||
|
||
private final static String JWT_ERROR = "jwtError"; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
Optional<String> token = jwtExtractor.extractJwtToken(request); | ||
|
||
if (token.isEmpty()) { | ||
request.setAttribute(JWT_ERROR, JWT_TOKEN_NOT_FOUND); | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
String accessToken = token.get(); | ||
|
||
if(jwtExtractor.isExpired(accessToken)){ | ||
request.setAttribute(JWT_ERROR, JWT_TOKEN_EXPIRED); | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
saveAuthentcation(accessToken); | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private void saveAuthentcation(String token) { | ||
Long id = jwtExtractor.getId(token); | ||
String email = jwtExtractor.getEmail(token); | ||
String role = jwtExtractor.getRole(token); | ||
|
||
UserDetails userDetails = JwtUserDetails.of(id, email, role); | ||
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
} |
Oops, something went wrong.