赞
踩
前言:
前一篇已经说完了一种rsa+aes混合加密的方式,后端采用实体类或map或者jsonObject方式来接收入参,本文要说的不采用这几种形式来入参,而是采用自定义的@RequestParam来接收参数
1.解决参数要判断的问题
2.解决代码重复太多的问题
demo下载地址:https://download.csdn.net/download/baidu_38990811/10809893
实现步骤:
1.创建spring boot项目,导入相关依赖,
2.编写RSA加解密工具类,AES加解密工具类
3.编写自定义注解SecurityParameter(让加解密细粒度),编写自定义注解RequestParam(参数绑定使用),记住这个注解是自定义的,和spring mvc的入参的某个名称一致,但是具体实现不一样
4.编写自定义MyMethodArgumentResolver和EncodeResponseAdvice
5.创建自定义类RepeatedlyReadFilter(复制body数据,可以进行多次读取body数据),创建自定义类RepeatedlyReadRequestWrapper(读取数据),创建自定义类RepeatedlyReadInterceptor(可以不创建)
6.创建配置类WebMvcConfig(配置拦截器,过滤器,参数解析器)
7.创建controller
8.创建jsp或者html,引入js(加密和解密的通用js),还有配置,配置文件
ps:因为这里没https证书,所有使用http, 考虑到前后端分离,使用json来传递数据,
红色标注的就是和前面一篇方式不同的地方(其实就是懒,想把入参简单化)
实践步骤(这里加密key都是采用前一篇文章的key):
1.第一步,略(参考前一篇文章)
2.第二步,略(参考前一篇文章)
3.自定义注解
- import org.springframework.web.bind.annotation.Mapping;
-
- import java.lang.annotation.*;
-
-
- /**
- * @author monkey
- * @desc 请求数据解密
- * @date 2018/10/25 20:17
- */
- @Target({ElementType.METHOD,ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Mapping
- @Documented
- public @interface SecurityParameter {
-
- /**
- * 入参是否解密,默认解密
- */
- boolean inDecode() default true;
-
- /**
- * 出参是否加密,默认加密
- */
- boolean outEncode() default true;
- }

- import org.springframework.core.annotation.AliasFor;
- import org.springframework.web.bind.annotation.ValueConstants;
-
- import java.lang.annotation.*;
-
-
- /**
- * @author monkey
- * @desc 请求数据解密
- * @date 2018/10/25 20:17
- */
- @Target(ElementType.PARAMETER)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface RequestParam {
-
- /**
- * Alias for {@link #name}.
- */
- @AliasFor("name")
- String value() default "";
-
- /**
- * The name of the request parameter to bind to.
- * @since 4.2
- */
- @AliasFor("value")
- String name() default "";
-
-
- boolean required() default true;
-
-
- String defaultValue() default ValueConstants.DEFAULT_NONE;
- }

第三步:
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import com.monkey.springboot.demo.annotation.RequestParam;
- import com.monkey.springboot.demo.annotation.SecurityParameter;
- import com.monkey.springboot.demo.utils.AesEncryptUtils;
- import com.monkey.springboot.demo.utils.RSAUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.core.MethodParameter;
- import org.springframework.web.bind.support.Web
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。