Junit Test Application-19-jwt 필터 등록

jaegeunsong97·2023년 8월 4일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

저번에 JwtAuthenticationFilter를 만들었으니까 등록을 해야한다.

  • SecurityConfig 추가
@Configuration
public class SecurityConfig {

     private final Logger log = LoggerFactory.getLogger(getClass()); // @slf4j -> JUnit 테스트 문제 발생

     @Bean // IOC에 BCryptPasswordEncoder 객체 등록, @Configuration 붙어있는 곳만
     public BCryptPasswordEncoder bCryptPasswordEncoder() {
          log.debug("디버그 : BCryptPasswordEncoder 빈 등록됨");
          return new BCryptPasswordEncoder();
     }

     // JWT 등록
     public class CustomSecurityFilterManager
               extends AbstractHttpConfigurer<CustomSecurityFilterManager, HttpSecurity> {

          @Override
          public void configure(HttpSecurity builder) throws Exception {
               AuthenticationManager authenticationManager = builder.getSharedObject(AuthenticationManager.class);
               // 강제 세션 로그인을 위해 authenticationManager 필요
               builder.addFilter(new JwtAuthenticationFilter(authenticationManager));
               super.configure(builder);
          }
     }
     .
     .
     .

필터를 등록할 때, new JwtAuthenticationFilter() 안에 authenticationManager를 넣어줘야 한다. 왜냐하면 이전 18장에서 나왔듯이, authenticationManager를 가지고 강제 로그인과 세션을 생성하기 때문에 꼭 필요하다.

그리고 필터 적용을 해주자

@Bean
     public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
          log.debug("디버그 : filterChain 빈 등록됨");
          http.headers().frameOptions().disable(); // iframe 허용 X
          http.csrf().disable();
          http.cors().configurationSource(configurationSource());

          // jSessionId 서버쪽에서 관리 안하겠다는 것
          http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
          // react, app 요청 예정
          http.formLogin().disable();
          // 브라우저가 팝업창을 이용해서 사용자 인증을 진행
          http.httpBasic().disable();
          // 필터 적용
          http.apply(new CustomSecurityFilterManager()); // 요기!!!!!!!!!!!!!

          // Exception 가로채기(일관성을 위해서)
          http.exceptionHandling().authenticationEntryPoint((request, response, authException) -> {
               CustomResponseUtil.unAuthentication(response, "로그인을 진행해 주세요");
          });
          .
          .
          .
  • 로그인 진행시

  • 잘못된 로그인 시

이 부분을 다음 시간에 잡을 것

profile
블로그 이전 : https://medium.com/@jaegeunsong97

0개의 댓글