Spring boot Spring Boot JWT:GET API禁止403错误

Spring boot Spring Boot JWT:GET API禁止403错误,spring-boot,spring-security,jwt,http-status-code-403,Spring Boot,Spring Security,Jwt,Http Status Code 403,我正在学习为我的端点实现JWT身份验证。 我能够使用POST调用成功地生成JWT代码。 但面临在SecurityConfig类中验证简单GET API的问题。希望得到一些帮助。 在这个问题上被困了2天,在这个问题上找不到任何东西 我在调试模式下配置了日志,并发现以下错误。我在现有的堆栈溢出线程上找不到任何合适的解决方案 DEBUG 8196 --- [nio-8080-exec-2] o.apache.coyote.http11.Http11Processor : Error parsing

我正在学习为我的端点实现JWT身份验证。 我能够使用POST调用成功地生成JWT代码。 但面临在SecurityConfig类中验证简单GET API的问题。希望得到一些帮助。 在这个问题上被困了2天,在这个问题上找不到任何东西

我在调试模式下配置了日志,并发现以下错误。我在现有的堆栈溢出线程上找不到任何合适的解决方案

DEBUG 8196 --- [nio-8080-exec-2] o.apache.coyote.http11.Http11Processor   : Error parsing HTTP request header        
java.io.EOFException: null
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1231) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1141) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:795) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:359) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:261) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.39.jar:9.0.39]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_191]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.39.jar:9.0.39]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_191]
错误:获取API

{
"timestamp": "2020-11-22T17:20:57.227+00:00",
"status": 403,
"error": "Forbidden",
"message": "",
"path": "/hello"
}

SecurityConfig.class

@Override
protected void configure(HttpSecurity http) throws Exception{
    http.csrf().disable();
    http.authorizeRequests().antMatchers("/authenticate").permitAll()
    .antMatchers("/hello").authenticated().and()
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
将我的代码上载到Repo以便更好地参考。
JwtRequestFilter.java->doFilterInternal->44行中,它看起来像是一个小的打字错误

我想你的意思是:

...
        if(username != null && SecurityContextHolder.getContext() != null) {
            UserDetails userDetails = myUserDetailsService.loadUserByUsername(username);
            if(jwtUtils.validateToken(jwt, userDetails)) {
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            }
        }
        filterChain.doFilter(request, response);
...
或许:

...
        if(username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
            UserDetails userDetails = myUserDetailsService.loadUserByUsername(username);
            if(jwtUtils.validateToken(jwt, userDetails)) {
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            }
        }
        filterChain.doFilter(request, response);
...
请注意,我将
SecurityContextHolder.getContext()==null
更改为
SecurityContextHolder.getContext()!=第一个代码段中的null
。在第二个代码片段中,我将其更改为
SecurityContextHolder.getContext().getAuthentication()==null


根据,
SecurityContextHolder.getContext()
不应为空。

当您调用
GET/hello
时,您是否有像
authorization:Bearer
这样的标题?谢谢,它确实有效,但我不清楚它为什么与notequal一起工作,Equalto Null检查用于检查传入请求的安全上下文中是否没有任何内容,然后我可以验证jwt并自行设置安全上下文。@Rohit我认为安全上下文已经存在,但
身份验证
属性为Null。我将答案更改为包括使用此检查
SecurityContextHolder.getContext().getAuthentication()==null