接口安全 时效性+签名+数据加密
导入maven依赖
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15</artifactId>
<version>1.46</version>
</dependency>
实现思路
1、所有接口通过注解+aop实现接口时效性、验证签名
2、具体业务接口再解密数据,然后进行业务代码处理逻辑
这里贴出简单的代码(在注解中实现接口时效性校验),以及验证签名的代码片段,数据解密部分的代码
可以自己实现,就不贴代码啦
show you the code
定义注解类
package com.pica.cloud.commercialization.crrs.interceptor;
import java.lang.annotation.*;
/**
* @ClassName SecurityAuth
* @Description
* @Author Chongwen.jiang
* @Date 2019/8/26 10:46
* @ModifyDate 2019/8/26 10:46
* @Version 1.0
*/
@Target({
ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SecurityAuth {
}
注解类业务代码实现
package com.pica.cloud.commercialization.crrs.interceptor;
import com.pica.cloud.commercialization.crrs.util.Signutil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
/**
* @ClassName SecurityAuthInterceptor
* @Description
* @Author Chongwen.jiang
* @Date 2019/8/26 10:48
* @ModifyDate 2019/8/26 10:48
* @Version 1.0
*/
@Component
@Slf4j
public class SecurityAuthInterceptor extends HandlerInterceptorAdapter{
private static final String PARAMETERS_ILLEGAL = "{\"code\":\"20000000\",\"data\":{},\"message\":\"接口调用参数非法\"}";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Annotation securityAuth = handlerMethod.getMethod().getAnnotation(SecurityAuth.class);
if (securityAuth == null) {
securityAuth = handlerMethod.getMethod().getDeclaringClass