淘东电商项目(71) -互联网安全架构设计(网关验证AccessToken)

引言

本文代码已提交至Github(版本号:626d01d9557e8989e6faf4522fa276a3bcc823c3),有兴趣的同学可以下载来看看:https://github.com/ylw-github/taodong-shop

在之前博客《淘东电商项目(67) -互联网安全架构设计(方法论)》,主要讲解了互联网安全架构设计的方法,主要介绍了如下几种:

  • 基于网关实现IP黑名单与名单拦截
  • API接口实现Token授权认证
  • 使用MD5实现API接口验证签名,防止抓包篡改数据
  • 实现API接口安全加密传输(公钥和私钥互换机制)
  • 基于Oauth2.0 实现API接口开放平台
  • 接口参数使用网关实现防止XSS、SQL注入
  • 定期工具实现代码健康扫描

上面的打勾代码实现在前面博客已经讲解完,本文继续讲解网关验证上一篇博客OAuth授权验证成功后返回的AccessToken。

本文目录结构:
l____引言
l____ 1.代码实现
l____ 2.测试

1.代码实现

①application.yml配置网关(url 地址后面以public开始的为提供给第三方接口,比如下面的/public/api-pay/**):

### 配置网关反向代理
zuul:
  routes:
    api-a:
      ### 以 /api-weixin/访问转发到会员服务
      path: /api-weixin/**
      serviceId: taodong-shop-service-weixin
    api-b:
      ### 以 /api-member/访问转发到订单服务
      path: /api-member/**
      serviceId: taodong-shop-service-member
    api-c:
      ### 以 /api-member/访问转发到订单服务
      path: /api-pay/**
      serviceId: taodong-shop-service-pay
    api-d:
      ### 以 /api-pay/访问转发到订单服务
      path: /public/api-pay/**
      serviceId: taodong-shop-service-pay

②增加AccessToken验证接口(GatewayBuild类):

/**
* api权限控制
*
*/
Boolean apiAuthority(RequestContext ctx, HttpServletRequest request);

③接口实现(VerificationBuild类):

@Override
public Boolean apiAuthority(RequestContext ctx, HttpServletRequest request) {
    String servletPath = request.getServletPath();
    log.info(">>>>>servletPath:" + servletPath + ",servletPath.substring(0, 5):" + servletPath.substring(0, 5));
    if (!servletPath.substring(0, 7).equals("/public")) {
        return true;
    }
    String accessToken = request.getParameter("accessToken");
    log.info(">>>>>accessToken验证:" + accessToken);
    if (StringUtils.isEmpty(accessToken)) {
        resultError(ctx, "AccessToken cannot be empty");
        return false;
    }
    // 调用接口验证accessToken是否失效
    BaseResponse<JSONObject> appInfo = verificaCodeServiceFeign.getAppInfo(accessToken);
    log.info(">>>>>>data:" + appInfo.toString());
    if (!isSuccess(appInfo)) {
        resultError(ctx, appInfo.getMsg());
        return false;
    }
    return true;
}

④构建者:

@Component
public class GatewayDirector {
    @Resource(name = "verificationBuild")
    private GatewayBuild gatewayBuild;

    public void direcot(RequestContext ctx, String ipAddres, HttpServletResponse response, HttpServletRequest request) {
        /**
         * 黑名单拦截
         */
        Boolean blackBlock = gatewayBuild.blackBlock(ctx, ipAddres, response);
        if (!blackBlock) {
            return;
        }
        /**
         * 参数验证
         */
        Boolean verifyMap = gatewayBuild.toVerifyMap(ctx, ipAddres, request);
        if (!verifyMap) {
            return;
        }

        /**
         * XSS攻击处理
         */
        Map<String, List<String>> filterParameters = gatewayBuild.filterParameters(request, ctx);
        if (filterParameters != null && filterParameters.size() > 0) {
            ctx.setRequestQueryParams(filterParameters);
        }

        // 3.验证accessToken
        Boolean apiAuthority = gatewayBuild.apiAuthority(ctx, request);
        if (!apiAuthority) {
            return;
        }
    }

}

2.测试

1.模拟合作伙伴提交个人信息,申请appIdappSecret,浏览器访问:http://localhost:9500/applyAppInfo?appName=腾讯小马
在这里插入图片描述
2.获取AccessToken令牌,浏览器访问:http://localhost:9500/getAccessToken?appId=7f38d645-032a-43e7-9f08-b7740288836d&appSecret=BF81CD9C70B597F88CF7794A7961F7FD
在这里插入图片描述
3.通过令牌去提交订单,首先使用错误的令牌访问:
http://localhost/public/api-pay/cratePayToken?payAmount=9999&orderId=20200513141452&userId=27&productName=apple&accessToken=123,可以看到令牌无效。

在这里插入图片描述
再使用正确的令牌访问http://localhost/public/api-pay/cratePayToken?payAmount=9999&orderId=20200513141452&userId=27&productName=apple&accessToken=authfdc563ec2ec049ea8fc66ab777215bb5,可以看到访问成功:
在这里插入图片描述

本文完!

扫描二维码关注公众号,回复: 11248664 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_20042935/article/details/106257875