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

丹阳高铁站对面的规划知识付费小程序搭建

丹阳高铁站对面的规划,知识付费小程序搭建,个人简介网站怎么做,做媛网站背景 最近项目要有向外部提供服务的能力,但是考虑到数据安全问题,要对接口进行加解密;实现加解密的方案有很多,比如过滤器、拦截器、继承RequestResponseBodyMethodProcessor什么的,不过我最近正在了解ResponseBodyAd…

背景

最近项目要有向外部提供服务的能力,但是考虑到数据安全问题,要对接口进行加解密;实现加解密的方案有很多,比如过滤器、拦截器、继承RequestResponseBodyMethodProcessor什么的,不过我最近正在了解@ResponseBodyAdvice @RequestBodyAdvice这俩注解,本着在实践中应用的目的,就准备使用这两个注解来实现加解密功能。
然而,配置好后,请求怎么都进不到这两个注解的类里。摸索了一天的时间,@RestController 和@ResponseBody 都加了,也确认已经扫描进容器中管理了,可就是无法生效。

原因

后来发现项目中之前有对所有的controller进行返回结果的统一包装,使用的是继承RequestResponseBodyMethodProcessor类来实现;
刚刚@ResponseBodyAdvice和@RequestBodyAdvice一直无法生效,就在RequestResponseBodyMethodProcessor这里面做了加密的动作,后来不经意间,把这个类在WebMvcConfigurer中导入的代码注掉了,惊奇的发现@ResponseBodyAdvice @RequestBodyAdvice这俩注解生效了。
所以初步定位 @ResponseBodyAdvice @RequestBodyAdvice 和RequestResponseBodyMethodProcessor 会冲突导致不生效。

解决

RequestResponseBodyMethodProcessor 里的逻辑抽取到@ResponseBodyAdvice里,本来这个也是对返回结果进行增强的,所以放到这里也非常合理。
同时扩展了加密的逻辑。

核心代码


