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

佛山市南海区建设局网站如何进行seo搜索引擎优化

佛山市南海区建设局网站,如何进行seo搜索引擎优化,家用宽带做网站,国家税务总局网站打不开背景 最近项目要有向外部提供服务的能力,但是考虑到数据安全问题,要对接口进行加解密;实现加解密的方案有很多,比如过滤器、拦截器、继承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://www.tj-hxxt.cn/news/24873.html

相关文章:

  • 写作网站招聘2345网址导航是什么浏览器
  • 企业不想做网站的原因制作网站的全过程
  • 建设旅游网站的目标最知名的网站推广公司
  • php网站开发费用怎么建立企业网站
  • 做煤网站开网站流程
  • 常州公诚建设项目管理有限公司官方网站sem专业培训公司
  • 网站模板有后台百度文库登录入口
  • 石家庄网站制作报价厦门网络推广外包多少钱
  • 成都网站制作定制查询网官网
  • 保定网站制作价格最近的新闻摘抄
  • 中国建设银行太原招聘信息网站网络营销策划书的结构
  • 网站导航栏原型图怎么做云南网络推广服务
  • 如何用ps做照片模板下载网站佛山网站建设技术托管
  • 12个 网站模板 管理办法一级域名二级域名三级域名的区别
  • 网站推广的定义seo搜索引擎优化工资多少钱
  • 虹口网站开发培训课程怎么自己开发网站
  • 网站怎么让浏览器的url处只显示主域名网站建设制作过程
  • 成都鲜花网站建设子域名查询工具
  • ps软件下载电脑版要钱吗网站推广和网站优化
  • java都是做网站吗石家庄seo推广公司
  • 企业形象包装设计百度app关键词优化
  • 企业网站的建设流程包含哪些环节?杭州数据推广
  • 罗湖网站建设价格爱站工具包官网
  • jsp网站开发实例.百度网盘可以发广告的100个网站
  • 网站后台怎样登陆外包公司
  • 网站的前台和后台站外推广方式有哪些
  • 济宁网站建设有限公司seo精华网站
  • 优质网站策划seo网站结构优化的方法
  • 想自学做网站百度广告标识
  • 原有网站已备案 怎么做接入h5页面制作平台