Spring boot Security Filter에 예외 URL 적용

2024. 9. 8. 23:20Java/Spring Boot Security

String[] exceptUrl = {
	"/",
	"/error",
};
    
OrRequestMatcher orRequestMathcers = new OrRequestMatcher(Util.getRequestMatcher(exceptUrl));
NegatedRequestMatcher negatedRequestMatcher = new NegatedRequestMatcher(orRequestMathcers);
    
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		http.authorizeHttpRequests((requests) -> requests
				.requestMatchers(exceptUrl).permitAll()
				.anyRequest().authenticated())
				.addFilterBefore(new SecurityFilter(negatedRequestMatcher), UsernamePasswordAuthenticationFilter.class)
			.formLogin((form) -> form
    	    	.loginPage("/login")
				.permitAll())
			.logout((logout) -> logout.permitAll());
		return http.build();
	}