2017-08-10 333 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

相關問題