TIL_WIL

TIL_230227

성-민 2023. 2. 28. 03:56

오늘은 백엔드에서 구현한 기능을 다 합치며 잘 못 만들었던 부분을 모두 수정했고, 기능이 돌아가도록 구현했다. 그리고 CORS에러가 발생했지만 비교적 간단하게 해결했다.

 

내가 했던 Timestamped를 extends시켜주지 않아서 생긴 오류를 해결했다.

그리고 FE에서는 Token값을 디코드 한 후 BE에게 값을 반환해주지 않아서 403오류가 났고 이를 해결하지 못하고 끝냈다. 내일은 무조건 해결해야겠다.

 

@Configuration
@RequiredArgsConstructor
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig {
    private final JwtUtil jwtUtil;
    
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring()
                .requestMatchers(PathRequest.toH2Console())
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations());
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.csrf().disable();

        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.authorizeRequests().antMatchers("/api/auth/**").permitAll()
                .antMatchers(HttpMethod.GET,"/api/post/**").permitAll()
                .anyRequest().authenticated()
                .and().addFilterBefore(new JwtAuthFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class);


//        http.formLogin().loginPage("/api/auth/login").permitAll(); //로그인 페이지 : form이 있어야 가능,그냥 formLogin만 사용하면 loginPage 없어도됨
//        http.addFilterBefore(new CustomSecurityFilter(userDatailService, passwordEncoder()), UsernamePasswordAuthenticationFilter.class); 써주면 custom한 security filter 쓸수있다.

        http.exceptionHandling().accessDeniedPage("/api/auth/login"); // 인증불가시 보낼곳

        // 401 Error 처리, Authorization 즉, 인증과정에서 실패할 시 처리
//        http.exceptionHandling().authenticationEntryPoint(customAuthenticationEntryPoint);

        // 403 Error 처리, 인증과는 별개로 추가적인 권한이 충족되지 않는 경우
//        http.exceptionHandling().accessDeniedHandler(customAccessDeniedHandler);


        return http.build();
    }

}

시큐리티 설정

@EnableWebSecurity: Spring Securit 설정 활성화

@EnalbeGlobalMethodSecurity(securedEnabled = true): @Secured라는, User의 Role을 보고 접근 권한에 따라 막을 수 있는 어노테이션 활성화

 

addMapping("/**")  // url(도메인 뒤에 붙는)
(/**) 와일드카드


allowCredentials(true) 사용시
allowedOriginPatterns("*")   //도메인
을 사용해야한다.

 

 

exposedHeaders("*") 모든 헤더값을 반환