I'd like to alter the content of the response in a Spring Security filter. Let's say all I want is as follows:
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();
}
}
Behind the filter a REST service resides that retrieves a List of Strings. If I use the getOutputStream() to write then I can the List of Strings on client (and not the String 'Hello'). If I use the getWriter() then I get the following error:
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
How can I modify the response content in Spring Security filter? BTW I use wildfly10 but it should work on Tomcat and Weblogic12c too. I use Spring Boot.
The relevant part from securityContext.xml:
<security:csrf disabled="true" />
<security:custom-filter ref="securityFilter" after="FORM_LOGIN_FILTER"/>
</security:http>
I presume the response is already sent when I want to write my content but what can I do about that?
Any response would be highly appreciated!
Thanks, V.
-----UPDATE------ I forgot to mention that I need the response from the REST service as I want to manipulate it.