작성
·
880
1
@Configuration
@EnableWebSecurity
public class Config extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.formLogin().disable()
.httpBasic().disable()
.addFilter(new TestFilter(authenticationManager()))
.authorizeRequests()
.mvcMatchers("/").permitAll()
.mvcMatchers("/user").hasRole("USER");
}
}
컨트롤러
@RestController
public class BasicController {
@GetMapping("/")
public String test1() {
return "기본 접근";
}
@GetMapping("/user")
public String test2() {
return "유저 접근";
}
}
public class TestFilter extends BasicAuthenticationFilter {
public TestFilter(AuthenticationManager authenticationManager) {
super(authenticationManager);
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("인증이 필요한거만 실행");
chain.doFilter(request, response);
}
}
이렇게 간단한 필터 테스트용 프로젝트를 작성했는데. "/" 요청 같이 permitAll() 처리하면 인증이나 인가가 필요 없어서
필터를 타지 않을거라 생각했는데. "인증이 필요한거만 실행" 이 찍히면서 필터를 타는데. 어디가 문제인지 모르겠습니다..
지금 jwt 토큰을 사용해서 프로젝트를 구현 중인데. 필터를 onceperrequestfilter를 상속 받아서 구현해서 로그인이나 토큰 재발급 같은 인증, 인가가 필요 없는 요청도 필터를 타면서 만약에 사용이 불가능한 토큰이 들어 있으면 막혀가지고 BasicAuthenticationFilter를 상속한 필터로 교체 해보려고 하는데.
생각한 것 처럼 동작하지가 않네요 . 단순하게는 필터 시작할 때, 리스트를 만들어서 해당 요청은 넘어가게 해줘도 되겠지만 더 깔끔하게 처리하고 싶어서 문의드립니다.