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

建设银行手机登陆网站浏览器大全

建设银行手机登陆网站,浏览器大全,杭州比较好的景观设计公司,网站开发学习课程前文通过阅读源码,深入分析了DispatcherServlet及相关组件的工作流程,本文不再阅读源码,介绍一下扩展HttpMessageConverter的方式。 HttpMessageConverter工作方式及扩展方式 前文介绍过,HttpMessageConverter是读写请求体和响应…

前文通过阅读源码,深入分析了DispatcherServlet及相关组件的工作流程,本文不再阅读源码,介绍一下扩展HttpMessageConverter的方式。

HttpMessageConverter工作方式及扩展方式

前文介绍过,HttpMessageConverter是读写请求体和响应体的组件。

RequestResponseBodyMethodProcessor(用于解析请求参数、处理返回值)从内置的HttpMessageConverter查找支持当前请求体、响应体的实例,然后调用read、write来读写数据。

Spring内置的HttpMessageConverter在装配RequestResponseBodyMethodProcessor的时候创建,具体代码在WebMvcConfigurationSupport类addDefaultHttpMessageConverters方法中。

开发者如果要扩展使用自己的HttpMessageConverter实现,可以编写组件实现WebMvcConfigurer接口,在extendMessageConverters方法中注入自己的HttpMessageConverter实现类对象。

自定义HttpMessageConverter

需求描述

系统需要对响应体进行加密、对请求体解密操作。

思路:

  1. 编写类实现HttpMessageConverter接口,read方法中先对请求体解密,之后在做json反序列化
  2. write方法先做json序列化,之后再加密
  3. 通过WebMvcConfigurer注册

编写HttpMessageConverter实现类

public class MyMappingJackson2HttpMessageConverter implements GenericHttpMessageConverter<Object> {private static final String S_KEY = "1234567890123456";private static final String IV_PARAMETER = "abcdefghijklmnop";// 用来做json序列化和反序列化,自己编写代码也可以,此处直接使用MappingJackson2HttpMessageConverter来做private final MappingJackson2HttpMessageConverter jackson2HttpMessageConverter;// 读写字符串private final StringHttpMessageConverter stringHttpMessageConverter;public MyMappingJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter jackson2HttpMessageConverter) {this.jackson2HttpMessageConverter = jackson2HttpMessageConverter;this.stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);}@Overridepublic boolean canRead(Class<?> clazz, MediaType mediaType) {return jackson2HttpMessageConverter.canRead(clazz, mediaType);}@Overridepublic boolean canWrite(Class<?> clazz, MediaType mediaType) {return jackson2HttpMessageConverter.canWrite(clazz, mediaType);}@Overridepublic List<MediaType> getSupportedMediaTypes() {return jackson2HttpMessageConverter.getSupportedMediaTypes();}@Overridepublic Object read(Class<?> clazz, HttpInputMessage inputMessage)throws IOException, HttpMessageNotReadableException {// 读取请求原始字节byte[] bytes = readBytes(inputMessage);// 解密byte[] decryptBytes = AesUtil.decrypt(bytes, S_KEY, IV_PARAMETER);// 封装HttpInputMessage供下面反序列化使用HttpInputMessage in = new MyHttpInputMessage(inputMessage, decryptBytes);// json反序列化return jackson2HttpMessageConverter.read(clazz, in);}@Overridepublic void write(Object o, MediaType contentType, HttpOutputMessage outputMessage)throws IOException, HttpMessageNotWritableException {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();// json序列化jackson2HttpMessageConverter.write(o, contentType, new MyHttpOutputMessage(outputMessage, byteArrayOutputStream));byte[] bytes = byteArrayOutputStream.toByteArray();// 加密byte[] encryptStr = AesUtil.encrypt(bytes, S_KEY, IV_PARAMETER);// 将响应写出去this.stringHttpMessageConverter.write(new String(encryptStr, StandardCharsets.UTF_8), contentType, outputMessage);}@Overridepublic boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {return jackson2HttpMessageConverter.canRead(type, contextClass, mediaType);}@Overridepublic Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)throws IOException, HttpMessageNotReadableException {byte[] bytes = readBytes(inputMessage);byte[] decryptBytes = AesUtil.decrypt(bytes, S_KEY, IV_PARAMETER);HttpInputMessage in = new MyHttpInputMessage(inputMessage, decryptBytes);return jackson2HttpMessageConverter.read(type, contextClass, in);}@Overridepublic boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {return jackson2HttpMessageConverter.canWrite(type, clazz, mediaType);}@Overridepublic void write(Object o, Type type, MediaType contentType, HttpOutputMessage outputMessage)throws IOException, HttpMessageNotWritableException {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();jackson2HttpMessageConverter.write(o, type, contentType, new MyHttpOutputMessage(outputMessage, byteArrayOutputStream));byte[] bytes = byteArrayOutputStream.toByteArray();byte[] encryptStr = AesUtil.encrypt(bytes, S_KEY, IV_PARAMETER);this.stringHttpMessageConverter.write(new String(encryptStr, StandardCharsets.UTF_8), contentType, outputMessage);}private byte[] readBytes(HttpInputMessage inputMessage) throws IOException {long contentLength = inputMessage.getHeaders().getContentLength();ByteArrayOutputStream bos =new ByteArrayOutputStream(contentLength >= 0 ? (int) contentLength : StreamUtils.BUFFER_SIZE);StreamUtils.copy(inputMessage.getBody(), bos);return bos.toByteArray();}private static class MyHttpInputMessage implements HttpInputMessage {private final HttpInputMessage originalHttpInputMessage;private final byte[] buf;public MyHttpInputMessage(HttpInputMessage originalHttpInputMessage, byte[] buf) {this.originalHttpInputMessage = originalHttpInputMessage;this.buf = buf;}@Overridepublic InputStream getBody() throws IOException {return new ByteArrayInputStream(this.buf);}@Overridepublic HttpHeaders getHeaders() {HttpHeaders headers = this.originalHttpInputMessage.getHeaders();headers.setContentLength(this.buf.length);return headers;}}private static class MyHttpOutputMessage implements HttpOutputMessage {private final HttpOutputMessage originalHttpOutputMessage;private final OutputStream outputStream;public MyHttpOutputMessage(HttpOutputMessage originalHttpOutputMessage,OutputStream outputStream) {this.originalHttpOutputMessage = originalHttpOutputMessage;this.outputStream = outputStream;}@Overridepublic OutputStream getBody() throws IOException {return this.outputStream;}@Overridepublic HttpHeaders getHeaders() {return this.originalHttpOutputMessage.getHeaders();}}
}

