英文:
How to add HTTP Response Header by a value of Response Payload in Spring Boot
问题
我想在我的Spring Boot项目中,在Response生成之后,通过filter
或类似的方式设置HttpServletResponse
的头部。在过滤器中,我可以进行类似以下的操作:
((HttpServletResponse)servletResponse).addHeader("test","value");
fc.doFilter(request, servletResponse);
但问题是,我的头部值是根据响应体(payload)本身生成的,因此我必须使用ServletResponse
负载的内容将其添加到头部。我该如何实现?
我可以在控制器级别上实现它,但我不想为所有的响应手动执行此操作。
英文:
I want to set the header of HttpServletResponse
in filter
or something similar after the Response is generated in my spring boot project. In filter, I can do something similar:
((HttpServletResponse)servletResponse).addHeader("test","value");
fc.doFilter(request, servletResponse);
But the problem is that my header value is generated from the response body(payload) itself, so I have to use the content of ServletResponse
payload to add it to the header. How can I do it?
I could do it on controller level but I don't want to do it manually for all the responses.
答案1
得分: 1
我在过滤器中实现了ContentCachingResponseWrapper
,以获取响应主体并从ServletResponse
中添加头,如下所示:
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ContentCachingResponseWrapper responseCacheWrapperObject = new ContentCachingResponseWrapper((HttpServletResponse) servletResponse);
filterChain.doFilter(servletRequest, responseCacheWrapperObject);
byte[] responseArray = responseCacheWrapperObject.getContentAsByteArray();
String responseStr = new String(responseArray, responseCacheWrapperObject.getCharacterEncoding());
//这是我自己的逻辑函数,从responseStr生成头部值
String headerValue = getHeaderValueFromResponseString(responseStr);
responseCacheWrapperObject.addHeader("headerKey", headerValue)
responseCacheWrapperObject.copyBodyToResponse();
}
英文:
I implemented the ContentCachingResponseWrapper
in the Filter to get the response body and to add the header from ServletResponse
as follow:
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ContentCachingResponseWrapper responseCacheWrapperObject = new ContentCachingResponseWrapper((HttpServletResponse) servletResponse);
filterChain.doFilter(servletRequest, responseCacheWrapperObject);
byte[] responseArray = responseCacheWrapperObject.getContentAsByteArray();
String responseStr = new String(responseArray, responseCacheWrapperObject.getCharacterEncoding());
//This is my own logic function to generate header value from responseStr
String headerValue = getHeaderValueFromResponseString(responseStr);
responseCacheWrapperObject.addHeader("headerKey", headerValue)
responseCacheWrapperObject.copyBodyToResponse();
}
答案2
得分: 0
你可以使用这段代码:
String authorizationHeader = request.getHeader("Authorization");
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
token = authorizationHeader.substring(7);
userName = jwtUtil.extractUsername(token);
request.setAttribute("userid", userName);
}
所以现在这段代码生成一个令牌,其中包括用户名(userName)以及令牌本身。
英文:
You can make use of this code
String authorizationHeader = request.getHeader("Authorization");
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
token = authorizationHeader.substring(7);
userName = jwtUtil.extractUsername(token);
request.setAttribute("userid",userName);
}
so now this code generates a token where you have userid that gives userName along with the token
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论