长沙网站优化对策,替换wordpress logo,做设计找图有哪些网站有哪些问题,优化营商环境发言材料在做web项目开发时#xff0c;我们有时候需要做一些前置的拦截判断处理#xff0c;比如非法参数校验#xff0c;防攻击拦截#xff0c;统一日志处理等#xff0c;而请求参数如果是form表单提交还好处理#xff1b;对于json这种输入流的数据就会有问题#xff0c;统一处理…在做web项目开发时我们有时候需要做一些前置的拦截判断处理比如非法参数校验防攻击拦截统一日志处理等而请求参数如果是form表单提交还好处理对于json这种输入流的数据就会有问题统一处理如果读取了数据流就会将流进行关闭这就会导致接下来的业务处理无法读取数据流。为了解决这个问题需要将request中的输入流包装为可以重复读取的数据流具体的操作如下 自定义一个类继承HttpServletRequestWrapper并实现它里面的相关方法
import cn.hutool.core.io.IoUtil;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;/*** Author xingo* Date 2024/1/26*/
public class RepeatableReadRequestWrapper extends HttpServletRequestWrapper {private final byte[] body;public RepeatableReadRequestWrapper(HttpServletRequest request) throws IOException {super(request);request.setCharacterEncoding(UTF-8);body IoUtil.readBytes(request.getInputStream());}Overridepublic BufferedReader getReader() throws IOException {return new BufferedReader(new InputStreamReader(getInputStream()));}Overridepublic ServletInputStream getInputStream() throws IOException {final ByteArrayInputStream bis new ByteArrayInputStream(body);return new ServletInputStream() {Overridepublic int read() throws IOException {return bis.read();}Overridepublic boolean isFinished() {return false;}Overridepublic boolean isReady() {return false;}Overridepublic void setReadListener(ReadListener readListener) {}};}}封装成这个类就是为了解决需要重复读取输入流的地方就使用这个包装类替换原有的request对象。再定义一个过滤器用于模拟统一处理请求参数下面就简单模拟在参数中取用户名的过滤器
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;import java.io.IOException;
import java.util.Objects;/*** Author xingo* Date 2024/1/26*/
Order(value Ordered.LOWEST_PRECEDENCE - 1)
Component
WebFilter(filterName paramsFilter, urlPatterns /*)
public class CheckParamsFilter implements Filter {private ServletContext context;static final String checkKey userName;Overridepublic void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig);context filterConfig.getServletContext();}Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {HttpServletRequest request (HttpServletRequest) servletRequest;ServletRequest requestWrapper null;String userName null;String contentType request.getContentType();if(contentType ! null contentType.contains(MediaType.APPLICATION_JSON_VALUE)) {try {// 对于需要读取输入流的先对request进行包装处理这样后续再次需要读取数据流时就可以正常读到requestWrapper new RepeatableReadRequestWrapper(request);JsonNode jsonNode JacksonUtils.getObjectMapper().readTree(requestWrapper.getInputStream());if(jsonNode.get(checkKey) ! null) {userName jsonNode.get(checkKey).asText();}} catch (Exception e) {e.printStackTrace();}} else {try {if(request.getParameter(checkKey) ! null) {userName request.getParameter(checkKey);}} catch (Exception e) {e.printStackTrace();}}if(userName ! null) {// 这里判断用户名检查成功就放行、否则就返回失败信息在放行处理时需要判断是否需要传递包装requestif(this.check(userName)) {chain.doFilter(Objects.requireNonNullElse(requestWrapper, servletRequest), servletResponse);}servletResponse.setContentType(application/json; charsetutf-8);servletResponse.getWriter().print(JacksonUtils.toJSONString(ApiResult.fail(400, 信息验证失败)));return;}chain.doFilter(Objects.requireNonNullElse(requestWrapper, servletRequest), servletResponse);}Overridepublic void destroy() {Filter.super.destroy();}private boolean check(String userName) {return admin.equals(userName);}
}
文章转载自: http://www.morning.mdgb.cn.gov.cn.mdgb.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.rfjmy.cn.gov.cn.rfjmy.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.xgxbr.cn.gov.cn.xgxbr.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.dtgjt.cn.gov.cn.dtgjt.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.pndw.cn.gov.cn.pndw.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.rsfp.cn.gov.cn.rsfp.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.wljzr.cn.gov.cn.wljzr.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.ymwcs.cn.gov.cn.ymwcs.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.prlgn.cn.gov.cn.prlgn.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.mkygc.cn.gov.cn.mkygc.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.hwycs.cn.gov.cn.hwycs.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.bmhc.cn.gov.cn.bmhc.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn