spring - Spring Security - 蚂蚁匹配器允许所有不工作
问题描述
我对 antMatchers 有疑问。这不像我预期的那样工作。我尝试允许所有端点 /tokens/**,但是当调用此端点时,我的过滤器也会被调用(JwtTokenVerifier)
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/tokens/**").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.cors()
.and()
.addFilter(new JwtUsernameAndPasswordAuthenticationFilter(authenticationManager(), this.jwtProperties, objectMapper))
.addFilterAfter(new JwtTokenVerifier(this.jwtProperties), JwtUsernameAndPasswordAuthenticationFilter.class);
}
每次我尝试访问例如 /tokens/refresh-access-token 时都会调用 JwtTokenVerifier。
@PostMapping("/tokens/refresh-access-token")
@ResponseStatus(HttpStatus.OK)
public Map<String,String> refreshAccessToken(@RequestBody String refreshToken)
令牌验证器:
public class JwtTokenVerifier extends OncePerRequestFilter {
private final JwtProperties jwtProperties;
public JwtTokenVerifier(JwtProperties jwtProperties) {
this.jwtProperties = jwtProperties;
}
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
解决方案
如果可能,您可以尝试通过覆盖 WebSecurity 方法将 URL 从 Spring Security 处理中完全排除。不过,我不知道您是否想完全忽略该路径的 FilterChain。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/tokens/**");
}
推荐阅读
- ios - 是否可以从另一个视图调用 IBAction UIButton?
- node.js - 如何从MongoDB中的数组中删除对象?
- timer - Knife.timer 关于 Lua 语法 - 行为不符合预期
- javascript - 关于连接 React 和 NodeJS express 的问题
- python - 如何检测被阴影遮挡的黄色物体?
- php - 当两个文件的行中有相同的数据时,PHP CSV 合并两个 CSV 文件
- python - 'function' 对象不可迭代
- sql - Postgresql 根据 B 列上的某些条件选择不同的 A 列
- javascript - 不和谐机器人是否被归类为 Web 服务器?
- c# - 如何启动 webApi Asp.net