2017-07-23 29 views
0

我得到threw exception java.lang.IllegalStateException: getWriter() has already been called for this response at org.apache.catalina.connector.Response.getOutputStream(Response.java:544)如何解決這個IlleagalstateException在訪問部署在Tomcat上的restful spring-boot應用程序?

同時通過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; 
} 

怎樣才能解決這個問題?

+0

告訴我們的代碼你正在嘗試做的。看起來你一旦調用writer就調用getWriter()。 – Barath

+0

我正在嘗試執行安靜的基本身份驗證。當我從我的客戶端登錄時,我收到了這個異常。在登錄期間,我從安全上下文對象 – Prince

+0

返回經過身份驗證的用戶請分享最少的代碼。 – Barath

回答

0

變化:

@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; 
} 
+0

好的,謝謝。這會解決我的問題嗎? – Prince

+0

你可以嘗試讓我們知道。 @Prince – Barath

+0

它給「org.springframework.security.authentication.UsernamePasswordAuthenticationToken無法轉換爲org.springframework.security.core.userdetails.User」異常 – Prince

相關問題