lua脚本使用,单个及多个参数post请求

1、脚本内容 access_token_check.lua:

token = ngx.req.get_headers()['token']

if (token == nil or token == '') then
    ngx.header['Content-Type'] = 'application/json; charset=utf-8'
    ngx.print('{"errorCode":"401","value":"无访问权限!","data":null}')
    ngx.exit(ngx.OK)
end


res = ngx.location.capture(
        '/gateway/api/gateway/checkToken',
        {
            method = ngx.HTTP_POST,
            --单个固定参数
            body = '{"channelId":"1"}'
        }
)
if (res ~= nil
        and res.status ~= nil
        and res.status == ngx.HTTP_OK) then

    --放入头部
    ngx.req.set_header('userId', res.header['userId'])
    ngx.req.set_header('channelId', res.header['channelId'])
    ngx.req.set_header('platform', res.header['platform'])


    --获取请求的URI
    local request_uri = ngx.var.request_uri
    --日志打印
    ngx.log(ngx.ERR, request_uri)
    ngx.log(ngx.ERR,"-----------------------------------------")
    ngx.header['Content-Type'] = 'application/json; charset=utf-8'
    ngx.log(ngx.ERR, res.body)
    ngx.log(ngx.ERR, res.status)

    check_res = ngx.location.capture(
        '/gateway-login/api/checkRolePermission',
        {
            method = ngx.HTTP_POST,
            --多个动态参数
            body = '{"userId":"' .. res.header['userId'] .. '","requestUri":"' .. ngx.var.request_uri .. '"}'
        }
    )

    if (res ~= nil
        and res.status ~= nil
        and res.status ~= ngx.HTTP_OK) then
        ngx.print('{"errorCode":"401","value":"权限不足!","data":null}')
        ngx.exit(ngx.OK)
    end


else
    ngx.header['Content-Type'] = 'application/json; charset=utf-8'
    ngx.print('{"errorCode":"401","value":"无访问权限!","data":null}')
    ngx.exit(ngx.OK)


end

2、使用 

location /gateway{
       access_by_lua_file /usr/local/openresty/lualib/access_token_check.lua;
       proxy_pass http://gateway/;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用Lua脚本可以将多个Redis命令组合成一个请求,从而减少网络开销和提高性能。以下是使用Lua脚本组合Redis的多个命令的示例代码: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void updateUserData(String userId, Map<String, Object> userData) { String script = "redis.call('HMSET', KEYS[1], unpack(ARGV))"; List<String> keys = Collections.singletonList("user:" + userId); List<Object> args = new ArrayList<>(userData.size() * 2); userData.forEach((key, value) -> { args.add(key); args.add(value); }); redisTemplate.execute(new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { Long result = (Long) connection.eval( script.getBytes(StandardCharsets.UTF_8), ReturnType.INTEGER, 1, keys.toArray(new String[0]), args.toArray(new Object[0]) ); return null; } }); } ``` 在上面的示例代码中,我们使用了RedisTemplate来执行Redis命令。在updateUserData方法中,我们将要更新的用户数据存储在Map<String, Object>对象中,并使用Lua脚本将其作为参数传递给HMSET命令。在Lua脚本中,我们使用unpack函数将Map的所有键值对展开为一系列参数,然后将这些参数传递给HMSET命令。 注意,在使用Lua脚本时,我们需要使用RedisConnection对象的eval方法来执行脚本。我们还需要指定脚本的返回类型(在这种情况下,我们期望返回一个整数)以及脚本使用的键和参数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值