Skip to content

Commit

Permalink
feat: 인증 관련 코드 추가 #21
Browse files Browse the repository at this point in the history
JwtAuthorizationFilter, ApiAuthenticationEntryPoint
  • Loading branch information
heokyeongju committed Nov 8, 2022
1 parent 6be0bfd commit 6d88da2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.mutbook.week4_mission.app.security.filter;

import com.mutbook.week4_mission.app.base.rq.Rq;
import com.mutbook.week4_mission.app.domain.member.entity.Member;
import com.mutbook.week4_mission.app.domain.member.service.MemberService;
import com.mutbook.week4_mission.app.security.jwt.JwtProvider;
import com.mutbook.week4_mission.app.security.dto.MemberContext;
import groovy.util.logging.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class JwtAuthorizationFilter extends OncePerRequestFilter {
private final JwtProvider jwtProvider;
private final MemberService memberService;
private final Rq rq;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String bearerToken = request.getHeader("Authorization");

if (bearerToken != null) {
String token = bearerToken.substring("Bearer ".length());

// 1차 체크(정보가 변조되지 않았는지 체크)
if (jwtProvider.verify(token)) {
Map<String, Object> claims = jwtProvider.getClaims(token);
Member member = memberService.findByUsername((String) claims.get("username")).get();

// 2차 체크(화이트리스트에 포함되는지)
if (memberService.verifyWithWhiteList(member, token)) {
forceAuthentication(member);
}
}
}
filterChain.doFilter(request, response);
}

private void forceAuthentication(Member member) {
MemberContext memberContext = new MemberContext(member, member.genAuthorities());

UsernamePasswordAuthenticationToken authentication =
UsernamePasswordAuthenticationToken.authenticated(
memberContext,
null,
member.genAuthorities()
);

SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mutbook.week4_mission.app.security.handler;

import com.mutbook.week4_mission.app.base.dto.RsData;
import com.mutbook.week4_mission.util.Util;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@Component
public class ApiAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException{
RsData rs = RsData.of("F-AccessDenied","인증실패",null);
response.setCharacterEncoding("UTF-8");
response.setContentType(APPLICATION_JSON_VALUE);
response.setStatus(403);
response.getWriter().append(Util.json.toStr(rs));
}
}

0 comments on commit 6d88da2

Please sign in to comment.