Spring boot 3.2.5 and JDK17 셋팅

2024. 5. 19. 20:38Java/Spring Boot

서비스 Prot 설정

# application.properties
server.port=8080

로깅설정

# application.properties
logging.level.root=info
logging.level.{원하는 패키지}=debug

Filter 설정

JDK17에서는 jakarta.servlet.Filter를 사용해야 됨
chain.doFilter(request, response)가 빠지면 안됨
urlPatterns를 "/*"로 설정해야 함

# 필터 생성
@Slf4j
public class AuthFilter implements Filter {@Override
	public void doFilter(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response,
			jakarta.servlet.FilterChain chain) throws IOException, jakarta.servlet.ServletException {
		// TODO Auto-generated method stub
		log.debug("434343");
		chain.doFilter(request, response);        
	}
}

# 환경설정
@Slf4j
@Configuration
public class FilterConfig {

	@Bean
	public FilterRegistrationBean<Filter> filterBean() {
		FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
		bean.setFilter(new AuthFilter());
		bean.setOrder(1);
		bean.addUrlPatterns("/*");
		log.debug("aaaaa333");
		return bean;
	}
}

Interceptor 설정

# interceptor 생성
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		log.debug("-------------------------");
		log.info("=============");
		return HandlerInterceptor.super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		log.debug("-------------------------");
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}
}

# 환경설정
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// TODO Auto-generated method stub
		registry.addInterceptor(new AuthInterceptor())
			.addPathPatterns("/**");
		log.debug("sssssssssssssssssssssswwe");
		WebMvcConfigurer.super.addInterceptors(registry);
	}
}

TS

  • Bound mismatch: The type Filter is not a valid substitute for the bounded parameter <T extends Filter> of the type FilterRegistrationBean<T>

JDK17에서는 jakarta.servlet.Filter를 사용해야 함

  • Filter와 Interceptor를 같이 사용할때, 한 쪽이 작동하지 않는 경우

Filter에 chain.doFilter(request, response)가 빠지고, 
환경설정에서 urlPatterns를 "/**"로 설정한 경우 발생하였음

@Slf4j
public class AuthFilter implements Filter {@Override
	public void doFilter(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response,
			jakarta.servlet.FilterChain chain) throws IOException, jakarta.servlet.ServletException {
		// TODO Auto-generated method stub
		log.debug("434343");
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();
		log.debug(session.getId());
	}
}

@Slf4j
@Configuration
public class FilterConfig {

	@Bean
	public FilterRegistrationBean<Filter> filterBean() {
		FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
		bean.setFilter(new AuthFilter());
		bean.setOrder(1);
		bean.addUrlPatterns("/**");
		log.debug("aaaaa333");
		return bean;
	}
}

'Java > Spring Boot' 카테고리의 다른 글

ViewController 추가  (0) 2024.06.13
@Controller와 @RestController를 같이 사용하기  (0) 2024.05.26
Spring Authorization Server 테스트  (0) 2024.03.14
Spring boot 3.2.3 설정  (0) 2024.03.14
Spring(Boot) 사용법  (0) 2022.02.25