Den*_*nov 0 java regex spring spring-security pattern-matching
在我们的应用程序中,我们正在创建不应被某些网址命中的请求过滤器。我们希望能够排除像 spring do witch url patterns 这样的 url,例如:
// register filter in bean
FilterRegistrationBean filterBeanRegistration = new FilterRegistrationBean();
filterBeanRegistration.setFilter(myFilter());
filterBeanRegistration.addInitParameter("excluded", "*/foo/**, */bar/**");
...
Run Code Online (Sandbox Code Playgroud)
并且新的过滤器不会点击像这样的网址domain:8080/aaa/foo/xxxx,domain:8080/bbb/bar/xxxx。
你能告诉我如何用 spring 类或其他简单的方法来做到这一点吗?谢谢指教。
编辑:有一种FilterBeanRegistration#addUrlPatterns(String... urls)方法可以指定网址,但没有任何格式可以说明应该点击哪个网址。为了我们的目的,最好排除一些网址。
您可以使用org.springframework.web.filter.OncePerRequestFilter. 每个传入请求都会执行一次。您可以覆盖shouldNotFilter方法以排除您不希望过滤器运行的 URL。示例代码:
public class MyFilter extends OncePerRequestFilter {
private static final String[] excludedEndpoints = new String[] {"*/foo/**, */bar/**"};
@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
return Arrays.stream(excludedEndpoints)
.anyMatch(e -> new AntPathMatcher().match(e, request.getServletPath()));
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
// Filtering logic goes here. call below line on successful authentication.
filterChain.doFilter(request, response);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4528 次 |
| 最近记录: |