강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

개발자는개발님의 프로필 이미지
개발자는개발

작성한 질문수

스프링부트 시큐리티 & JWT 강의

BasicAuthenticationFilter 필터에 대해서 질문드립니다.

작성

·

880

1

먼저 좋은 강의를 올려주신거에 감사합니다!
 
강의를 보다가 BasicAuthenticationFilter를 상속받은 필터가 생각과는 다르게 동작해서 질문드립니다.
강의에서는 BasicAuthenticationFilter는 인증, 인가가 필요한 요청에만 동작하고 그 외에는 동작하지 않는다고 나와있는데.
 
시큐리티 설정
@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를 상속한 필터로 교체 해보려고 하는데.

생각한 것 처럼 동작하지가 않네요 . 단순하게는 필터 시작할 때, 리스트를 만들어서 해당 요청은 넘어가게 해줘도 되겠지만 더 깔끔하게 처리하고 싶어서 문의드립니다.

답변 1

1

최주호님의 프로필 이미지
최주호
지식공유자

/ 가 퍼밋올이면

 

컨트롤러 모든 주소 개방입니다.

저걸 수정하세요

개발자는개발님의 프로필 이미지
개발자는개발

작성한 질문수

질문하기