最近做公司的管理系统,发现登录的session过期以后,刷新页面,很多接口页面会报500错误,getWriter() has already been called for this response
起初没有在意,以往是页面中断了请求,导致的错误,后面发现调用接口,大面积(seesion过期后)会报错,并不影响会跳登录页面,但是这个500的弹框,很不友好,也很不爽;
决心查看一番:
查看后台日志详细错误信息如下:
in context with path [/owner] threw exception
java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:546)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:210)
at com.aciga.framework.logger.filter.AccessLoggerFilter.doFilterInternal(AccessLoggerFilter.java:112)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.aciga.framework.logger.filter.TraceIdFilter.doFilter(TraceIdFilter.java:33)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:109)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
断点调试
发现接口都没有进去,然后在报错的地方在日志组件里面:
根据报错信息猜测:
IllegalStateException: getWriter() has already been called for this response
从字面意思不难得出错误原因:HttpServletResponse中的PrintWriter已经被手动调用过了。所以当servlet执行到方法结果处理逻辑时,需要将返回值输出到writer中去,这时发现PrintWriter已经被调用过。于是servlet认为这是使用混乱的逻辑错误,于是抛出错误。
验证:由于接口都没有进去,怀疑是权限拦截框架的逻辑处理;
翻看代码发现还真是,在shiro的鉴权处理时,发现已经登录过期了,需要返回给页面,跳转登录页面信息;
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response)
throws Exception {
if (isLoginRequest(request, response)) {
if (isLoginSubmission(request, response)) {
return executeLogin(request, response);
} else {
return true;
}
} else {
// 返回固定的JSON串
WebUtils.toHttp(response).setContentType("application/json; charset=utf-8");
JSONObject result = new JSONObject();
result.put("message", "登录失效");
result.put("code", 1000);
WebUtils.toHttp(response).getWriter().print(result);
return false;
}
}
找到地方就好办了,改写一下处理方式,使用ServletOutputStream进行返回:
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response)
throws Exception {
if (isLoginRequest(request, response)) {
if (isLoginSubmission(request, response)) {
return executeLogin(request, response);
} else {
return true;
}
} else {
// 返回固定的JSON串
WebUtils.toHttp(response).setContentType("application/json; charset=utf-8");
JSONObject result = new JSONObject();
result.put("message", "登录失效");
result.put("code", 1000);
ServletOutputStream output = response.getOutputStream();
output.write(result.toString().getBytes());
output.flush();
return false;
}
}
实测,完美解决,控制台不报错了,页面也能正常返回,完美!