接口安全 时效性+签名+数据加密

本文介绍了接口安全策略的实现,通过注解+AOP来确保接口时效性和签名验证。详细展示了注解类定义、业务代码实现、接口使用注解以及SignUtil的时效性校验和签名验证功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值