@Bean fun getJwtFilter(): FilterRegistrationBean { val filterBean = FilterRegistrationBean() filterBean.filter = JwtFilter() val including = appProperties.filter?.jwt?.include?.split(",") including?.forEach { it -> filterBean.addUrlPatterns(it) } return filterBean }
@Component class JwtFilter : GenericFilterBean() { private val log: Logger = LoggerFactory.getLogger(JwtFilter::class.java.simpleName) privatevalmethods= arrayListOf(RequestMethod.GET.name, RequestMethod.POST.name, RequestMethod.PUT.name, RequestMethod.DELETE.name)
override fun doFilter(servletRequest: ServletRequest?, servletResponse: ServletResponse?, filterChain: FilterChain?){ val request = servletRequest as HttpServletRequest val response = servletResponse as HttpServletResponse val path = request.requestURI.substring(request.contextPath.length).replace("[/]+$", "") log.debug("doFilter(): path:$path") if (methods.contains(request.method)) { if (!FilterConfig.JwtExcludePath.containsKey(path)) { val token = request.getHeader(AuthConfig.TokenHeader) if (!JwtUtil.isValid(token, KeyConfig.publicKey)) { response.sendError(401) return } } } filterChain?.doFilter(request, response) } }