Spring permitAll()需要身份验证

Spring permitAll()需要身份验证,spring,spring-security,jwt,Spring,Spring Security,Jwt,我正在尝试用Spring开发REST应用程序,并使用JWT进行身份验证 目前,我努力实现的目标是: GET/api/subjects/*应可供所有用户访问 POST/api/subjects/*应仅可供管理员用户访问 问题是,对于这两种情况,JWT过滤器都会被调用,我会得到一个错误响应,指出JWT令牌丢失 我已经实现了我的WebSecurityConfig,包括一个JWT过滤器来替换BasicAuthenticationFilter: @配置 公共类WebSecurityConfig扩展了W

我正在尝试用Spring开发REST应用程序,并使用JWT进行身份验证

目前,我努力实现的目标是:

  • GET/api/subjects/*
    应可供所有用户访问
  • POST/api/subjects/*
    应仅可供管理员用户访问
问题是,对于这两种情况,JWT过滤器都会被调用,我会得到一个错误响应,指出JWT令牌丢失

我已经实现了我的
WebSecurityConfig
,包括一个JWT过滤器来替换
BasicAuthenticationFilter

@配置
公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{
@自动连线
JWTAuthenticationEntryPoint authenticationEntryPoint;
@自动连线
JWTAuthenticationProvider JWTAuthenticationProvider;
@凌驾
public void configure(WebSecurity web)引发异常{
//忽略().antMatchers(HttpMethod.GET,“/api/subjects/*”;
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.csrf().disable()
.授权请求()
.antMatchers(HttpMethod.GET,“/api/subjects/*”).permitAll()
.antMatchers(HttpMethod.POST,“/api/subjects/*”).hasRole(Profile.Role.ADMIN.toString())
.及()
.会议管理()
.sessionCreationPolicy(sessionCreationPolicy.STATELESS)
.及()
.addFilterAt(authenticationTokenFilter(),BasicAuthenticationFilter.class)
.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
}
公共JWTAuthenticationFilter authenticationTokenFilter(){
返回新的JWTAuthenticationFilter(authenticationManager(),authenticationEntryPoint);
}
公共ProviderManager authenticationManager(){
返回新的ProviderManager(新的ArrayList(Arrays.asList(jwtAuthenticationProvider));
}
}
我的
JWTAuthenticationFilter
的实现是基于
BasicAuthenticationFilter
的实现的:

公共类JWTAuthenticationFilter扩展了OncePerRequestFilter{
私有静态最终字符串JWT\u TOKEN\u START=“JWT”;
私人AuthenticationManager AuthenticationManager;
私有身份验证入口点身份验证入口点;
公共JWTAuthenticationFilter(AuthenticationManager AuthenticationManager,AuthenticationEntryPoint AuthenticationEntryPoint){
Assert.notNull(authenticationManager,“身份验证管理器不能为null”);
Assert.notNull(authenticationEntryPoint,“身份验证入口点不能为null”);
this.authenticationManager=authenticationManager;
this.authenticationEntryPoint=authenticationEntryPoint;
}
@凌驾
受保护的无效doFilterInternal(HttpServletRequest HttpServletRequest,
HttpServletResponse HttpServletResponse,
FilterChain FilterChain)抛出ServletException、IOException{
String header=httpServletRequest.getHeader(“授权”);
if(header==null | |!header.startsWith(JWT_TOKEN_START)){
抛出新的IllegalStateException(“标头不包含:\“Authorization\”:\“JWT\”。值:“+标头”);
}
试一试{
字符串jwt=header.substring(jwt_TOKEN_START.length()).trim().replace(“,”);
JWTAuthenticationToken JWTAuthenticationToken=新的JWTAuthenticationToken(jwt);
this.authenticationManager.authenticate(jwtAuthenticationToken);
doFilter(httpServletRequest,httpServletResponse);
}catch(AuthenticationException auth){
SecurityContextHolder.clearContext();
this.authenticationEntryPoint.start(httpServletRequest、httpServletResponse、auth);
}
}
}

导致此问题的原因是什么?

每个文档中可能存在的重复:
调用antMatcher(字符串)将覆盖之前对requestMatchers()、antMatcher(字符串)、regexMatcher(字符串)和requestMatcher(requestMatcher)的调用您正在用后者覆盖前一个请求匹配器。请按照此处接受的答案进行解答:@Andonaeus:您显示的示例具有不同的URL。在我的例子中,我有相同的URL,但不同的HTTP方法。在这种情况下,我将如何构造我的ant模式?每个文档中可能存在的重复:
调用antMatcher(String)将覆盖之前对requestMatchers()、antMatcher(String)、regexMatcher(String)和requestMatcher(requestMatcher)的调用您正在用后者覆盖前一个请求匹配器。请按照此处接受的答案进行解答:@Andonaeus:您显示的示例具有不同的URL。在我的例子中,我有相同的URL,但不同的HTTP方法。在这种情况下,我将如何构造我的ant模式?