【发布时间】:2019-02-10 11:26:54
【问题描述】:
我的 Spring Security 有以下配置
http
// if I gonna comment adding filter it's gonna work as expected
.addFilterBefore(tokenAuthenticationFilter, BasicAuthenticationFilter.class)
.authorizeRequests()
.antMatchers("/rest/_health")
.permitAll()
.anyRequest()
.authenticated()
.and()
.csrf()
.disable();
因此,如果没有自定义过滤器,一切都会按预期工作 - 我可以访问 /rest/_health 并拒绝访问其他所有内容。
但是当我添加此过滤器时 - 匹配器不起作用,过滤器即使对“permitAll”资源也有效。
我的过滤器中的代码如下所示:
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
try {
String token = httpRequest.getHeader(HttpHeaders.AUTHORIZATION);
Authentication authentication = authenticationManager.authenticate(
new TokenBasedAuthentication(token)
);
SecurityContextHolder.getContext().setAuthentication(authentication);
chain.doFilter(request, response);
} catch (AuthenticationException ex) {
authenticationEntryPoint.commence(httpRequest, httpResponse, ex);
}
}
有什么建议吗?
【问题讨论】:
标签: java spring-security servlet-filters