微服务集成测试如何集成指定用户

Blade 已结 1 518
klniu
klniu 剑圣 2022-04-01 15:17

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

做集成测试时要调用其他服务时需要用到用户认证信息,比如获取用户id,租户id等,请问如何在测试时通过oauth2的认证或者加入认证信息?

我尝试使用继承RequestInterceptor的方式写入请求属性,但未成功:

@Service
class FeignRequestInterceptor : RequestInterceptor {
    override fun apply(requestTemplate: RequestTemplate) {
        val attributes = RequestContextHolder.getRequestAttributes() as ServletRequestAttributes
        val bladeUser = BladeUser()
        with (bladeUser) {
            clientId = "clientId"
            userId = 1234564000000000302
            tenantId = "123456"
            oauthId = "1"
            deptId = "1234564000000000501"
            postId = "1234564000000000302"
            roleId = "-1"
            account = "test"
            roleName = "用户"
            userName = "test"
            nickName = "test"
            detail = Kv.create()
        }
        requestTemplate.header("Authorization", "Bearer test") // 可以传过去
//        attributes.setAttribute("_BLADE_USER_REQUEST_ATTR_", bladeUser, 1)
        attributes.request.setAttribute("_BLADE_USER_REQUEST_ATTR_", bladeUser) // 无效
        attributes.request.setAttribute("aaaaaaaaaaaaaaaa", "test") // 无效
//        val token = JwtUtil.getAccessTokenKey("123456", "1234564000000000302", null)
//        RequestContextHolder.setRequestAttributes(attributes, true)
    }
}

上述代码里,head是可以传过去的,但

_BLADE_USER_REQUEST_ATTR_

属性传不过去,后来我又想办法用oauth2认证,但因为blade-auth并没有接口,所以想请问下有什么方便的方法集成吗?

1条回答
  •  yinyuncan
    yinyuncan (最佳回答者)
    2022-04-01 22:02

    最简单的我感觉是 封装个根据用户名密码登录获取token的方法就行了

    1 讨论(3)
提交回复