Spring安全自定义标头请求匹配器不工作



我有一个使用spring安全的要求,我想如果任何类型的请求包含具有特定值的特定头,那么只有它应该被允许访问api,否则不。我的配置代码如下:

@Configuration
@EnableWebSecurity
public class AppSecurityConfig extends WebSecurityConfigurerAdapter{
public AppSecurityConfig() {
}

@Autowired
public void configure​(HttpSecurity http) throws Exception {
http.authorizeRequests().requestMatchers(new CustomHeaderRequestMatcher()).permitAll();

}
}

下面是客户报头请求匹配器业务逻辑:

public class CustomHeaderRequestMatcher implements RequestMatcher{
public CustomHeaderRequestMatcher() {

}
@Override
public boolean matches(HttpServletRequest request) {
if(Objects.nonNull(request.getHeader("my-token"))
&& request.getHeader("my-token").equals("abc")) {
System.out.println("true");
return true;
}
System.out.println("false");
return false;
}
}

但是在这里我可以看到,即使我没有传递自定义标题"my-token"在我的请求,它是允许访问api。然而,对于每个请求,都会调用自定义请求匹配器类。我很困惑为什么春天保安不处理这个案子?

您没有阻止所有其他请求。Spring看到了第一条规则,却不知道下一步该做什么。您可以添加"anyRequest().denyAll()"或"anyRequest () .authenticated()";在"permitAll()">

最新更新