如何将令牌从 Nestjs 请求标头传递到 Spring Boot 标头

时间:2021-04-29 11:52:29

标签: java spring spring-boot nestjs http-proxy

我正在将来自 nestjs 的请求代理到我的 Spring Boot 应用程序。我在 nestjs 中创建代理中间件时添加了 jwt 令牌。

这是我的中间件的代码

const onProxyReq = function (
        proxyReq: ClientRequest,
        req: Request,
        res: Response
    ) {
        const tokenPayload = {
            ...((req?.session as any)?.user && { // payload data is taken from the session which I have created
                user: (req.session as any).user,
            }),
        };
        const token = createToken(tokenPayload);
        proxyReq.setHeader('Authorization', `Bearer ${token}`);
    console.log(proxyReq); // getting the token in the request
    };

当我试图在 spring boot 的 preHandler 拦截器中检查这个令牌时,我得到了 Bearer null

我的spring boot拦截器文件

@Component
public class AuthInterceptor implements HandlerInterceptor{
    
    @Autowired
    private JwtHelper jwtHelper; //class for checking the token
    
    @Override
    public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler) {
        String requestHeader = request.getHeader("Authorization");
        System.out.printltn(requestHeader); // getting "Bearer null"
        if( requestHeader != null && requestHeader.startsWith("Bearer ")) {
            String token = requestHeader.substring(7);
            System.out.println(token);
            if(jwtHelper.verifyToken(token)) {
                return true;
            }
            else {
                System.out.println("False");
                return false;
            }
        }
        
        else {
            System.out.println("In valid Auth Header");
            return false;
        }
    }
}

如何在 Spring Boot 中获取我的令牌?提前致谢。

0 个答案:

没有答案