-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Your Name
committed
Jul 27, 2023
1 parent
87afa65
commit 9a51abe
Showing
3 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
eladmin-system/src/main/java/me/zhengjie/config/ResponseWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package me.zhengjie.config; | ||
|
||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.WriteListener; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpServletResponseWrapper; | ||
|
||
|
||
/** | ||
* 返回值输出代理类 | ||
* | ||
* @Title: ResponseWrapper | ||
* @Description: | ||
* @author fei | ||
* @date 2023-07-26 | ||
*/ | ||
public class ResponseWrapper extends HttpServletResponseWrapper | ||
{ | ||
|
||
private ByteArrayOutputStream buffer; | ||
|
||
private ServletOutputStream out; | ||
|
||
public ResponseWrapper(HttpServletResponse httpServletResponse) | ||
{ | ||
super(httpServletResponse); | ||
buffer = new ByteArrayOutputStream(); | ||
out = new WrapperOutputStream(buffer); | ||
} | ||
|
||
@Override | ||
public ServletOutputStream getOutputStream() | ||
throws IOException | ||
{ | ||
return out; | ||
} | ||
|
||
@Override | ||
public void flushBuffer() | ||
throws IOException | ||
{ | ||
if (out != null) | ||
{ | ||
out.flush(); | ||
} | ||
} | ||
|
||
public byte[] getContent() | ||
throws IOException | ||
{ | ||
flushBuffer(); | ||
return buffer.toByteArray(); | ||
} | ||
|
||
class WrapperOutputStream extends ServletOutputStream | ||
{ | ||
private ByteArrayOutputStream bos; | ||
|
||
public WrapperOutputStream(ByteArrayOutputStream bos) | ||
{ | ||
this.bos = bos; | ||
} | ||
|
||
@Override | ||
public void write(int b) | ||
throws IOException | ||
{ | ||
bos.write(b); | ||
} | ||
|
||
@Override | ||
public boolean isReady() | ||
{ | ||
|
||
// TODO Auto-generated method stub | ||
return false; | ||
|
||
} | ||
|
||
@Override | ||
public void setWriteListener(WriteListener arg0) | ||
{ | ||
|
||
// TODO Auto-generated method stub | ||
|
||
} | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...-system/src/main/java/me/zhengjie/modules/security/security/PromethuseResponseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package me.zhengjie.modules.security.security; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
|
||
import me.zhengjie.config.ResponseWrapper; | ||
|
||
public class PromethuseResponseFilter extends OncePerRequestFilter { | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse)response);//转换成代理类 | ||
filterChain.doFilter(request, wrapperResponse); | ||
|
||
byte[] content = wrapperResponse.getContent();//获取返回值 | ||
if (content.length > 0) { | ||
String jsonString = new String(content); | ||
Object plaObject = JSON.parse(jsonString); | ||
//把返回值输出到客户端 | ||
ServletOutputStream out = response.getOutputStream(); | ||
// Prometheus actuator endpoint should produce a text/plain response | ||
// https://github.com/spring-projects/spring-boot/issues/28446 | ||
response.setContentType("text/plain"); | ||
out.write(plaObject.toString().getBytes()); | ||
out.flush(); | ||
out.close(); | ||
} | ||
|
||
} | ||
} |