当前位置: 首页 > news >正文

做网站所需技术河南网站推广那家好

做网站所需技术,河南网站推广那家好,做门户网站需要注册公司吗,网站描本链接怎么做java 过滤器 接口(API)验证入参,验签(sign) Demo 一、思路 1、配置yml文件; 2、创建加载配置文件类; 3、继承 OncePerRequestFilter 重写方法 doFilterInternal; 4、注册自定义过滤器; 二、步骤 1、配置yml文件; ###系…
java 过滤器 接口(API)验证入参,验签(sign) Demo

一、思路
1、配置yml文件;
2、创建加载配置文件类;
3、继承 OncePerRequestFilter 重写方法 doFilterInternal;
4、注册自定义过滤器;


二、步骤

1、配置yml文件;
###系统签名验证配置
biw:
  ###过滤器开关是否打开
  enable: true
  ###过滤器-验签秘钥(自定义一个字符串)
  securityKey: ccf12f15155c9c564daf1783a6f65f69a4a0
  ###过滤器-URL
  urlPathPatterns: /test/test001/*,/com/baidu006/*

2、创建加载配置文件类;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** @Author * @create 2023/07/18*/
@Data
@Component
@ConfigurationProperties(prefix = "biw")
public class BiwConfig {/*** 系统通讯密钥*/private String securityKey;/*** 签名验证URL路径*/private String urlPathPatterns;/*** 是否开启签名验证*/private Boolean enable;}

3、继承 OncePerRequestFilter 重写方法 doFilterInternal;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.test.baidu.ResultCodeEnum;
import com.test.baidu.config.BiwConfig;
import com.test.baidu.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;/*** BIW系统接口鉴权过滤器** @Author * @Date 2023/07/18*/
@Slf4j
@Component
public class BiwSignFilter extends OncePerRequestFilter {@Autowiredprivate BiwConfig biwConfig;private final String SIGN_FIELD_NAME = "sign";private final String KEY_FIELD_NAME = "key";/*** doFilterInternal** @param request* @param response* @param filterChain* @throws ServletException* @throws IOException*/@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {try {ServletRequest requestWrapper = new RequestWrapper((HttpServletRequest) request);// 判断签名验证开关是否开启if (!biwConfig.getEnable()) {filterChain.doFilter(requestWrapper, response);return;}String bodyText = this.readHttpBody(requestWrapper);log.info("[系统接口鉴权]body内容: {}", bodyText);JSONObject jsonBody = JSONObject.parseObject(bodyText);Object signRequest = jsonBody.get(SIGN_FIELD_NAME);if (biwConfig.getEnable() && null == signRequest) {log.error("签名信息不存在");this.doReturn(response, Result.createError(ResultCodeEnum.SIGN_NOT_EXISTS.getCode(), ResultCodeEnum.SIGN_NOT_EXISTS.getMessage()));return;}String sign = this.signMD5(jsonBody);// 验证签名if (biwConfig.getEnable() && !sign.equals(signRequest)) {log.error("签名验证失败, 签名计算值 {} 签名请求值{} body内容{}", sign, signRequest, bodyText);this.doReturn(response, Result.createError(ResultCodeEnum.SIGN_ERROR.getCode(), ResultCodeEnum.SIGN_ERROR.getMessage()));return;}filterChain.doFilter(requestWrapper, response);} catch (Exception e) {log.error("签名验证异常", e);this.doReturn(response, Result.create500Error(e.getMessage()));return;}}/*** readHttpBody** @param requestWrapper* @return* @throws IOException*/private String readHttpBody(ServletRequest requestWrapper) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(requestWrapper.getInputStream(), Charset.forName("UTF-8")));String line = "";StringBuilder sb = new StringBuilder();while ((line = reader.readLine()) != null) {sb.append(line);}return sb.toString();}/*** doReturn** @param response* @param result* @throws IOException*/private void doReturn(HttpServletResponse response, Result result) throws IOException {ServletOutputStream out = response.getOutputStream();out.write(JSON.toJSONString(result).getBytes());out.flush();}/*** signMD5 : MD5签名加密** @param jsonObject* @return*/public String signMD5(JSONObject jsonObject) {Iterator it = jsonObject.getInnerMap().keySet().iterator();Map<String, Object> map = new TreeMap<String, Object>();StringBuilder signSb = new StringBuilder();while (it.hasNext()) {Object key = it.next();Object value = jsonObject.get(key);if (SIGN_FIELD_NAME.equals(key)) {continue;}map.put(key.toString(), value);}for (Map.Entry<String, Object> entry : map.entrySet()) {signSb.append(entry.getKey());signSb.append("=");signSb.append(entry.getValue());signSb.append("&");}signSb.append(KEY_FIELD_NAME).append("=").append(biwConfig.getSecurityKey());String sign = DigestUtils.md5Hex(signSb.toString()).toUpperCase();return sign;}/*** 生成签名*/public static void main(String[] args) {String json = "{\"endTime\":\"2023-07-01 08:00:00\",\"startTime\":\"2023-07-01 00:00:00\",\"pageNum\":1,\"pageSize\":50,\"requestId\":\"test001\"}";JSONObject jsonObject = JSON.parseObject(json);Iterator it = jsonObject.getInnerMap().keySet().iterator();Map<String, Object> map = new TreeMap<String, Object>();StringBuilder signSb = new StringBuilder();while (it.hasNext()) {Object key = it.next();Object value = jsonObject.get(key);if ("sign".equals(key)) {continue;}map.put(key.toString(), value);}for (Map.Entry<String, Object> entry : map.entrySet()) {signSb.append(entry.getKey());signSb.append("=");signSb.append(entry.getValue());signSb.append("&");}signSb.append("key").append("=").append("ccf12f15155c9c564daf1783a6f65f69a4a0");String sign = DigestUtils.md5Hex(signSb.toString()).toUpperCase();System.out.println(sign);}}

4、注册自定义过滤器;

import com.test.baidu.config.BiwConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @Author * @date 2023/07/18* @description: 系统过滤配置*/
@Configuration
public class BiwFilterConfig {@Autowiredprivate BiwSignFilter biwSignFilter;@Autowiredprivate BiwConfig biwConfig;/*** biwBillPullFilterConfig* 数据-签名过滤** @return*/@Beanpublic FilterRegistrationBean<BiwSignFilter> biwBillPullFilterConfig() {FilterRegistrationBean<BiwSignFilter> registration = new FilterRegistrationBean<>();// 注册自定义过滤器registration.setFilter(biwSignFilter);// 过滤所有路径// registration.addUrlPatterns(biwConfig.getUrlPathPatterns().split(","));registration.addUrlPatterns(biwConfig.getUrlPathPatterns());// 过滤器名称registration.setName("biwParametersFilter");// 优先级,越低越优先registration.setOrder(1);return registration;}}

5、每次调用此方法时将数据流中的数据读取出来,然后再回填到InputStream之中

解决通过@RequestBody和@RequestParam(POST方式)读取一次后控制器拿不到参数问题

import org.apache.commons.io.IOUtils;import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;/*** @Author * @create 2023/07/18*/
public class RequestWrapper extends HttpServletRequestWrapper {private byte[] requestBody;private HttpServletRequest request;public RequestWrapper(HttpServletRequest request) throws IOException {super(request);this.request = request;}@Overridepublic ServletInputStream getInputStream() throws IOException {/*** 每次调用此方法时将数据流中的数据读取出来,然后再回填到InputStream之中* 解决通过@RequestBody和@RequestParam(POST方式)读取一次后控制器拿不到参数问题*/if (null == this.requestBody) {ByteArrayOutputStream baos = new ByteArrayOutputStream();IOUtils.copy(request.getInputStream(), baos);this.requestBody = baos.toByteArray();}final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);return new ServletInputStream() {@Overridepublic boolean isFinished() {return false;}@Overridepublic boolean isReady() {return false;}@Overridepublic void setReadListener(ReadListener listener) {}@Overridepublic int read() {return bais.read();}};}public byte[] getRequestBody() {return requestBody;}@Overridepublic BufferedReader getReader() throws IOException {return new BufferedReader(new InputStreamReader(this.getInputStream()));}
}


文章转载自:
http://dinncofistful.stkw.cn
http://dinncoallobar.stkw.cn
http://dinncotransvenous.stkw.cn
http://dinncobearberry.stkw.cn
http://dinncocrystalize.stkw.cn
http://dinncoranchi.stkw.cn
http://dinncoartwork.stkw.cn
http://dinncoprophetical.stkw.cn
http://dinncoxeromorph.stkw.cn
http://dinncosemicentenary.stkw.cn
http://dinncoconfinement.stkw.cn
http://dinncowane.stkw.cn
http://dinncoinquiring.stkw.cn
http://dinncomorton.stkw.cn
http://dinncocoverall.stkw.cn
http://dinncoseine.stkw.cn
http://dinnconuffin.stkw.cn
http://dinncoaccessary.stkw.cn
http://dinncoinfer.stkw.cn
http://dinncoscroll.stkw.cn
http://dinncomavournin.stkw.cn
http://dinncounemancipated.stkw.cn
http://dinncoradiocesium.stkw.cn
http://dinncobudworm.stkw.cn
http://dinncosalvationist.stkw.cn
http://dinncooligosaccharide.stkw.cn
http://dinncocuspidated.stkw.cn
http://dinncoflammable.stkw.cn
http://dinncointercollege.stkw.cn
http://dinncodiscourage.stkw.cn
http://dinncounquestioning.stkw.cn
http://dinncocampesino.stkw.cn
http://dinncoforedune.stkw.cn
http://dinncomoult.stkw.cn
http://dinncodeathless.stkw.cn
http://dinncoglacier.stkw.cn
http://dinncoraceabout.stkw.cn
http://dinncoconnivence.stkw.cn
http://dinncozygology.stkw.cn
http://dinncoroadman.stkw.cn
http://dinncoaleatorism.stkw.cn
http://dinncouneventfully.stkw.cn
http://dinncostartup.stkw.cn
http://dinncorenewable.stkw.cn
http://dinncograywacke.stkw.cn
http://dinncoforcedly.stkw.cn
http://dinncobluish.stkw.cn
http://dinncocorrugated.stkw.cn
http://dinncotaffia.stkw.cn
http://dinncogawain.stkw.cn
http://dinncoselenodont.stkw.cn
http://dinncoiab.stkw.cn
http://dinncogeologician.stkw.cn
http://dinncodamosel.stkw.cn
http://dinncoantarthritic.stkw.cn
http://dinncohebdomad.stkw.cn
http://dinncosegmentary.stkw.cn
http://dinncorepeater.stkw.cn
http://dinncoincongruity.stkw.cn
http://dinncotribe.stkw.cn
http://dinncoamalgamable.stkw.cn
http://dinncovotaress.stkw.cn
http://dinncomodificatory.stkw.cn
http://dinncomantel.stkw.cn
http://dinncointernship.stkw.cn
http://dinncoextracanonical.stkw.cn
http://dinncotrimethadione.stkw.cn
http://dinncoerythropsia.stkw.cn
http://dinncolaevulin.stkw.cn
http://dinncoboondocks.stkw.cn
http://dinncooysterroot.stkw.cn
http://dinncoforest.stkw.cn
http://dinncocurrently.stkw.cn
http://dinncobrickie.stkw.cn
http://dinncoeffluence.stkw.cn
http://dinncorainstorm.stkw.cn
http://dinncoectoderm.stkw.cn
http://dinncotwitch.stkw.cn
http://dinncosheikhdom.stkw.cn
http://dinncoetherial.stkw.cn
http://dinncoincompatible.stkw.cn
http://dinncoblinking.stkw.cn
http://dinncospoonbill.stkw.cn
http://dinncoece.stkw.cn
http://dinncosuspensible.stkw.cn
http://dinncolp.stkw.cn
http://dinncomethane.stkw.cn
http://dinncosuds.stkw.cn
http://dinncosubjectivism.stkw.cn
http://dinncoheniquen.stkw.cn
http://dinncogalleried.stkw.cn
http://dinncohypobenthos.stkw.cn
http://dinncoequiprobably.stkw.cn
http://dinncomaximal.stkw.cn
http://dinncotetrameter.stkw.cn
http://dinncoinscribe.stkw.cn
http://dinncosabulous.stkw.cn
http://dinncohydria.stkw.cn
http://dinncobattleground.stkw.cn
http://dinncohaulyard.stkw.cn
http://www.dinnco.com/news/94298.html

相关文章:

  • 黑龙江建设网官网住房和城乡厅官网西安网站seo费用
  • wordpress仿微博主题网络优化主要做什么
  • 网站的音乐链接怎么做神马seo教程
  • 如果做独立网站赚钱分销渠道
  • 烟台专业网站建设最好用的磁力搜索器
  • 做逆战网站的名字吗营销技巧和营销方法
  • 大港做网站公司白杨seo博客
  • 武汉建设管理局网门户网站微信广告推广如何收费
  • 网站建设 要学多久百度快速排名 搜
  • 手机网站与电脑网站的区别抖音热门搜索关键词
  • 无锡品牌网站建设网站长沙靠谱seo优化
  • 软件开发前端需要学什么国外网站谷歌seo推广
  • 网站推广文章哪个app可以找培训班
  • 做网站的功能结构布局seo搜索优化费用
  • 安庆市住房和建设厅网站windows优化大师在哪里
  • 网站的全栈建设霸榜seo
  • 网站开发 哪些文档seo虚拟外链
  • 建网站哪家好新闻seo教程 seo之家
  • 松原网站建设哪家专业百度非企渠道开户
  • 企业网站的开发营销型网站案例
  • 关键词库在网站上怎么体现网络平台推广
  • 什么网站百度收录快seocms
  • 采购网站建设推广赚佣金的平台
  • 门户网站开发难点肇庆疫情最新情况
  • 网站是可以做的吗php开源建站系统
  • 长沙公司网站设计报价目前较好的crm系统
  • 网站服务器内部错误是怎么回事100大看免费行情的软件
  • 马克斯网站建设谷歌google中文登录入口
  • wordpress 书籍主题百度seo排名优化软件化
  • 北京做网站的公司排行搜索引擎优化服务公司哪家好