SpringMVC利⽤⾃定义注解实现controller的参数解密
post请求⽆法保证数据安全,所以平时对⼀些敏感信息的传输时候,通常都是使⽤前端加密,后端解密的⽅式来实现。
解密⽅法千千万万,但是核⼼都是为了讲⼀串加密过的字符还原其本来的意思。
本⽂讲解的⽅法是在controller上⾯加⼀个⾃定义注解,全局统⼀解密,让加密参数在⽅法体中使⽤前就进⾏了解密。
步骤:
1、⾃定义注解
类的修饰符是class,接⼝的修饰符是interface,注解的修饰符是@interface。Java提供的元注解有四个,本⽂使⽤其中的两
个,@Target(使⽤范围,在哪⾥使⽤⽅法、接⼝、类、常量等),@Retention(该注解的⽣命周期)
如下就是定义了⼀个名为DecryptRequest的注解,该注解的⽣命周期是虚拟机的整个运⾏期间,使⽤范围是⽅法上
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DecryptRequest {
}
定义完就可以在⽅法头上使⽤该注解了
@PostMapping("postTest")
@DecryptRequest
public ModelAndView test(String name,String passowrd){
System.out.println("name:"+name);
System.out.println("passowrd:"+passowrd);
return null;
}
但是⽬前该注解没有进⾏任何操作,下⾯将赋予该注解解密功能,实现RequestBodyAdvice接⼝的作⽤是:只对使⽤该注解的body参数起作⽤。@ControllerAdvice注解的作⽤是让其运⾏在普通controller之前
aduation.advice;
import java.io.IOException;
import java.io.InputStream;
import flect.Type;
import org.apachemons.io.IOUtils;
import MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.verter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.hod.annotation.RequestBodyAdvice;
aduation.annotation.DecryptRequest;
/**
* ⾃定义实现类DecryptRequestBodyAdvice,该类实现RequestBodyAdvice接⼝,然后重写下⾯四个⽅法
* 实现RequestBodyAdvice接⼝的作⽤是:只对使⽤该注解的body参数起作⽤
*
* @author lollipop
*
*/
@ControllerAdvice
public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
@Override
public Object afterBodyRead(Object arg0, HttpInputMessage arg1, MethodParameter arg2, Type arg3,
Class<? extends HttpMessageConverter<?>> arg4) {
// ⽆条件放⾏数据
return arg0;
}
/*
*  该⽅法是在原使⽤DecryptRequest注解的⽅法体中使⽤body参数之前对⾥⾯的参数进⾏操作
*/
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage arg0, MethodParameter arg1, Type arg2,
Class<? extends HttpMessageConverter<?>> arg3) throws IOException {
//先判断有没有使⽤该注解
boolean isAnnotationPresent = Method().isAnnotationPresent(DecryptRequest.class);
if(isAnnotationPresent){
return new DecryptHttpInputMessage(arg0, "UTF-8");
}
return arg0;
}
@Override
public Object handleEmptyBody(Object arg0, HttpInputMessage arg1, MethodParameter arg2, Type arg3,
Class<? extends HttpMessageConverter<?>> arg4) {
// ⽆条件放⾏数据
return arg0;
}
@Override
public boolean supports(MethodParameter arg0, Type arg1, Class<? extends HttpMessageConverter<?>> arg2) {  // 原本是false,将其改成true
return true;
}
private class DecryptHttpInputMessage implements HttpInputMessage{
private HttpInputMessage httpInputMessage;
private String charset;
@Override
public HttpHeaders getHeaders() {
Headers();
}
public DecryptHttpInputMessage(HttpInputMessage httpInputMessage, String charset) {
this.httpInputMessage = httpInputMessage;
this.charset = charset;
}
@Override
public InputStream getBody() throws IOException {
//读取body的数据
String decrypt = Body(), charset);
System.out.println("前端传进来的数据:"+decrypt);
springmvc的注解有哪些//把数据解密,具体的解密⽅式因⼈⽽异,具体需要根据前端的加密⽅式来解密,这⾥只是简单的把它替换了
//把数据解密,具体的解密⽅式因⼈⽽异,具体需要根据前端的加密⽅式来解密,这⾥只是简单的把它替换了  decrypt="9999";
InputStream(decrypt, charset);
}
}
}
我的controller
@PostMapping(value="postTest",headers = {"content-type=application/json"})
@DecryptRequest
public ModelAndView test(@RequestBody String password,@RequestParam String name){
System.out.println("controller⽅法体中的密码:"+password);
System.out.println("controller⽅法体中的名字:"+name);
return null;
}
postman截图
运⾏结果图:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。