1.加密请求异常后请求/error处理
目的:解决解密时,解密时参数对象类型不同导致抛出异常后接口二次请求到/error的问题
方式:设置异常拦截器,自定义捕获异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @RestControllerAdvice public class GlobalExceptionConfig { private static Logger logger = LogManager.getLogger(GlobalExceptionConfig.class);
@ExceptionHandler(value = Exception.class) public JSONObject AllException(Exception e) { logger.error(e.getMessage()); e.printStackTrace(); String data = EncryptApi(e.getMessage()); JSONObject json = new JSONObject(); json.put("data", data); return json; }
public String EncryptApi(String data) { } }
|
2.HttpServletRequestWrapper处理文件被破坏的问题
1.对文件上传的的请求不进行string读取,进行byte[] 转换
2.不对文件上传加密
3.文件上传有数据时,原路返还
重点是
1
| bodyBytes= StreamUtils.copyToByteArray(inputStream);
|
和
1 2 3 4 5
| if(bodyBytes!=null&&bodyBytes.length>0){ byteArrayInputStream = new ByteArrayInputStream(bodyBytes); }else { byteArrayInputStream = new ByteArrayInputStream(body.getBytes()); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| import org.springframework.http.MediaType; import org.springframework.util.StreamUtils;
import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.io.*; import java.util.*;
public class RequestWrapper extends HttpServletRequestWrapper { private String body; private byte[] bodyBytes; private Map<String, String[]> params = new HashMap<String, String[]>();
public RequestWrapper(HttpServletRequest request) { super(request); StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; InputStream inputStream = null; try { inputStream = request.getInputStream(); String contentType = request.getContentType(); if (contentType!=null&& contentType.startsWith(MediaType.MULTIPART_FORM_DATA_VALUE)) { bodyBytes= StreamUtils.copyToByteArray(inputStream); inputStream= new ByteArrayInputStream(bodyBytes); }
if (inputStream != null) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); char[] charBuffer = new char[128]; int bytesRead = -1; while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { stringBuilder.append(charBuffer, 0, bytesRead); } } else { stringBuilder.append(""); } } catch (IOException ex) {
} finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } this.body = stringBuilder.toString(); this.params.putAll(request.getParameterMap()); }
@Override public Map<String, String[]> getParameterMap() { return params; }
@Override public ServletInputStream getInputStream() throws IOException {
ByteArrayInputStream byteArrayInputStream =null; if(bodyBytes!=null&&bodyBytes.length>0){ byteArrayInputStream = new ByteArrayInputStream(bodyBytes); }else { byteArrayInputStream = new ByteArrayInputStream(body.getBytes()); }
ByteArrayInputStream finalByteArrayInputStream = byteArrayInputStream; ServletInputStream servletInputStream = new ServletInputStream() { @Override public boolean isFinished() { return false; }
@Override public boolean isReady() { return false; }
@Override public void setReadListener(ReadListener readListener) { }
@Override public int read() throws IOException { return finalByteArrayInputStream.read(); } }; return servletInputStream; }
public RequestWrapper(HttpServletRequest request, Map<String, Object> extendParams, String body) { this(request); if (body != null && body.length() > 0) { setBody(body); } if (extendParams.size() > 0) { addAllParameters(extendParams); } }
@Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(this.getInputStream())); }
public String getBody() { return this.body; }
public void setBody(String body) { this.body = body; }
@Override public String getParameter(String name) { String[] values = params.get(name); if (values == null || values.length == 0) { return null; } return values[0]; }
public String[] getParameterValues(String name) { return params.get(name); }
@Override public Enumeration<String> getParameterNames() { ArrayList<String> list = list = new ArrayList<>(); for (Map.Entry<String, String[]> entry : params.entrySet()) { list.add(entry.getKey()); } return Collections.enumeration(list); }
public void addAllParameters(Map<String, Object> otherParams) { for (Map.Entry<String, Object> entry : otherParams.entrySet()) { addParameter(entry.getKey(), entry.getValue()); } }
public void addParameter(String name, Object value) { if (value != null) { if (value instanceof String[]) { params.put(name, (String[]) value); } else if (value instanceof String) { params.put(name, new String[]{(String) value}); } else { params.put(name, new String[]{String.valueOf(value)}); } } }
public byte[] getBodyBytes() { return bodyBytes; }
public void setBodyBytes(byte[] bodyBytes) { this.bodyBytes = bodyBytes; } }
|