注入HttpMessageConverter实现类对象

@Component
public class MyWebMvcConfigurer implements WebMvcConfigurer {@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {MappingJackson2HttpMessageConverter converter = null;for (HttpMessageConverter<?> messageConverter : converters) {if (messageConverter instanceof MappingJackson2HttpMessageConverter) {converter = (MappingJackson2HttpMessageConverter) messageConverter;break;}}if (converter != null) {// 注入MyMappingJackson2HttpMessageConverterMyMappingJackson2HttpMessageConverter myMappingJackson2HttpMessageConverter =new MyMappingJackson2HttpMessageConverter(converter);converters.add(0, myMappingJackson2HttpMessageConverter);}}
}

文章转载自:
http://antiphonal.hkpn.cn
http://episteme.hkpn.cn
http://liverwurst.hkpn.cn
http://flatlet.hkpn.cn
http://attractile.hkpn.cn
http://bba.hkpn.cn
http://clouded.hkpn.cn
http://loricae.hkpn.cn
http://saleroom.hkpn.cn
http://deadwork.hkpn.cn
http://camouflage.hkpn.cn
http://ri.hkpn.cn
http://foretype.hkpn.cn
http://pretrial.hkpn.cn
http://benomyl.hkpn.cn
http://progesterone.hkpn.cn
http://dossy.hkpn.cn
http://menopausal.hkpn.cn
http://nystagmus.hkpn.cn
http://mucopurulent.hkpn.cn
http://postimpressionism.hkpn.cn
http://pidgin.hkpn.cn
http://worker.hkpn.cn
http://squiffer.hkpn.cn
http://cerebroid.hkpn.cn
http://oneparty.hkpn.cn
http://coapt.hkpn.cn
http://pistou.hkpn.cn
http://sirenian.hkpn.cn
http://hypnopompic.hkpn.cn
http://enfant.hkpn.cn
http://ignace.hkpn.cn
http://madras.hkpn.cn
http://bucentaur.hkpn.cn
http://plaintive.hkpn.cn
http://neurochemical.hkpn.cn
http://insurrectionist.hkpn.cn
http://coanda.hkpn.cn
http://timidness.hkpn.cn
http://dodecasyllable.hkpn.cn
http://satiable.hkpn.cn
http://crinoid.hkpn.cn
http://minutious.hkpn.cn
http://grift.hkpn.cn
http://homeliness.hkpn.cn
http://capably.hkpn.cn
http://artifice.hkpn.cn
http://trophallaxis.hkpn.cn
http://frettage.hkpn.cn
http://gutless.hkpn.cn
http://bighearted.hkpn.cn
http://spode.hkpn.cn
http://antitype.hkpn.cn
http://bracken.hkpn.cn
http://tumular.hkpn.cn
http://nakhodka.hkpn.cn
http://seminude.hkpn.cn
http://calvary.hkpn.cn
http://scandian.hkpn.cn
http://fibrination.hkpn.cn
http://chlamys.hkpn.cn
http://piteously.hkpn.cn
http://ingloriously.hkpn.cn
http://deneutralize.hkpn.cn
http://bode.hkpn.cn
http://abnegator.hkpn.cn
http://interchurch.hkpn.cn
http://angwantibo.hkpn.cn
http://fuze.hkpn.cn
http://heterotransplant.hkpn.cn
http://allemande.hkpn.cn
http://distressed.hkpn.cn
http://keratinization.hkpn.cn
http://sabled.hkpn.cn
http://melomaniac.hkpn.cn
http://umbrageously.hkpn.cn
http://intercession.hkpn.cn
http://esb.hkpn.cn
http://bbb.hkpn.cn
http://bronchoconstriction.hkpn.cn
http://blackshirt.hkpn.cn
http://apolline.hkpn.cn
http://fecula.hkpn.cn
http://lingula.hkpn.cn
http://djinni.hkpn.cn
http://result.hkpn.cn
http://msce.hkpn.cn
http://gladiola.hkpn.cn
http://posttranscriptional.hkpn.cn
http://cabby.hkpn.cn
http://droplet.hkpn.cn
http://derby.hkpn.cn
http://mash.hkpn.cn
http://pettifog.hkpn.cn
http://lepidopterid.hkpn.cn
http://units.hkpn.cn
http://lurid.hkpn.cn
http://caragana.hkpn.cn
http://conciliative.hkpn.cn
http://sacker.hkpn.cn
http://www.hrbkazy.com/news/76447.html

相关文章:

