2017-08-10 331 views
1

我想在Spring安全筛选器中更改响应的内容。比方说,我要的是如下:在Spring安全筛选器中修改响应内容

public class SecurityFilter extends OncePerRequestFilter { 

    @Override 
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 
     filterChain.doFilter(request, response); 

     //response.getWriter().write("a"); 
     PrintWriter p = new PrintWriter(response.getOutputStream()); 
     p.println("Hello"); 
     p.flush(); 
     p.close(); 
    } 
} 

过滤器背后一个REST服务驻留在检索字符串列表。 如果我使用getOutputStream()来写,那么我可以在客户端(而不是字符串'你好')的字符串列表。如果我使用的getWriter(),然后我得到以下错误:

2017-08-10 09:10:42,900 ERROR [org.springframework.boot.web.support.ErrorPageFilter] (default task-7) Forwarding to error page from request [/worker/system/urmlprod30] due to exception [UT010006: 
Cannot call getWriter(), getOutputStream() already called]: java.lang.IllegalStateException: UT010006: Cannot call getWriter(), getOutputStream() already called 

我怎么能修改Spring Security的响应内容过滤? 顺便说一句我使用wildfly10,但它也应该在Tomcat和Weblogic12c上工作。我使用Spring Boot。

相关部分从securityContext.xml:

<security:csrf disabled="true" /> 
     <security:custom-filter ref="securityFilter" after="FORM_LOGIN_FILTER"/> 
    </security:http> 

我相信的响应已经发出的时候,我想写我的内容,但我能做些什么呢?

任何反应将不胜感激!

感谢,五

-----更新------ 我忘了提,我需要从REST服务的响应,因为我想操纵它。

+0

不错,它工作!我记得我试过这个HttpServletResponseWrapper,但我得到了错误......无论如何,谢谢你! – Viktor

回答

1

您可以按here所述使用HttpServletResponseWrapper

+0

感谢您的回复!我忘了提及(...)我需要REST服务的响应,因为我想操纵它。 – Viktor

+0

请看看[this](https://stackoverflow.com/questions/32829124/adding-header-in-response-in-filter),因为它看起来像你可以使用的东西。 – lzagkaretos

+0

HttpServletResponseWrapper正在工作?然后你的评论是在错误的地方。也许我们应该更新答案以供将来参考。 – lzagkaretos

相关问题