【问题标题】:How can I fix this IlleagalstateException on accessing restful spring-boot application deployed on Tomcat?如何在访问部署在 Tomcat 上的 restful spring-boot 应用程序时修复此 IllegalstateException?
【发布时间】:2017-12-29 02:34:02
【问题描述】:

我收到threw exception java.lang.IllegalStateException: getWriter() has already been called for this response at org.apache.catalina.connector.Response.getOutputStream(Response.java:544)

在从 angular2 通信我的服务器端代码(RESTFul、Spring-boot)时。两者都部署在同一台服务器上(Tomcat 8.0.2)。

以下是我的代码

@RequestMapping(value="/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public User login() {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    User user = (User) principal;

    return user;
}

如何解决这个问题?

【问题讨论】:

  • 用代码告诉我们你想要做什么。一旦调用了作者,您似乎正在调用 getWriter()。
  • 我正在尝试进行宁静的基本身份验证。当我从我的客户端登录时,我得到了这个异常。在登录期间,我从安全上下文对象返回经过身份验证的用户
  • 请分享最少的代码。

标签: spring tomcat spring-boot tomcat8 restful-authentication


【解决方案1】:

改变:

@RequestMapping(value="/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public User login(Principal principal) {
    User user = (User) principal; // take care of the conversion!
    return user;
}

@RequestMapping(value="/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public User login(Authentication authentication) {
    User user = (User) authentication.getPrincipal(); // take care of the conversion!
    return user;
}

【讨论】:

  • 好的,谢谢。这能解决我的问题吗?
  • 你能不能告诉我们。 @王子
  • 它给出了“org.springframework.security.authentication.UsernamePasswordAuthenticationToken cannot be cast to org.springframework.security.core.userdetails.User”异常
  • 你必须处理它。我怀疑转换,根据要求填充
猜你喜欢
  • 2016-07-03
  • 2021-11-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 2020-11-27
相关资源
最近更新 更多