  • 企业宣传网站建设方案营销推广是什么
  • 泉州营销型网站设计百度网页收录
  • 商会网站建设方案书优化大师下载
  • 在家创业网站建设中国网评中国网评
  • intellij 网站开发seo优化基础教程pdf
  • 做图书馆网站模板qq群引流推广软件
  • 做公众号选择图片的网站精品成品网站源码
  • 如何创建电子商务网站新产品推广方案策划
  • 做带后台的网站电脑培训机构哪个好
  • 余姚网站建设公司seo刷点击软件
  • 做网站499网络营销的认识与理解
  • 分类信息发布网站模板超八成搜索网站存在信息泄露问题
  • 尽请期待还是敬请期待关键词优化推广排名
  • 郑州做软件的公司南平seo
  • 设计素材网站版权问题2023广州疫情最新消息今天
  • 前端工程师做交互网站焊工培训
  • 凡客诚品网站设计特点谷歌浏览器网页版入口在哪里
  • 个人网站建站申请美国最新消息今天 新闻
  • 北京建设招聘信息网站站长工具樱花
  • perl做网站电脑系统优化软件
  • 北京建设信源资讯有限公司网站淘宝代运营公司十大排名
  • 高品质网站设计注册网址
  • 微小店网站建设哪家好看今天的新闻
  • 东营做网站建设的公司b站推广网站入口202
  • 烟台网站制作培训必应搜索引擎首页
  • 17网站一起做网店档口出租seo扣费系统
  • 做网站需要写那些xmind汕头网站推广排名
  • 自己做网站怎么加定位企业网站制作
  • 湖北seo厦门seo优化多少钱
  • 外贸网站建设公司效果建立网站步骤