Skip to content

Commit

Permalink
[chore] log2
Browse files Browse the repository at this point in the history
  • Loading branch information
suhyeon7497 committed Nov 14, 2024
1 parent 4d9f279 commit 56af2aa
Showing 1 changed file with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
Expand All @@ -14,11 +19,7 @@
import team7.inplace.security.application.dto.CustomOAuth2User;
import team7.inplace.security.util.JwtUtil;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

@Slf4j
public class AuthorizationFilter extends OncePerRequestFilter {

private final JwtUtil jwtUtil;
Expand All @@ -29,10 +30,22 @@ public AuthorizationFilter(JwtUtil jwtUtil) {

@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
log.info("cookie exists");
Cookie accessToken = Arrays.stream(cookies)
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue()))
.findAny()
.orElse(null);
if (accessToken != null) {
log.info("Access token: {}", accessToken.getValue());
}
}
if (hasNoTokenCookie(request)) {
filterChain.doFilter(request, response);
return;
Expand All @@ -52,21 +65,21 @@ protected void doFilterInternal(

private boolean hasNoTokenCookie(HttpServletRequest request) {
return Optional.ofNullable(request.getCookies())
.flatMap(cookies -> Arrays.stream(cookies)
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue())
|| cookie.getName().equals(TokenType.REFRESH_TOKEN.getValue())
)
.findAny())
.isEmpty();
.flatMap(cookies -> Arrays.stream(cookies)
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue())
|| cookie.getName().equals(TokenType.REFRESH_TOKEN.getValue())
)
.findAny())
.isEmpty();
}

private String getAccessToken(HttpServletRequest request) throws InplaceException {
Cookie accessTokenCookie = Arrays.stream(request.getCookies())
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue()))
.findAny()
.orElse(null);
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue()))
.findAny()
.orElse(null);
if (Objects.isNull(accessTokenCookie)) {
return null;
}
Expand All @@ -75,10 +88,10 @@ private String getAccessToken(HttpServletRequest request) throws InplaceExceptio

private String getRefreshToken(HttpServletRequest request) throws InplaceException {
Cookie refreshTokenCookie = Arrays.stream(request.getCookies())
.filter(
cookie -> cookie.getName().equals(TokenType.REFRESH_TOKEN.getValue()))
.findAny()
.orElse(null);
.filter(
cookie -> cookie.getName().equals(TokenType.REFRESH_TOKEN.getValue()))
.findAny()
.orElse(null);
if (Objects.isNull(refreshTokenCookie)) {
return null;
}
Expand All @@ -91,7 +104,7 @@ private void addUserToAuthentication(String token) throws InplaceException {
String roles = jwtUtil.getRoles(token);
CustomOAuth2User customOAuth2User = new CustomOAuth2User(username, id, roles);
Authentication authToken = new OAuth2AuthenticationToken(customOAuth2User,
customOAuth2User.getAuthorities(), customOAuth2User.getRegistrationId());
customOAuth2User.getAuthorities(), customOAuth2User.getRegistrationId());
SecurityContextHolder.getContext().setAuthentication(authToken);
}

Expand Down

0 comments on commit 56af2aa

Please sign in to comment.