티스토리 뷰

카테고리 없음

SpringBoot3.x SpringSecurity JWT

KyeongRok Kim 2023. 12. 22. 14:49

 

JWT를 쓴다는 것은

Header를 변조 한다는 것

Authorization이 추가 되기 때문에 변조가 됨

 

변조가 되면 preflight요청을 하게 되는데 여기에서 HttpMethod중 Options가 막혀 있다면 본 호출을 하지 않습니다.

 

@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .cors(c->c.disable())
                .httpBasic(basic ->basic.disable()) // ui를 통한 로그인 비활성
                .csrf(csrf -> csrf.disable()) // 토큰을 사용하기 때문에 csrf 설정 disable
                .headers(c -> c.frameOptions(f -> f.disable()).disable())
                .authorizeHttpRequests(request -> {
                    try{
                        request
                                .requestMatchers(PERMIT_ALL).permitAll() // 회원가입과 같은 오픈된 URL
                                .requestMatchers(HttpMethod.GET, AUTHENTICATED_GET).authenticated()
                                .anyRequest().authenticated()
                        ;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                })
                .addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class)
                .exceptionHandling(handler -> handler.authenticationEntryPoint(authenticationEntryPoint)
                )
                .sessionManagement(c ->c.sessionCreationPolicy(SessionCreationPolicy.STATELESS))// 세션 사용하지 않기 때문에 세션 설정 STATELESS
                ;
        return http.build();
    }

 

 

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedOrigins(
                        "https://dev.yourdomain.ai", "https://yourdomain.ai",
                        "http://localhost:3000", "http://localhost:3001"
                )
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
                .allowCredentials(true)
                .allowedHeaders("Content-Type", "Authorization", "X-Requested-With", "X-AUTH-TOKEN", "Accept", "Referer");
    }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함