Spring MVC请求参数与响应结果全局加密和解密详解
前提
前段时间在做一个对外的网关项目,涉及到加密和解密模块,这里详细分析解决方案和适用的场景。为了模拟真实的交互场景,先定制一下整个交互流程。第三方传输(包括请求和响应)数据报文包括三个部分:
1、timestamp,long类型,时间戳。
2、data,string类型,实际的业务请求数据转化成的json字符串再进行加密得到的密文。
3、sign,签名,生成规则算法伪代码是sha-256(data=xxx×tamp=11111),防篡改。
为了简单起见,加密和解密采用aes,对称秘钥为"throwable"。上面的场景和加解密例子仅仅是为了模拟真实场景,安全系数低,切勿直接用于生产环境。
现在还有一个地方要考虑,就是无法得知第三方如何提交请求数据,假定都是采用post的http请求方法,提交报文的时候指定contenttype为application/json或者application/x-www-form-urlencoded,两种contenttype提交方式的请求体是不相同的:
//application/x-www-form-urlencoded timestamp=xxxx&data=yyyyyy&sign=zzzzzzz //application/json {"timestamp":xxxxxx,"data":"yyyyyyyy","sign":"zzzzzzz"}
最后一个要考虑的地方是,第三方强制要求部分接口需要用明文进行请求,在提供一些接口方法的时候,允许使用明文交互。总结一下就是要做到以下三点:
1、需要加解密的接口请求参数要进行解密,响应结果要进行加密。
2、不需要加解密的接口可以用明文请求。
3、兼容contenttype为application/json或者application/x-www-form-urlencoded两种方式。
上面三种情况要同时兼容算是十分严苛的场景,在生产环境中可能也是极少情况下才遇到,不过还是能找到相对优雅的解决方案。先定义两个特定场景的接口:
1、下单接口(加密)
- url:/order/save
- http method:post
- contenttype:application/x-www-form-urlencoded
- 原始参数:orderid=yyyyyyyyy&userid=xxxxxxxxx&amount=zzzzzzzzz
- 加密参数:timestamp=xxxx&data=yyyyyy&sign=zzzzzzz
2、订单查询接口(明文)
- url:/order/query
- contenttype:application/json
- http method:post
- 原始参数:{"userid":"xxxxxxxx"}
两个接口的contenttype不相同是为了故意复杂化场景,在下面的可取方案中,做法是把application/x-www-form-urlencoded中的形式如xxx=yyy&aaa=bbb的表单参数和application/json中形式如{"key":"value"}的请求参数统一当做application/json形式的参数处理,这样的话,我们就可以直接在控制器方法中使用@requestbody。
方案
我们首先基于上面说到的加解密方案,提供一个加解密工具类:
public enum encryptutils { /** * singleton */ singleton; private static final string secret = "throwable"; private static final string charset = "utf-8"; public string sha(string raw) throws exception { messagedigest messagedigest = messagedigest.getinstance("sha-256"); messagedigest.update(raw.getbytes(charset)); return hex.encodehexstring(messagedigest.digest()); } private cipher createaescipher() throws exception { return cipher.getinstance("aes"); } public string encryptbyaes(string raw) throws exception { cipher aescipher = createaescipher(); keygenerator keygenerator = keygenerator.getinstance("aes"); keygenerator.init(128, new securerandom(secret.getbytes(charset))); secretkey secretkey = keygenerator.generatekey(); secretkeyspec secretkeyspec = new secretkeyspec(secretkey.getencoded(), "aes"); aescipher.init(cipher.encrypt_mode, secretkeyspec); byte[] bytes = aescipher.dofinal(raw.getbytes(charset)); return hex.encodehexstring(bytes); } public string decryptbyaes(string raw) throws exception { byte[] bytes = hex.decodehex(raw); cipher aescipher = createaescipher(); keygenerator keygenerator = keygenerator.getinstance("aes"); keygenerator.init(128, new securerandom(secret.getbytes(charset))); secretkey secretkey = keygenerator.generatekey(); secretkeyspec secretkeyspec = new secretkeyspec(secretkey.getencoded(), "aes"); aescipher.init(cipher.decrypt_mode, secretkeyspec); return new string(aescipher.dofinal(bytes), charset); } }
注意为了简化加解密操作引入了apache的codec依赖:
<dependency> <groupid>commons-codec</groupid> <artifactid>commons-codec</artifactid> <version>1.11</version> </dependency>
上面的加解密过程中要注意两点:
1、加密后的结果是byte数组,要把二进制转化为十六进制字符串。
2、解密的时候要把原始密文由十六进制转化为二进制的byte数组。
上面两点必须注意,否则会产生乱码,这个和编码相关,具体可以看之前写的一篇博客。
不推荐的方案
其实最暴力的方案是直接定制每个控制器的方法参数类型,因为我们可以和第三方磋商哪些请求路径需要加密,哪些是不需要加密,甚至哪些是application/x-www-form-urlencoded,哪些是application/json的请求,这样我们可以通过大量的硬编码达到最终的目标。举个例子:
@restcontroller public class controller1 { @autowired private objectmapper objectmapper; @postmapping(value = "/order/save", consumes = mediatype.application_form_urlencoded_value, produces = mediatype.application_json_utf8_value) public responseentity<encryptmodel> saveorder(@requestparam(name = "sign") string sign, @requestparam(name = "timestamp") long timestamp, @requestparam(name = "data") string data) throws exception { encryptmodel model = new encryptmodel(); model.setdata(data); model.settimestamp(timestamp); model.setsign(sign); string inrawsign = string.format("data=%s×tamp=%d", model.getdata(), model.gettimestamp()); string insign = encryptutils.singleton.sha(inrawsign); if (!insign.equals(model.getsign())){ throw new illegalargumentexception("验证参数签名失败!"); } //这里忽略实际的业务逻辑,简单设置返回的data为一个map map<string, object> result = new hashmap<>(8); result.put("code", "200"); result.put("message", "success"); encryptmodel out = new encryptmodel(); out.settimestamp(system.currenttimemillis()); out.setdata(encryptutils.singleton.encryptbyaes(objectmapper.writevalueasstring(result))); string rawsign = string.format("data=%s×tamp=%d", out.getdata(), out.gettimestamp()); out.setsign(encryptutils.singleton.sha(rawsign)); return responseentity.ok(out); } @postmapping(value = "/order/query", consumes = mediatype.application_json_value, produces = mediatype.application_json_utf8_value) public responseentity<order> queryorder(@requestbody user user){ order order = new order(); //这里忽略实际的业务逻辑 return responseentity.ok(order); } }
这种做法能在短时间完成对应的加解密功能,不需要加解密的接口不用引入相关的代码即可。缺陷十分明显,存在硬编码、代码冗余等问题,一旦接口增多,项目的维护难度大大提高。因此,这种做法是不可取的。
混合方案之filter和springmvc的http消息转换器
这里先说一点,这里是在springmvc中使用filter。因为要兼容两种contenttype,我们需要做到几点:
1、修改请求头的contenttype为application/json。
2、修改请求体中的参数,统一转化为inputstream。
3、定制url规则,区别需要加解密和不需要加解密的url。
使用filter有一个优点:不需要理解springmvc的流程,也不需要扩展springmvc的相关组件。缺点也比较明显:
1、如果需要区分加解密,只能通过url规则进行过滤。
2、需要加密的接口的springmvc控制器的返回参数必须是加密后的实体类,无法做到加密逻辑和业务逻辑完全拆分,也就是解密逻辑对接收的参数是无感知,但是加密逻辑对返回结果是有感知的。
ps:上面提到的几个需要修改请求参数、请求头等是因为特殊场景的定制,所以如果无此场景可以直接看下面的"单纯的json请求参数和json响应结果"小节。流程大致如下:
编写filter的实现和httpservletrequestwrapper的实现:
//customencryptfilter @requiredargsconstructor public class customencryptfilter extends onceperrequestfilter { private final objectmapper objectmapper; @override protected void dofilterinternal(httpservletrequest request, httpservletresponse response, filterchain filterchain) throws servletexception, ioexception { //content-type string contenttype = request.getcontenttype(); string requestbody = null; boolean shouldencrypt = false; if (stringutils.substringmatch(contenttype, 0, mediatype.application_form_urlencoded_value)) { shouldencrypt = true; requestbody = convertformtostring(request); } else if (stringutils.substringmatch(contenttype, 0, mediatype.application_json_value)) { shouldencrypt = true; requestbody = convertinputstreamtostring(request.getinputstream()); } if (!shouldencrypt) { filterchain.dofilter(request, response); } else { customencrypthttpwrapper wrapper = new customencrypthttpwrapper(request, requestbody); wrapper.putheader("content-type", mediatype.application_problem_json_utf8_value); filterchain.dofilter(wrapper, response); } } private string convertformtostring(httpservletrequest request) { map<string, string> result = new hashmap<>(8); enumeration<string> parameternames = request.getparameternames(); while (parameternames.hasmoreelements()) { string name = parameternames.nextelement(); result.put(name, request.getparameter(name)); } try { return objectmapper.writevalueasstring(result); } catch (jsonprocessingexception e) { throw new illegalargumentexception(e); } } private string convertinputstreamtostring(inputstream inputstream) throws ioexception { return streamutils.copytostring(inputstream, charset.forname("utf-8")); } } //customencrypthttpwrapper public class customencrypthttpwrapper extends httpservletrequestwrapper { private final map<string, string> headers = new hashmap<>(8); private final byte[] data; public customencrypthttpwrapper(httpservletrequest request, string content) { super(request); data = content.getbytes(charset.forname("utf-8")); enumeration<string> headernames = request.getheadernames(); while (headernames.hasmoreelements()) { string key = headernames.nextelement(); headers.put(key, request.getheader(key)); } } public void putheader(string key, string value) { headers.put(key, value); } @override public string getheader(string name) { return headers.get(name); } @override public enumeration<string> getheaders(string name) { return collections.enumeration(collections.singletonlist(headers.get(name))); } @override public enumeration<string> getheadernames() { return collections.enumeration(headers.keyset()); } @override public servletinputstream getinputstream() throws ioexception { bytearrayinputstream inputstream = new bytearrayinputstream(data); return new servletinputstream() { @override public boolean isfinished() { return !isready(); } @override public boolean isready() { return inputstream.available() > 0; } @override public void setreadlistener(readlistener listener) { } @override public int read() throws ioexception { return inputstream.read(); } }; } @override public bufferedreader getreader() throws ioexception { return super.getreader(); } } //customencryptconfiguration @configuration public class customencryptconfiguration { @bean public filterregistrationbean<customencryptfilter> customencryptfilter(objectmapper objectmapper){ filterregistrationbean<customencryptfilter> bean = new filterregistrationbean<>(new customencryptfilter(objectmapper)); bean.addurlpatterns("/e/*"); return bean; } }
控制器代码:
//可加密的,空接口 public interface encryptable { } @data public class order implements encryptable{ private long userid; } @data public class encryptresponse<t> implements encryptable { private integer code; private t data; } @requiredargsconstructor @restcontroller public class controller { private final objectmapper objectmapper; @postmapping(value = "/e/order/save", consumes = mediatype.application_json_value, produces = mediatype.application_json_utf8_value) public encryptresponse<order> saveorder(@requestbody order order) throws exception { //这里忽略实际的业务逻辑,简单设置返回的data为一个map encryptresponse<order> response = new encryptresponse<>(); response.setcode(200); response.setdata(order); return response; } @postmapping(value = "/c/order/query", consumes = mediatype.application_json_value, produces = mediatype.application_json_utf8_value) public responseentity<order> queryorder(@requestbody user user) { order order = new order(); //这里忽略实际的业务逻辑 return responseentity.ok(order); } }
这里可能有人有疑问,为什么不在filter做加解密的操作?因为考虑到场景太特殊,要兼容两种形式的表单提交参数,如果在filter做加解密操作,会影响到controller的编码,这就违反了全局加解密不影响到里层业务代码的目标。上面的filter只会拦截url满足/e/*的请求,因此查询接口/c/order/query不会受到影响。这里使用了标识接口用于决定请求参数或者响应结果是否需要加解密,也就是只需要在httpmessageconverter中判断请求参数的类型或者响应结果的类型是否加解密标识接口的子类:
@requiredargsconstructor public class customencrypthttpmessageconverter extends mappingjackson2httpmessageconverter { private final objectmapper objectmapper; @override protected object readinternal(class<?> clazz, httpinputmessage inputmessage) throws ioexception, httpmessagenotreadableexception { if (encryptable.class.isassignablefrom(clazz)) { encryptmodel in = objectmapper.readvalue(streamutils.copytobytearray(inputmessage.getbody()), encryptmodel.class); string inrawsign = string.format("data=%s×tamp=%d", in.getdata(), in.gettimestamp()); string insign; try { insign = encryptutils.singleton.sha(inrawsign); } catch (exception e) { throw new illegalargumentexception("验证参数签名失败!"); } if (!insign.equals(in.getsign())) { throw new illegalargumentexception("验证参数签名失败!"); } try { return objectmapper.readvalue(encryptutils.singleton.decryptbyaes(in.getdata()), clazz); } catch (exception e) { throw new illegalargumentexception("解密失败!"); } } else { return super.readinternal(clazz, inputmessage); } } @override protected void writeinternal(object object, type type, httpoutputmessage outputmessage) throws ioexception, httpmessagenotwritableexception { class<?> clazz = (class) type; if (encryptable.class.isassignablefrom(clazz)) { encryptmodel out = new encryptmodel(); out.settimestamp(system.currenttimemillis()); try { out.setdata(encryptutils.singleton.encryptbyaes(objectmapper.writevalueasstring(object))); string rawsign = string.format("data=%s×tamp=%d", out.getdata(), out.gettimestamp()); out.setsign(encryptutils.singleton.sha(rawsign)); } catch (exception e) { throw new illegalargumentexception("参数签名失败!"); } super.writeinternal(out, type, outputmessage); } else { super.writeinternal(object, type, outputmessage); } } }
自实现的httpmessageconverter主要需要判断请求参数的类型和返回值的类型,从而判断是否需要进行加解密。
单纯的json请求参数和json响应结果的加解密处理最佳实践
一般情况下,对接方的请求参数和响应结果是完全规范统一使用json(contenttype指定为application/json,使用@requestbody接收参数),那么所有的事情就会变得简单,因为不需要考虑请求参数由xxx=yyy&aaa=bbb转换为inputstream再交给springmvc处理,因此我们只需要提供一个mappingjackson2httpmessageconverter子类实现(继承它并且覆盖对应方法,添加加解密特性)。我们还是使用标识接口用于决定请求参数或者响应结果是否需要加解密:
@requiredargsconstructor public class customencrypthttpmessageconverter extends mappingjackson2httpmessageconverter { private final objectmapper objectmapper; @override protected object readinternal(class<?> clazz, httpinputmessage inputmessage) throws ioexception, httpmessagenotreadableexception { if (encryptable.class.isassignablefrom(clazz)) { encryptmodel in = objectmapper.readvalue(streamutils.copytobytearray(inputmessage.getbody()), encryptmodel.class); string inrawsign = string.format("data=%s×tamp=%d", in.getdata(), in.gettimestamp()); string insign; try { insign = encryptutils.singleton.sha(inrawsign); } catch (exception e) { throw new illegalargumentexception("验证参数签名失败!"); } if (!insign.equals(in.getsign())) { throw new illegalargumentexception("验证参数签名失败!"); } try { return objectmapper.readvalue(encryptutils.singleton.decryptbyaes(in.getdata()), clazz); } catch (exception e) { throw new illegalargumentexception("解密失败!"); } } else { return super.readinternal(clazz, inputmessage); } } @override protected void writeinternal(object object, type type, httpoutputmessage outputmessage) throws ioexception, httpmessagenotwritableexception { class<?> clazz = (class) type; if (encryptable.class.isassignablefrom(clazz)) { encryptmodel out = new encryptmodel(); out.settimestamp(system.currenttimemillis()); try { out.setdata(encryptutils.singleton.encryptbyaes(objectmapper.writevalueasstring(object))); string rawsign = string.format("data=%s×tamp=%d", out.getdata(), out.gettimestamp()); out.setsign(encryptutils.singleton.sha(rawsign)); } catch (exception e) { throw new illegalargumentexception("参数签名失败!"); } super.writeinternal(out, type, outputmessage); } else { super.writeinternal(object, type, outputmessage); } } }
没错,代码是拷贝上一节提供的httpmessageconverter实现,然后控制器方法的参数使用@requestbody注解并且类型实现加解密标识接口encryptable即可,返回值的类型也需要实现加解密标识接口encryptable。这种做法可以让控制器的代码对加解密完全无感知。当然,也可以不改变原来的mappingjackson2httpmessageconverter实现,使用requestbodyadvice和responsebodyadvice完成相同的功能:
@requiredargsconstructor public class customrequestbodyadvice extends requestbodyadviceadapter { private final objectmapper objectmapper; @override public boolean supports(methodparameter methodparameter, type targettype, class<? extends httpmessageconverter<?>> convertertype) { class<?> clazz = (class) targettype; return encryptable.class.isassignablefrom(clazz); } @override public httpinputmessage beforebodyread(httpinputmessage inputmessage, methodparameter parameter, type targettype, class<? extends httpmessageconverter<?>> convertertype) throws ioexception { class<?> clazz = (class) targettype; if (encryptable.class.isassignablefrom(clazz)) { string content = streamutils.copytostring(inputmessage.getbody(), charset.forname("utf-8")); encryptmodel in = objectmapper.readvalue(content, encryptmodel.class); string inrawsign = string.format("data=%s×tamp=%d", in.getdata(), in.gettimestamp()); string insign; try { insign = encryptutils.singleton.sha(inrawsign); } catch (exception e) { throw new illegalargumentexception("验证参数签名失败!"); } if (!insign.equals(in.getsign())) { throw new illegalargumentexception("验证参数签名失败!"); } bytearrayinputstream inputstream = new bytearrayinputstream(in.getdata().getbytes(charset.forname("utf-8"))); return new mappingjacksoninputmessage(inputstream, inputmessage.getheaders()); } else { return super.beforebodyread(inputmessage, parameter, targettype, convertertype); } } } @requiredargsconstructor public class customresponsebodyadvice extends jsonviewresponsebodyadvice { private final objectmapper objectmapper; @override public boolean supports(methodparameter returntype, class<? extends httpmessageconverter<?>> convertertype) { class<?> parametertype = returntype.getparametertype(); return encryptable.class.isassignablefrom(parametertype); } @override protected void beforebodywriteinternal(mappingjacksonvalue bodycontainer, mediatype contenttype, methodparameter returntype, serverhttprequest request, serverhttpresponse response) { class<?> parametertype = returntype.getparametertype(); if (encryptable.class.isassignablefrom(parametertype)) { encryptmodel out = new encryptmodel(); out.settimestamp(system.currenttimemillis()); try { out.setdata(encryptutils.singleton.encryptbyaes(objectmapper.writevalueasstring(bodycontainer.getvalue()))); string rawsign = string.format("data=%s×tamp=%d", out.getdata(), out.gettimestamp()); out.setsign(encryptutils.singleton.sha(rawsign)); out.setsign(encryptutils.singleton.sha(rawsign)); } catch (exception e) { throw new illegalargumentexception("参数签名失败!"); } } else { super.beforebodywriteinternal(bodycontainer, contenttype, returntype, request, response); } } }
单纯的application/x-www-form-urlencoded表单请求参数和json响应结果的加解密处理最佳实践
一般情况下,对接方的请求参数完全采用application/x-www-form-urlencoded表单请求参数返回结果全部按照json接收,我们也可以通过一个httpmessageconverter实现就完成加解密模块。
public class formhttpmessageconverter implements httpmessageconverter<object> { private final list<mediatype> mediatypes; private final objectmapper objectmapper; public formhttpmessageconverter(objectmapper objectmapper) { this.objectmapper = objectmapper; this.mediatypes = new arraylist<>(1); this.mediatypes.add(mediatype.application_form_urlencoded); } @override public boolean canread(class<?> clazz, mediatype mediatype) { return encryptable.class.isassignablefrom(clazz) && mediatypes.contains(mediatype); } @override public boolean canwrite(class<?> clazz, mediatype mediatype) { return encryptable.class.isassignablefrom(clazz) && mediatypes.contains(mediatype); } @override public list<mediatype> getsupportedmediatypes() { return mediatypes; } @override public object read(class<?> clazz, httpinputmessage inputmessage) throws ioexception, httpmessagenotreadableexception { if (encryptable.class.isassignablefrom(clazz)) { string content = streamutils.copytostring(inputmessage.getbody(), charset.forname("utf-8")); encryptmodel in = objectmapper.readvalue(content, encryptmodel.class); string inrawsign = string.format("data=%s×tamp=%d", in.getdata(), in.gettimestamp()); string insign; try { insign = encryptutils.singleton.sha(inrawsign); } catch (exception e) { throw new illegalargumentexception("验证参数签名失败!"); } if (!insign.equals(in.getsign())) { throw new illegalargumentexception("验证参数签名失败!"); } try { return objectmapper.readvalue(encryptutils.singleton.decryptbyaes(in.getdata()), clazz); } catch (exception e) { throw new illegalargumentexception("解密失败!"); } } else { mediatype contenttype = inputmessage.getheaders().getcontenttype(); charset charset = (contenttype != null && contenttype.getcharset() != null ? contenttype.getcharset() : charset.forname("utf-8")); string body = streamutils.copytostring(inputmessage.getbody(), charset); string[] pairs = stringutils.tokenizetostringarray(body, "&"); multivaluemap<string, string> result = new linkedmultivaluemap<>(pairs.length); for (string pair : pairs) { int idx = pair.indexof('='); if (idx == -1) { result.add(urldecoder.decode(pair, charset.name()), null); } else { string name = urldecoder.decode(pair.substring(0, idx), charset.name()); string value = urldecoder.decode(pair.substring(idx + 1), charset.name()); result.add(name, value); } } return result; } } @override public void write(object o, mediatype contenttype, httpoutputmessage outputmessage) throws ioexception, httpmessagenotwritableexception { class<?> clazz = o.getclass(); if (encryptable.class.isassignablefrom(clazz)) { encryptmodel out = new encryptmodel(); out.settimestamp(system.currenttimemillis()); try { out.setdata(encryptutils.singleton.encryptbyaes(objectmapper.writevalueasstring(o))); string rawsign = string.format("data=%s×tamp=%d", out.getdata(), out.gettimestamp()); out.setsign(encryptutils.singleton.sha(rawsign)); streamutils.copy(objectmapper.writevalueasstring(out) .getbytes(charset.forname("utf-8")), outputmessage.getbody()); } catch (exception e) { throw new illegalargumentexception("参数签名失败!"); } } else { string out = objectmapper.writevalueasstring(o); streamutils.copy(out.getbytes(charset.forname("utf-8")), outputmessage.getbody()); } } }
上面的httpmessageconverter的实现可以参考org.springframework.http.converter.formhttpmessageconverter。
小结
这篇文章强行复杂化了实际的情况(但是在实际中真的碰到过),一般情况下,现在流行使用json进行数据传输,在springmvc项目中,我们只需要针对性地改造mappingjackson2httpmessageconverter即可(继承并且添加特性),如果对springmvc的源码相对熟悉的话,直接添加自定义的requestbodyadvice(requestbodyadviceadapter)和responsebodyadvice(jsonviewresponsebodyadvice)实现也可以达到目的。至于为什么使用httpmessageconverter做加解密功能,这里基于springmvc源码的对请求参数处理的过程整理了一张处理流程图:
上面流程最核心的代码可以看abstractmessageconvertermethodargumentresolver#readwithmessageconverters和handlermethodargumentresolvercomposite#resolveargument,毕竟源码不会骗人。控制器方法返回值的处理基于是对称的,阅读起来也比较轻松。
参考资料:
spring-boot-web-starter:2.0.3.release源码。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: JDBC板块精华整理20051226
下一篇: MySQL恢复中的几个问题解决方法