springboot-对post请求中的参数解密

之所以这里只提到解密,是因为加密在VUE前端已经实现了,前端返给后端的就是加密后的对象,而后端只负责解密就行了。如果需要后端进行加密,建议在ResponseBodyAdvice中进行。

@ApiOperationSupport(author = ApiAuthor.wenhao)
@ApiOperation(value = "手机+密码 登陆",notes = "手机+密码 登陆",nickname = "loginByPhoneAndPassWord",tags={"登陆,用户接口"})
@PostMapping (value= "/loginByPhoneAndPassWord",produces = "application/json;charset=UTF-8")
@DesDecrypt(aeskey = "eyJzdWgfgOiJ0b2tl")
public R<SysLoginsDto> loginByPhoneAndPassWord(@RequestBody @ApiParam @Valid loginByPhoneAndPassWord p){
    return R.success(loginimp.loginByPhoneAndPassWord(p));
};
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DesDecrypt {
    String aeskey();
}
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Accessors(fluent = true)
public class DecryptHttpInputMessage implements HttpInputMessage {
    private HttpHeaders headers;
    private InputStream body;

    @Override
    public InputStream getBody() throws IOException {
        return this.body;
    }

    @Override
    public HttpHeaders getHeaders() {
        return this.headers;
    }
}

 

@RestControllerAdvice(annotations = RestController.class)
@Configuration
@Order(1)
public class DecryptRequestBodyAdvice  extends  RequestBodyAdviceAdapter {

    private String supportRequest(MethodParameter methodParameter) {
        Method method = methodParameter.getMethod();
        //拦截DesDecrypt注解的方法进行解密
        if (method.isAnnotationPresent(DesDecrypt.class)) {
            DesDecrypt ddec = (DesDecrypt)method.getAnnotation(DesDecrypt.class);
            return ddec.aeskey();
        }else{
            return "";
        }
    }

    @Value("${spring.profiles.active}")
    private String profiles;

    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage request, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
        String httpBody = StreamUtils.copyToString(request.getBody(), Charset.defaultCharset());
        if(!profiles.equals("pro")){
            //非生成环境打印原始密文
            Tool.logByController.info(httpBody);
        }
        String aeskey = supportRequest(methodParameter);
        if (!"".equals(aeskey)) {
            try {
                httpBody = DesUtil.desEncrypt(httpBody, aeskey);
            } catch (Exception e) {
                throw new RuntimeException("提供的密文与密钥不匹配!");
            }
        }
        return DecryptHttpInputMessage.builder().headers(request.getHeaders()).body(new ByteArrayInputStream(httpBody.getBytes("UTF-8"))).build();
    }
}

 

/**
 * 解密方法工具用的hutool
 *
 * @param data 要解密的数据
 * @param key  解密key
 * @return 解密的结果
 * @throws Exception
 */
public static String desEncrypt(String data, String key) throws Exception {
    try {
        //DES des = SecureUtil.des(key.getBytes());
        DES des = new DES(Mode.ECB, Padding.PKCS5Padding,key.getBytes());
        return des.decryptStr(data, CharsetUtil.CHARSET_UTF_8);
    } catch (Exception e) {
        throw new Exception("解密失败");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QQ2738671

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值