如何在XxlJob中调用其他模块的方法

Blade 未结 3 1193
Jalena
Jalena 剑圣 2021-06-21 15:36

一、该问题的重现步骤是什么?

在项目中使用XxlJob定时去执行一个方法,且这个方法是在一个新的模块中,且这个模块的方法受Blade权限管控。


如果在XxlJob中去调用这个方法,应当如何处理服务鉴权呢?


请问有例子吗?


目前打算使用`blade-starter-http`的HttpRequest来实现。


三、你正在使用的是什么产品,什么版本?在什么操作系统上?

BladeX 2.8.1


3条回答
  • 2021-06-21 17:47

    如果引入feign,会显得很臃肿,推荐使用 http组件来调用。

    调用的时候需要用到认证,你可以通过secure的配置来指定这些接口是需要token还是basic认证还是其他的认证。

    配置好之后再用http组件调用即可。

    image.png

    image.png

    作者追问:2021-06-22 12:01

    按照该文档方法进行了配置,一直提示 

    {"msg":"缺失令牌,鉴权失败","code":401,"data":null}


    配置如下:

    #动态签名认证配置
    sign:
      - method: ALL
        pattern: /dashboard/sign
        crypto: "sha1"
      - method: ALL
        pattern: /blade-business/taxrawdata/pull
        crypto: "sha1"


    代码如下:

    // secure
    Map<String, String> headers = new HashMap<>();
    long timestamp = DateUtil.now().getTime();
    String nonce = "job";
    String signature = DigestUtil.sha1Hex(timestamp + nonce);
    headers.put("nonce", nonce);
    headers.put("timestamp", String.valueOf(timestamp));
    headers.put("signature", signature);
    
    HttpRequest.get("http://localhost/blade-business/taxrawdata/pull")
          .log(LogLevel.BODY)
          .queryMap(queryMap)
          .cacheControl(CacheControl.FORCE_NETWORK)
          .addHeader(headers)
          .execute()
          .onResponse(responseSpec -> {
             if (responseSpec.isOk()) return ReturnT.SUCCESS;
             return new ReturnT(ReturnT.FAIL.getCode(), responseSpec.message());
          });


    日志:

    org.springblade.core.http.Slf4jLogger    : --> GET http://localhost/blade-business/taxrawdata/pull?endDate=20210622&startDate=20210619
    org.springblade.core.http.Slf4jLogger    : signature: 5e138fd57bd5f65faf01954d4740ef43ac3d0a5f
    org.springblade.core.http.Slf4jLogger    : nonce: job
    org.springblade.core.http.Slf4jLogger    : timestamp: 1624332240162
    org.springblade.core.http.Slf4jLogger    : User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
    org.springblade.core.http.Slf4jLogger    : --> END GET


    目前接口都能顺利调用通,但一直没用鉴权成功。

    0 讨论(0)
  • 2021-06-22 12:02

    我晕,编辑好的咋个成这样~


    配置:

        #动态签名认证配置
        sign:
          - method: ALL
            pattern: /dashboard/sign
            crypto: "sha1"
          - method: ALL
            pattern: /blade-business/taxrawdata/pull
            crypto: "sha1"


    代码

    // secure
    Map<String, String> headers = new HashMap<>();
    long timestamp = DateUtil.now().getTime();
    String nonce = "223";
    String signature = DigestUtil.sha1Hex(timestamp + nonce);
    headers.put("nonce", nonce);
    headers.put("timestamp", String.valueOf(timestamp));
    headers.put("signature", signature);
    
    HttpRequest.get("http://localhost/blade-business/taxrawdata/pull")
            .log(LogLevel.BODY)
            .queryMap(queryMap)
            .cacheControl(CacheControl.FORCE_NETWORK)
            .addHeader(headers)
            .execute()
            .onResponse(responseSpec -> {
                if (responseSpec.isOk()) return ReturnT.SUCCESS;
                return new ReturnT(ReturnT.FAIL.getCode(), responseSpec.message());
            });


    日志:

    org.springblade.core.http.Slf4jLogger    : --> GET http://localhost/blade-business/taxrawdata/pull?endDate=20210622&startDate=20210619
    org.springblade.core.http.Slf4jLogger    : signature: 5e138fd57bd5f65faf01954d4740ef43ac3d0a5f
    org.springblade.core.http.Slf4jLogger    : nonce: 223
    org.springblade.core.http.Slf4jLogger    : timestamp: 1624332240162
    org.springblade.core.http.Slf4jLogger    : User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
    org.springblade.core.http.Slf4jLogger    : --> END GET


    错误信息

    {"msg":"缺失令牌,鉴权失败","code":401,"data":null}


    0 讨论(0)
  • 网关默认会对Blade-Auth请求头进行校验

    %P463B70$HXWEX6P283~UCK.png

    LPG6~(2N_A(9GXDMLJA6IKU.png

    0 讨论(0)
提交回复