NGINX-验证oauth2令牌

NGINX-验证oauth2令牌,nginx,spring-websocket,stomp,sockjs,Nginx,Spring Websocket,Stomp,Sockjs,首先,这是我用来建立websocket连接的技术: Sock JS用于前端,带有STOMP协议 NGINX作为web代理 使用STOMP协议的Spring as套接字服务器 我配置了一个NGINX实例来代理web套接字请求,到目前为止,这个NGINX配置在没有令牌验证的情况下运行良好 location /wsapp/ { proxy_pass http://wsbackend; proxy_http_version 1.1; proxy_set_header Upgr

首先,这是我用来建立websocket连接的技术:

  • Sock JS用于前端,带有STOMP协议
  • NGINX作为web代理
  • 使用STOMP协议的Spring as套接字服务器
我配置了一个NGINX实例来代理web套接字请求,到目前为止,这个NGINX配置在没有令牌验证的情况下运行良好

location /wsapp/ {
    proxy_pass http://wsbackend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host; }

想要在NGINX中验证和终止令牌,但似乎没有找到拦截作为stomp头一部分的令牌的方法。有解决方法吗?

为了让所有遇到相同问题的人受益,我设法解决了这个问题,不依赖stomp消息交换进行令牌验证

location /wsapp/ {
    proxy_pass http://wsbackend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host; }
相反,在建立websocket连接时进行令牌验证,令牌通过查询字符串传递

let socket = new SockJS('https://example.net/wsapp?myjwt=' + token);
stompClient = Stomp.over(socket);
NGINX将使用auth_jwt指令进行验证:

auth_jwt "JWT Test Realm" token=$arg_myjwt;
参考: