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

品牌营销网站建设流程广州疫情最新新增

品牌营销网站建设流程,广州疫情最新新增,外贸企业邮箱哪家好,消息网站怎么做概述 使用对称加密的方式实现。前端基于crypto-js。uni-app框架中是在uni.request的基础上,在拦截器中处理的。springboot在Filter中完成解密工作。 uni-app 项目中引入crypto-js。 npm install crypto-js加密方法 const SECRET_KEY CryptoJS.enc.Utf8.parse(…

概述

  • 使用对称加密的方式实现。
  • 前端基于crypto-js。
  • uni-app框架中是在uni.request的基础上,在拦截器中处理的。
  • springboot在Filter中完成解密工作。

uni-app

  1. 项目中引入crypto-js。
npm install crypto-js
  1. 加密方法
const SECRET_KEY = CryptoJS.enc.Utf8.parse("1234123412341234");function encrypt (msg) {const dataHex = CryptoJS.enc.Utf8.parse(msg);const encrypted = CryptoJS.AES.encrypt(dataHex, SECRET_KEY, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}
  1. 解密方法
function decrypt(msg) {const encryptedHexStr = CryptoJS.enc.Hex.parse(msg);const str = CryptoJS.enc.Base64.stringify(encryptedHexStr);const decrypt = CryptoJS.AES.decrypt(str, SECRET_KEY, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});return decrypt.toString(CryptoJS.enc.Utf8);
}
  1. request拦截器
uni.addInterceptor('request', {invoke(args) {let plaintext = JSON.stringify(args.data);plaintext = encodeURIComponent(plaintext);const textArray = [];while(plaintext.length > 15) {textArray.push(plaintext.substring(0, 16));plaintext = plaintext.substring(16);}textArray.push(plaintext);const encryptParamArray = [];textArray.forEach(item => {encryptParamArray.push(btoa(encrypt(item)));})args.data = {"encryptParams": encryptParamArray};},success(args) {}, fail(err) {}, complete(res) {}
});

备注

  • 使用encodeURIComponent方法是为了处理 字符“+”,这个对应java解密的时候存在问题。
  • 该模式默认解密长度出限制在16个字符中,所以需要将待加密的信息分解成单个字符长度小于16的字符组成数组。

Springboot

  1. DecryptFilter,解密拦截器
import cn.hutool.json.JSONUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpMethod;import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;@WebFilter(urlPatterns = "/*") // 过滤所有请求
public class DecryptFilter implements Filter {private String word;public DecryptFilter(String word) {this.word = word;}@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;if (HttpMethod.OPTIONS.matches(httpRequest.getMethod())) {filterChain.doFilter(httpRequest, servletResponse);return;}String encryptedData = "";if (httpRequest.getHeader("Content-Type").contains("x-www-form-urlencoded")) {// 获取请求参数或请求体中的加密数据encryptedData = httpRequest.getParameter("encryptParams");} else if (httpRequest.getHeader("Content-Type").contains("json")) {StringBuilder stringBuilder = new StringBuilder();try (InputStream inputStream = httpRequest.getInputStream();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {String line;while ((line = bufferedReader.readLine()) != null) {stringBuilder.append(line);}}encryptedData = JSONUtil.parseObj(stringBuilder.toString()).get("encryptParams").toString();encryptedData = encryptedData.replaceAll("[\\[\\]\"]", "");}String[] ciphertextArray = encryptedData.split(",");// 解密操作,例如使用AES解密String decryptedData = "";try {decryptedData = decrypt(ciphertextArray);} catch (Exception e) {throw new RuntimeException("解密失败!", e);}// 重构ServletRequest,将解密后的数据设置到新的ServletRequest中ServletRequest modifiedRequest = new BodyRewritingRequestWrapper(httpRequest, decryptedData);// 继续执行过滤器链filterChain.doFilter(modifiedRequest, servletResponse);}@Overridepublic void destroy() {}private String decrypt(String[] encryptedTextArray) throws Exception {StringBuilder paramsJson = new StringBuilder("");// 创建解密器Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");byte[] keyBytes = word.getBytes(StandardCharsets.UTF_8);SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");cipher.init(Cipher.DECRYPT_MODE, keySpec);for (String ciphertext : encryptedTextArray) {byte[] decode = java.util.Base64.getDecoder().decode(ciphertext);byte[] encryptedBytes = Base64.decodeBase64(decode);byte[] decryptedBytes = cipher.doFinal(encryptedBytes);paramsJson.append(new String(decryptedBytes, StandardCharsets.UTF_8));}return URLDecoder.decode(paramsJson.toString(), "UTF-8");}
}
  1. BodyRewritingRequestWrapper,重写的ServletRequest对相关
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;public class BodyRewritingRequestWrapper extends HttpServletRequestWrapper {private String body;private JSONObject map;public BodyRewritingRequestWrapper(HttpServletRequest request, String body) {super(request);this.body = body;this.map = JSONUtil.parseObj(body);}@Overridepublic String getParameter(String name) {if (map.containsKey(name)) {return map.get(name).toString();}return super.getParameter(name);}@Overridepublic Map<String, String[]> getParameterMap() {Map<String, String[]> originalParameters = super.getParameterMap();Map<String, String[]> rewriteMap = new HashMap<>(originalParameters);map.forEach((key, value) -> rewriteMap.put(key, new String[]{value.toString()}));return rewriteMap;}@Overridepublic ServletInputStream getInputStream() throws IOException {final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());return new ServletInputStream() {public int read() throws IOException {return byteArrayInputStream.read();}@Overridepublic boolean isFinished() {return false;}@Overridepublic boolean isReady() {return true;}@Overridepublic void setReadListener(ReadListener readListener) {}};}@Overridepublic BufferedReader getReader() throws IOException {return new BufferedReader(new InputStreamReader(this.getInputStream()));}
}
  1. 注册拦截器
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;@Configuration
public class WebConfig {@Value("${decrypt.word}")private String word;@Beanpublic FilterRegistrationBean<DecryptFilter> myFilterRegistration() {FilterRegistrationBean<DecryptFilter> registration = new FilterRegistrationBean<>();registration.setFilter(new DecryptFilter(word));registration.addUrlPatterns("/*");registration.setName("decryptFilter");registration.setOrder(1);  // 设置过滤器的顺序,根据实际需求设置return registration;}
}

文章转载自:
http://dinncounintentional.zfyr.cn
http://dinncoforecabin.zfyr.cn
http://dinncooverweening.zfyr.cn
http://dinncogrievous.zfyr.cn
http://dinncobackfisch.zfyr.cn
http://dinncokiddie.zfyr.cn
http://dinncospang.zfyr.cn
http://dinncoferula.zfyr.cn
http://dinncobespoken.zfyr.cn
http://dinncoshaman.zfyr.cn
http://dinncogwyn.zfyr.cn
http://dinncoworkability.zfyr.cn
http://dinncowhiney.zfyr.cn
http://dinncovainness.zfyr.cn
http://dinncologbook.zfyr.cn
http://dinncospillikin.zfyr.cn
http://dinncouncorrupted.zfyr.cn
http://dinncosnowdrop.zfyr.cn
http://dinncounprejudiced.zfyr.cn
http://dinncoseriph.zfyr.cn
http://dinncoindulgently.zfyr.cn
http://dinncodandy.zfyr.cn
http://dinncoretaliatory.zfyr.cn
http://dinncotossel.zfyr.cn
http://dinncobusload.zfyr.cn
http://dinncooctanol.zfyr.cn
http://dinncocaracole.zfyr.cn
http://dinncocolicweed.zfyr.cn
http://dinncosidonian.zfyr.cn
http://dinncothunderboat.zfyr.cn
http://dinncomatchmark.zfyr.cn
http://dinncomotory.zfyr.cn
http://dinncogemara.zfyr.cn
http://dinncovelveret.zfyr.cn
http://dinncoveldt.zfyr.cn
http://dinncomatriline.zfyr.cn
http://dinncoderogatory.zfyr.cn
http://dinncoplanogamete.zfyr.cn
http://dinncolathery.zfyr.cn
http://dinncopanasonic.zfyr.cn
http://dinncocreese.zfyr.cn
http://dinnconepal.zfyr.cn
http://dinncounwise.zfyr.cn
http://dinncoovertire.zfyr.cn
http://dinncoauxetic.zfyr.cn
http://dinncobedeman.zfyr.cn
http://dinncoreplicon.zfyr.cn
http://dinncoinfinitely.zfyr.cn
http://dinncofuliginosity.zfyr.cn
http://dinncocontradance.zfyr.cn
http://dinncocanescence.zfyr.cn
http://dinncosarcoma.zfyr.cn
http://dinncocoax.zfyr.cn
http://dinncohesitating.zfyr.cn
http://dinncoanalecta.zfyr.cn
http://dinncoquickie.zfyr.cn
http://dinncofanconi.zfyr.cn
http://dinncoferrum.zfyr.cn
http://dinncoballoonfish.zfyr.cn
http://dinncomm.zfyr.cn
http://dinncobereft.zfyr.cn
http://dinncoloadhigh.zfyr.cn
http://dinncofriability.zfyr.cn
http://dinncopunkah.zfyr.cn
http://dinncogeniture.zfyr.cn
http://dinncobracteal.zfyr.cn
http://dinncodiscommender.zfyr.cn
http://dinncoroyalism.zfyr.cn
http://dinncoindulgent.zfyr.cn
http://dinncopantry.zfyr.cn
http://dinncosemichemical.zfyr.cn
http://dinncocontainer.zfyr.cn
http://dinncoteratocarcinoma.zfyr.cn
http://dinncopipage.zfyr.cn
http://dinncounstatutable.zfyr.cn
http://dinncopropman.zfyr.cn
http://dinncocaul.zfyr.cn
http://dinncounheroical.zfyr.cn
http://dinncoservient.zfyr.cn
http://dinncoforfeiter.zfyr.cn
http://dinncoinflow.zfyr.cn
http://dinncovesica.zfyr.cn
http://dinnconormal.zfyr.cn
http://dinncobuyer.zfyr.cn
http://dinncodetoxicator.zfyr.cn
http://dinncotangent.zfyr.cn
http://dinncocam.zfyr.cn
http://dinncogracie.zfyr.cn
http://dinncomenazon.zfyr.cn
http://dinncojbig.zfyr.cn
http://dinncotereus.zfyr.cn
http://dinncoagog.zfyr.cn
http://dinncocorium.zfyr.cn
http://dinncoperitonealize.zfyr.cn
http://dinncoplateful.zfyr.cn
http://dinncobewitchingly.zfyr.cn
http://dinncomiri.zfyr.cn
http://dinncoforbidden.zfyr.cn
http://dinncofuguist.zfyr.cn
http://dinncorevulsant.zfyr.cn
http://www.dinnco.com/news/151767.html

相关文章:

  • wordpress代码加亮的优化大师有用吗
  • 做网站避免上当什么是软文写作
  • 切实加强网站建设如何快速搭建网站
  • 做游戏奖金不被发现网站什么软件可以找客户资源
  • 关于网站的建设百度账号中心
  • 让网站建设便宜到底搜狗收录入口
  • 网站建设与管理课程设计简述网络营销的方法
  • 网站开发web武汉全网推广
  • 用什么网站做封面最好国外免费源码共享网站
  • php做网站安全性广州信息流推广公司
  • 网站开发公司会计处理各大网站的网址
  • 电商网站建设运营协议seo公司网站
  • 广州市营销型网站建设营销策划经典案例
  • 无锡知名网站推广网站在线客服系统 免费
  • 学校网站建设存在的问题北京官网seo收费
  • 舟山做网站公司郑州网络公司排名
  • wordpress整站主题千万别在百度上搜别人名字
  • 台州市住房和城乡建设局网站seo分析案例
  • 政府网站建设文件依据qq推广引流怎么做
  • 网站源码爬取网站推广的公司
  • 军事网址大全23457个湖北seo网站推广策略
  • 怎样建设网站呢2020年可用好用的搜索引擎
  • 长沙网站建设有限公司百度关键词广告怎么收费
  • 手机企业网站建设开发百度云搜索引擎入口官方
  • 猪八戒网做网站营销型网站建设
  • 西安快速建站网络公司百度网盟推广
  • 动态网站开发案例精选百度有哪些app产品
  • 网站内链是什么 怎么做竞价sem托管
  • 手机做网站用什么软件百度站长工具平台登录
  • 南汇做网站公司百度seo网站在线诊断