Skip to content

Commit

Permalink
refactor : JwtAuthorizationFilter 다양한 가독성 개선 (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-ho committed Mar 11, 2024
1 parent e17b1f9 commit 6b75530
Showing 1 changed file with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gdsc.binaryho.imhere.security.filter;

import gdsc.binaryho.imhere.core.auth.exception.MemberNotFoundException;
import gdsc.binaryho.imhere.core.member.Member;
import gdsc.binaryho.imhere.core.member.infrastructure.MemberRepository;
import gdsc.binaryho.imhere.security.jwt.TokenService;
import gdsc.binaryho.imhere.security.principal.PrincipalDetails;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -18,7 +20,7 @@

public class JwtAuthorizationFilter extends BasicAuthenticationFilter {

private static final String HEADER_STRING = HttpHeaders.AUTHORIZATION;
private static final String TOKEN_HEADER_STRING = HttpHeaders.AUTHORIZATION;
private static final String ACCESS_TOKEN_PREFIX = "Token ";

private final TokenService tokenService;
Expand All @@ -35,34 +37,33 @@ public JwtAuthorizationFilter(
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
if (isNullToken(request)) {
String jwtToken = request.getHeader(TOKEN_HEADER_STRING);
if (isTokenNullOrInvalidate(jwtToken)) {
chain.doFilter(request, response);
return;
}

String jwtToken = request.getHeader(HEADER_STRING)
.replace(ACCESS_TOKEN_PREFIX, "");

if (tokenService.validateTokenExpirationTimeNotExpired(jwtToken)) {

String univId = tokenService.getUnivId(jwtToken);
Member member = memberRepository.findByUnivId(univId).orElseThrow();
PrincipalDetails principalDetails = new PrincipalDetails(member);

Authentication authentication =
new UsernamePasswordAuthenticationToken(principalDetails, "", principalDetails.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);
String tokenValue = jwtToken.replace(ACCESS_TOKEN_PREFIX, "");
if (tokenService.validateTokenExpirationTimeNotExpired(tokenValue)) {
setAuthentication(tokenValue);
}

chain.doFilter(request, response);
}

private boolean isNullToken(HttpServletRequest request) {
String jwtHeader = request.getHeader(HEADER_STRING);
if (jwtHeader == null || !jwtHeader.startsWith(ACCESS_TOKEN_PREFIX)) {
return true;
}
return false;
private boolean isTokenNullOrInvalidate(String token) {
return Objects.isNull(token)
|| (!token.startsWith(ACCESS_TOKEN_PREFIX));
}

private void setAuthentication(String jwtToken) {
String univId = tokenService.getUnivId(jwtToken);
Member member = memberRepository.findByUnivId(univId)
.orElseThrow(() -> MemberNotFoundException.EXCEPTION);

PrincipalDetails principalDetails = new PrincipalDetails(member);
Authentication authentication =
new UsernamePasswordAuthenticationToken(principalDetails, "", principalDetails.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);
}
}

0 comments on commit 6b75530

Please sign in to comment.