开发中会遇到要求对http传输的数据进行加密传输,我们可以重写spring mvc的MappingJackson2HttpMessageConverter类,实现对加密解密的支持。代码如下:
package com.hzwei.spring.messageconvert.ext; import java.io.*; import java.lang.reflect.Type; import org.springframework.http.*; import org.springframework.http.converter*; import org.springframework.http.converter.json.*; import org.springframework.util.TypeUtils; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.ser.FilterProvider; import com.jsecode.platform.util.EncryptDecrypt; /** * 扩展MappingJackson2HttpMessageConverter类,增加对消息的解密/加密支持处理(支持spring-web-4.3.x) */ public class EncryptMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter { private static final MediaType TEXT_EVENT_STREAM = new MediaType("text", "event-stream"); private PrettyPrinter ssePrettyPrinter; private boolean needEncryptOutputMessage = false; private boolean needDecryptInputMessage = false; protected void init(ObjectMapper objectMapper){ super.init(objectMapper); DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentObjectsWith(new DefaultIndenter(" ", "\ndata:")); this.ssePrettyPrinter = prettyPrinter; } public boolean isNeedEncryptOutputMessage(){ return needEncryptOutputMessage; } public void setNeedEncryptOutputMessage(boolean needEncryptOutputMessage){ this.needEncryptOutputMessage = needEncryptOutputMessage; } public boolean isNeedDecryptInputMessage(){ return needDecryptInputMessage; } public void setNeedDecryptInputMessage(boolean needDecryptInputMessage){ this.needDecryptInputMessage = needDecryptInputMessage; } @Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException{ JavaType javaType = getJavaType(clazz, null); return readJavaType(javaType, inputMessage); } public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException{ JavaType javaType = getJavaType(type, contextClass); return readJavaType(javaType, inputMessage); } private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage){ try{ if(inputMessage instanceof MappingJacksonInputMessage){ Class<?> deserializationView = ((MappingJacksonInputMessage)inputMessage).getDeserializationView(); if(deserializationView != null){ return this.objectMapper.readerWithView(deserializationView) .forType(javaType) .readValue(getInputStreamData(inputMessage.getBody())); } } return this.objectMapper.readValue(getInputStreamData(inputMessage.getBody()), javaType); }catch(JsonProcessingException ex){ throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex); }catch(IOException ex){ throw new HttpMessageNotReadableException("I/O error while reading input message", ex); } } private byte[] getInputStreamData(InputStream in) throws IOException{ ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int offset = 0; while((offset = in.read(buff)) > 0){ swapStream.write(buff, 0, offset); } byte[] bytes = swapStream.toByteArray(); if(needDecryptInputMessage){ // 对bytes进行解密 bytes = EncryptDecrypt.AESFastDecrypt2(bytes); //TODO 此处换上您的解密算法 } return bytes; } @Override protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException{ if(needEncryptOutputMessage){ writeInternalEncrypt(object, type, outputMessage); }else{ super.writeInternal(object, type, outputMessage); } } // 对返回值进行序列化并使用AES加密 protected void writeInternalEncrypt(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException{ MediaType contentType = outputMessage.getHeaders().getContentType(); JsonEncoding encoding = getJsonEncoding(contentType); JsonFactory factory = this.getObjectMapper().getFactory(); ByteArrayBuilder bb = new ByteArrayBuilder(factory._getBufferRecycler()); JsonGenerator generator = factory.createGenerator(bb, encoding); writeToJsonGenerator(object, type, generator, contentType); try{ //TODO 此处换上您自己的加密算法 byte[] byteData = EncryptDecrypt.AESFastEncrypt(bb.toByteArray()); outputMessage.getBody().write(byteData); }catch(Exception ex){ throw new HttpMessageNotWritableException("Could not encrypt and write message: " + ex.getMessage(), ex); } } // 保持MappingJackson2HttpMessageConverter原滋原味的序列化方法 protected void writeToJsonGenerator(Object object, Type type, JsonGenerator generator, MediaType contentType) throws IOException, HttpMessageNotWritableException{ try{ writePrefix(generator, object); Class<?> serializationView = null; FilterProvider filters = null; Object value = object; JavaType javaType = null; if(object instanceof MappingJacksonValue){ MappingJacksonValue container = (MappingJacksonValue)object; value = container.getValue(); serializationView = container.getSerializationView(); filters = container.getFilters(); } if(type != null && value != null && TypeUtils.isAssignable(type, value.getClass())){ javaType = getJavaType(type, null); } ObjectWriter objectWriter; if(serializationView != null){ objectWriter = this.objectMapper.writerWithView(serializationView); }else if(filters != null){ objectWriter = this.objectMapper.writer(filters); }else{ objectWriter = this.objectMapper.writer(); } if(javaType != null && javaType.isContainerType()){ objectWriter = objectWriter.forType(javaType); } SerializationConfig config = objectWriter.getConfig(); if(contentType != null && contentType.isCompatibleWith(TEXT_EVENT_STREAM) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)){ objectWriter = objectWriter.with(this.ssePrettyPrinter); } objectWriter.writeValue(generator, value); writeSuffix(generator, object); generator.flush(); }catch(JsonProcessingException ex){ throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getOriginalMessage(), ex); } } }
在spring mvc项目中,替换MappingJackson2HttpMessageConverter为EncryptMappingJackson2HttpMessageConverter
相关推荐
主要给大家介绍了关于Spring MVC请求参数与响应结果全局加密和解密的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本项目示例基于spring boot 最新版本(2.1.9)实现,Spring Boot、Spring Cloud 学习示例,将持续更新…… 在基于Spring Boot、Spring ...Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Sprin
spring+spring mvc+mybaits搭建基础框架,内集成sqllite数据库,直接把demo中的war包放到服务器下即可运行,实现数据库密码连接等信息在配置文件中以密文形式存在des方式加密解密,。http://localhost:8090/
程序采用了经典的MVC的设计方案,将底层的RSA加密解密功能封装为单独的组件,并定义了RSA的操作模型,提供视图和底层模型进行交互的外观;视图层分离为视图和控制器两块,视图信息的更新由控制层进行控制。 程序综合...
jwx是开源的java公众号开发MVC框架,基于spring配置文件和微信消息或事件注解,通过微信上下文处理一个或多个微信公众号服务请求。目的主要有两个,其一生封装微信请求xml消息为java实体对象,将返回对象转换为xml...
通过SM3算法杂凑用户原始密钥形成新的密钥,将新密钥作为SM4加密算法的密钥。 系统采用Spring MVC开发,其中使用了artery(封装了spring),兼容spring。
encrypt-message-converterspring mvc 加解密报文注解 @EncryptDecrypt 作用于类或者方法上,即实现加解密spring mvc报文注解 @ExcludeEncryptDecrypt作用与方法 可在@EncryptDecrypt作用于类之后,排除不想加密的...
加密N上载 encrypt-N-upload是一个演示应用程序,它最初是一个集成应用... 用Javascript加密文本并用Java解密 在Spring Boot中创建多部分表单处理程序控制器 单元测试Spring Boot MVC控制器 使用Spring Boot调度程序
主要介绍了SpringMvc/SpringBoot HTTP通信加解密的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
加密解密支持 可进行加密的方式有: MD5 SHA-224 / 256 / 384 / 512 AES DES RSA 可进行解密的方式有: AES DES RSA 使用方法 在pom.xml中引入依赖: <groupId>cn.licoy</groupId> <artifactId>encrypt-...
微服务将负责: 寻找伪随机素数, 生成公钥和私钥, 使用它们来加密和解密消息。现场演示待定科技栈Sprint Boot异步方法春云Netflix Eureka(服务定位器) Feign + Ribbon(具有负载平衡功能的REST客户端) 闸道...
春天MVC等级 特征 记录中 快取 验证方式 雷迪斯 线程服务 线程池 使用AES加密解密数据 Spring安全OAuth2 具有证书连接的数据库Postgres 纵横比
框架以Spring Framework为核心、Spring MVC作为模型视图控制器、Shiro作为权限框架、Hibernate作为数据库操作层。本项目也可以为学习SpringMVC的同学提供帮助。本项目以查询待办任务、查待受理任务、查看运行中的...
iBase4J项目简介 iBase4J是Java语言的分布式系统架构。 使用Maven对项目进行预测管理,提高项目的易开发性,扩展性。... MVC:基于spring mvc注解,Rest样式Controller。异常统一管理。 预定:Spring
使用Spring整合开源框架。 使用Maven对项目进行模块化管理,提高项目的易开发性、扩展性。 系统包括4个子系统:系统管理Service、系统管理Web、业务Service、业务Web。 系统管理:包括用户管理、权限管理、数据字典...
工具类:字符串处理,类型转换,日期处理,IO和文件,Excel读写,加密解密,HTTP客户端,XML处理,转码,各种Util等等。 代码生成器:根据数据库表结构生成简单的增删改查功能代码,包括model、mapper、service、...
使用Spring整合开源框架。 使用Maven对项目进行模块化管理,提高项目的易开发性、扩展性。 系统包括5个子系统:公共功能、系统管理Service、系统管理Web、业务Service、业务Web。 公共功能:公共功能(AOP、缓存、...
Spring MVC,用于控制器的Spring REST Spring JDBC用于数据库访问 Spring Security实现安全性 实施Spring缓存机制以提高搜索性能 为数据库操作实现了事务管理。 用于UI渲染的JSP,Javascript,jQuery,HTML,CSS ...
WDSsoft的一款免费源代码 JCT 1.0,它是一个Java加密解密常用工具包。 Java局域网通信——飞鸽传书源代码 28个目标文件 内容索引:JAVA源码,媒体网络,飞鸽传书 Java局域网通信——飞鸽传书源代码,大家都知道VB...