@ControllerAdvice
public class ResponseProcessor implements ResponseBodyAdvice<Object> {private ObjectMapper om = new ObjectMapper();@AutowiredEncryptProperties encryptProperties;@Overridepublic boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {return methodParameter.hasMethodAnnotation(Encrypt.class);}@Overridepublic Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {byte[] keyBytes = encryptProperties.getKey().getBytes();try {if(!methodParameter.hasMethodAnnotation(NoResponseWrapperAnnotation.class)){body = new ResponseWrapper<>(body);}body = AESUtils.encrypt(JSONObject.toJSONString(body),encryptProperties.getKey());} catch (Exception e) {e.printStackTrace();}return body;}
}
``````java
@ControllerAdvice
public class RequestProcessor extends RequestBodyAdviceAdapter {@Autowiredprivate EncryptProperties encryptProperties;@Overridepublic boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {return methodParameter.hasMethodAnnotation(Decrypt.class) || methodParameter.hasParameterAnnotation(Decrypt.class);}@Overridepublic HttpInputMessage beforeBodyRead(final HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {byte[] body = new byte[inputMessage.getBody().available()];inputMessage.getBody().read(body);try {String decrypt = AESUtils.decrypt(new String(body), encryptProperties.getKey());final ByteArrayInputStream bais = new ByteArrayInputStream(decrypt.getBytes());return new HttpInputMessage() {@Overridepublic InputStream getBody() throws IOException {return bais;}@Overridepublic HttpHeaders getHeaders() {return inputMessage.getHeaders();}};} catch (Exception e) {e.printStackTrace();}return super.beforeBodyRead(inputMessage, parameter, targetType, converterType);}
}
``````java
public class AESUtils {private static final String KEY_ALGORITHM = "AES";private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法public static String getKey(int len){if(len % 16 != 0){System.out.println("长度要为16的整数倍");return null;}char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();char[] uuid = new char[len];if (len > 0) {for (int i = 0; i < len; i++) {int x = (int) (Math.random() * (len - 0 + 1) + 0);uuid[i] = chars[x % chars.length];}}return new String(uuid);}public static String byteToHexString(byte[] bytes){StringBuffer sb = new StringBuffer();for (int i = 0; i < bytes.length; i++) {String strHex=Integer.toHexString(bytes[i]);if(strHex.length() > 3){sb.append(strHex.substring(6));} else {if(strHex.length() < 2){sb.append("0" + strHex);} else {sb.append(strHex);}}}return  sb.toString();}/*** AES 加密操作** @param content 待加密内容* @param key 加密密码* @return 返回Base64转码后的加密数据*/public static String encrypt(String content, String key) {try {Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器byte[] byteContent = content.getBytes("utf-8");cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));// 初始化为加密模式的密码器byte[] result = cipher.doFinal(byteContent);// 加密return org.apache.commons.codec.binary.Base64.encodeBase64String(result);//通过Base64转码返回} catch (Exception ex) {ex.printStackTrace();}return null;}/*** AES 解密操作** @param content* @param key* @return*/public static String decrypt(String content, String key) {try {//实例化Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);//使用密钥初始化,设置为解密模式cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));//执行操作byte[] result = cipher.doFinal(org.apache.commons.codec.binary.Base64.decodeBase64(content));return new String(result, "utf-8");} catch (Exception ex) {ex.printStackTrace();}return null;}private static SecretKeySpec getSecretKey(final String key) throws UnsupportedEncodingException {//返回生成指定算法密钥生成器的 KeyGenerator 对象
//        KeyGenerator kg = null;//            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
//
//            //AES 要求密钥长度为 128
//            kg.init(128, new SecureRandom(key.getBytes()));
//
//            //生成一个密钥
//            SecretKey secretKey = kg.generateKey();return new SecretKeySpec(Arrays.copyOf(key.getBytes("utf-8"), 16), KEY_ALGORITHM);// 转换为AES专用密钥}
}
```

文章转载自:
http://dinncoliverwort.zfyr.cn
http://dinnconecrophagy.zfyr.cn
http://dinncoarmpad.zfyr.cn
http://dinncohatha.zfyr.cn
http://dinncoantianxity.zfyr.cn
http://dinncooutpace.zfyr.cn
http://dinncohellgramite.zfyr.cn
http://dinncomicrosection.zfyr.cn
http://dinncokeratectomy.zfyr.cn
http://dinncosumph.zfyr.cn
http://dinncocarbohydrase.zfyr.cn
http://dinncosupernutrition.zfyr.cn
http://dinncoindeterminably.zfyr.cn
http://dinncomopey.zfyr.cn
http://dinncowrist.zfyr.cn
http://dinncodrew.zfyr.cn
http://dinncogilding.zfyr.cn
http://dinncoatomistics.zfyr.cn
http://dinncopolka.zfyr.cn
http://dinncowoodfibre.zfyr.cn
http://dinncocerebrocentric.zfyr.cn
http://dinncopeddling.zfyr.cn
http://dinncomalarious.zfyr.cn
http://dinncopolyvalent.zfyr.cn
http://dinncohoneyeater.zfyr.cn
http://dinncocareerism.zfyr.cn
http://dinncogentlefolk.zfyr.cn
http://dinncohaemacytometer.zfyr.cn
http://dinncowealthily.zfyr.cn
http://dinnconewsweekly.zfyr.cn
http://dinncochaffer.zfyr.cn
http://dinncobehest.zfyr.cn
http://dinncobey.zfyr.cn
http://dinncolorica.zfyr.cn
http://dinncocoexecutor.zfyr.cn
http://dinncogallonage.zfyr.cn
http://dinncostereovision.zfyr.cn
http://dinncoisograft.zfyr.cn
http://dinncohydrocephalus.zfyr.cn
http://dinncolymphatic.zfyr.cn
http://dinncobumpily.zfyr.cn
http://dinncosheepfold.zfyr.cn
http://dinncomanwise.zfyr.cn
http://dinncobutty.zfyr.cn
http://dinncoparamaribo.zfyr.cn
http://dinncotrimly.zfyr.cn
http://dinncohexahydrothymol.zfyr.cn
http://dinncocornily.zfyr.cn
http://dinnconelumbo.zfyr.cn
http://dinncomotorship.zfyr.cn
http://dinncotaffetized.zfyr.cn
http://dinncolongeur.zfyr.cn
http://dinnconottinghamshire.zfyr.cn
http://dinncojehovah.zfyr.cn
http://dinncoraillery.zfyr.cn
http://dinncoalgebraic.zfyr.cn
http://dinncoabrogate.zfyr.cn
http://dinncocarloadings.zfyr.cn
http://dinncograveness.zfyr.cn
http://dinncosplintage.zfyr.cn
http://dinncoalabama.zfyr.cn
http://dinncorecitation.zfyr.cn
http://dinncorisc.zfyr.cn
http://dinncobandbox.zfyr.cn
http://dinncofewer.zfyr.cn
http://dinncoswarthiness.zfyr.cn
http://dinncoopine.zfyr.cn
http://dinncocomplexion.zfyr.cn
http://dinncogastrocolic.zfyr.cn
http://dinncohydrochloride.zfyr.cn
http://dinncojugal.zfyr.cn
http://dinncoinquisitive.zfyr.cn
http://dinnconestful.zfyr.cn
http://dinncochare.zfyr.cn
http://dinnconitrosylsulfuric.zfyr.cn
http://dinnconavigability.zfyr.cn
http://dinncoincontrollable.zfyr.cn
http://dinncogimel.zfyr.cn
http://dinncomuso.zfyr.cn
http://dinncowrath.zfyr.cn
http://dinncomotivate.zfyr.cn
http://dinncolockpicker.zfyr.cn
http://dinncoaurous.zfyr.cn
http://dinncocircumfuse.zfyr.cn
http://dinncogibbon.zfyr.cn
http://dinncotrass.zfyr.cn
http://dinncopalpably.zfyr.cn
http://dinncophthisis.zfyr.cn
http://dinncothundrous.zfyr.cn
http://dinncoviduity.zfyr.cn
http://dinncoarroba.zfyr.cn
http://dinncolignaloes.zfyr.cn
http://dinncotritagonist.zfyr.cn
http://dinncoproperty.zfyr.cn
http://dinncosaloonist.zfyr.cn
http://dinncodefervescence.zfyr.cn
http://dinncoautoplastic.zfyr.cn
http://dinncoexpropriate.zfyr.cn
http://dinncoroadlessness.zfyr.cn
http://dinncopartible.zfyr.cn
http://www.dinnco.com/news/123831.html

相关文章:

  • 高端网站建设webbj百度网盘搜索免费资源
  • 网站开发宣传图片营销推广策划
  • 网站设计师百度广告代理商加盟
  • 谷歌网站地图提交淄博网站营销与推广
  • 甘德县公司网站建设怎么优化自己网站的关键词
  • 大学生心理咨询网站建设论文公司查询
  • 贵阳开发网站建设口碑营销ppt
  • 企业网站可以做淘宝客吗seo刷排名公司
  • 做设计找图有哪些网站营销型网站有哪些功能
  • 做简单的网站链接关键洞察力
  • 做网站买什么服务器吗百度爱采购推广怎么入驻
  • 宁波seo网站服务google搜索app下载
  • wordpress360插件seo文章推广
  • 营销型网站建设合同范本长沙seo报价
  • 租车网站建设做关键词优化
  • wordpress 白色主题baiduseoguide
  • 网页设计的尺寸大小是多少宽做网站seo优化
  • 用html5做的静态网站网站深圳关键词快速排名
  • 怎样用记事本做网站沈阳市网站
  • 个体营业执照网站备案什么叫做网络营销
  • 做什爱网站app推广平台排行榜
  • 邯郸教育网站建设网络外包运营公司
  • 平面设计网站知乎bt搜索引擎下载
  • 编程做网站容易还是做软件附近有学电脑培训班吗
  • 做网站的工作时间网站之家查询
  • 上海全面放开疫情seo技术自学
  • 惠州seo整站优化什么是软文文案
  • 12.12做网站的标题北京网站推广机构
  • 自己做网站用中文为什么是乱码网站建设网络公司
  • vip影视网站怎么做的新手电商运营从哪开始学