【发布时间】:2020-01-08 04:32:23
【问题描述】:
尝试向浏览器发送消息时出现以下错误
java.lang.IllegalStateException: getOutputStream() has already been called for this response
我正在使用基本身份验证并在身份验证失败时尝试发送消息。所以在执行以下代码时出现上述错误。
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
org.springframework.security.core.AuthenticationException authException)
throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "Basic realm=" + getRealmName());
response.setContentType("text/html");
PrintWriter writer = response.getWriter();//here getting error
writer.println("HTTP Status 401 : " + authException.getMessage());
}
@Override
public void afterPropertiesSet() throws Exception {
setRealmName("localhost");
super.afterPropertiesSet();
}
}
有人可以帮忙吗?
【问题讨论】:
标签: java spring-boot spring-security