当前位置: 首页 > 知识库问答 >
问题:

Spring Boot 1.4:主体不得为空异常

盖雪峰
2023-03-14

自从Spring Boot1.4发布以来,我有了一个定制的身份验证提供程序,用于管理Spring Security的JWT令牌的解析。基本上,当令牌无效或过期时,我会抛出BadCredentialsException。我还有一个AutenticationEntryPoint,它在JSON中使用未经授权的HttpServlet响应重新格式化消息

@Override
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException
{
    httpServletResponse.setContentType("application/json");
    httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    httpServletResponse.getOutputStream().println("{ \"error\": \"" + e.getMessage() + "\" }");

}

下面是管理身份验证提供程序异常的筛选器

@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException

{

    String authToken = httpServletRequest.getHeader("Authorization");



    JwtToken token = new JwtToken(authToken);
    try
    {
        Authentication auth = authenticationManager.authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(auth);
        filterChain.doFilter(httpServletRequest, httpServletResponse);

    }
    catch(AuthenticationException ae)
    {
        SecurityContextHolder.clearContext();
        unauthorizedHandler.commence(httpServletRequest, httpServletResponse, ae);
    }

java.lang.IllegalArgumentException:主体不能为空堆栈跟踪:

java.lang.IllegalArgumentException: Principal must not be null
at org.springframework.util.Assert.notNull(Assert.java:115) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.boot.actuate.audit.AuditEvent.<init>(AuditEvent.java:83) ~[spring-boot-actuator-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.actuate.audit.AuditEvent.<init>(AuditEvent.java:59) ~[spring-boot-actuator-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.actuate.security.AuthenticationAuditListener.onAuthenticationFailureEvent(AuthenticationAuditListener.java:67) ~[spring-boot-actuator-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.actuate.security.AuthenticationAuditListener.onApplicationEvent(AuthenticationAuditListener.java:50) ~[spring-boot-actuator-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.actuate.security.AuthenticationAuditListener.onApplicationEvent(AuthenticationAuditListener.java:34) ~[spring-boot-actuator-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.security.authentication.DefaultAuthenticationEventPublisher.publishAuthenticationFailure(DefaultAuthenticationEventPublisher.java:124) ~[spring-security-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.security.authentication.ProviderManager.prepareException(ProviderManager.java:240) ~[spring-security-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:233) ~[spring-security-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) ~[spring-security-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:454) ~[spring-security-config-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at com.icentia.tracking.security.JwtFilter.doFilterInternal(JwtFilter.java:49) ~[classes/:na]

这是来自Spring引导执行器。如果我移除它,它仍然像以前一样工作?!?

这里似乎列出了一个bug,但并不相同:https://github.com/spring-projects/spring-boot/issues/6447

共有1个答案

督翰学
2023-03-14

确保Principal接口的getName()方法JWTToken类中返回一个非空值。

 类似资料:
  • 问题内容: 所以我在与node.js一起运行的server.js文件中有以下代码。我正在使用express处理HTTP请求。 我在终端中运行以下命令: 运行该server.js后,将输出以下内容。 所以req.body是。我阅读了其他有关类似问题的Stack Overflow帖子,其中由于正文解析器,内容类型不正确。但这不是问题,因为内容类型是application / json。 有什么想法如何

  • 问题内容: 我正在尝试通过android应用程序中的改造连接到Rest服务。我正在得到回应。但是,当服务有一些错误响应时,就会发生转换异常,现在我想根据响应主体执行一些操作。但是我得到的响应主体为NULL。但是改造日志中有一条错误消息。为什么会这样。 码: 在这里,我得到的。不知道为什么 我是否需要使用rest适配器进行设置,以便将响应传递到改造错误对象。 问题答案: 试试这个代码: 与 Retr

  • 在Client.ExecuteMethod调用上。 为什么会发生这种情况?

  • 所以我想不通这个看似最简单的问题。我希望创建HttpEntity,其中header具有内容类型application/Json,主体是一个空的Json对象:

  • 问题内容: 我在Django中创建了一个新项目,并粘贴了另一个项目中的一些文件。每当我尝试运行服务器时,都会收到以下错误消息: 这是我的settings.py 这里也是manage.py 有什么帮助吗?谢谢! 问题答案: 就像错误说的那样,您没有定义。您需要在 settings.py中 添加一个。 如果未设置,Django将拒绝启动。 您可以在docs中阅读有关此设置的更多信息。 该可以做出点事来

  • 我有一个简单的Spring测试 我想验证响应正文是否为空。例如。做一些类似的事情