Nginx   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Nginx反向代理Websocket身份验证 – HTTP 403大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用Nginx作为Spring启动应用程序的反向代理.我还使用带有sockjs和stomp消息的Websockets.

这是上下文配置.

@H_197_7@<>broker application-desTination-prefix="/app">
    <>broker prefix="/topic" />
broker>

这是客户端代码:

@H_197_7@var socket = new SockJS(entryPointUrl);
var stompClient = Stomp.over(socket);

var _this = this;

stompClient.connect({},function () {
    stompClient.subscribe('/app/some-url',function (messagE) {
         // do some stuff
    });
});

我也是Spring Security来保护一些内容.

@H_197_7@@Configuration
@Order(4)
public static class FrontendSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(httpSecurity http) throws Exception {
        http.authorizerequests()
                .antMatchers("/js/**","/css/**","/webjars/**").permitAll()
                .anyrequest().authenticated()
                .and()
                .formLogin().loginPage("/login").permitAll()
                .and()
                .logout().permitAll();
    }

}

当我在Nginx反向代理后面运行这个应用程序时,一切都很好.这是相反的配置:

@H_197_7@    proxy_pass http://testsysten:8080;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-ForWARDed-For $proxy_add_x_forWARDed_for;

    # WebSocket support (Nginx 1.4)
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;

    # Max body size
    client_max_body_size 10M;

连接总是失败,出现http 403代码.

我正在使用1.9.7版.

你有什么想法,为什么客户端没有得到认证?

我知道类似的问题,比如this one,但解决方案根本不起作用.

更新

我设法通过http运行应用程序.我需要在Nginx配置中传递CSRF令牌.新配置是:

@H_197_7@    proxy_pass http://testsysten:8080;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-ForWARDed-For $proxy_add_x_forWARDed_for;

    # Pass the csrf token (see https://de.wikipedia.org/wiki/Cross-Site-request-Forgery)
    # Default in Spring Boot
    proxy_pass_header X-XSRF-TOKEN;

    # WebSocket support (Nginx 1.4)
    proxy_http_version 1.1;

只有缺少是通过httpS重定向.在Spring日志中可以看到以下条目:

@H_197_7@o.s.w.s.s.t.h.DefaultSockJsservice - Processing transport request: GET http://testsystem:80/localization/226/3mbmu212/websocket

好像Nginx Proxy需要重写到正确的端口.

最佳答案
我自己解决了这个问题.基本上,如果要使用Websocket和Spring Security,Nginx需要传递一些额外的标头值.需要将以下行添加到Nginx配置中的位置部分:

@H_197_7@    # Pass the csrf token (see https://de.wikipedia.org/wiki/Cross-Site-request-Forgery)
    # Default in Spring Boot and required. Without it Nginx suppresses the value
    proxy_pass_header X-XSRF-TOKEN;

    # Set origin to the real instance,otherwise a of Spring security check will fail
    # Same value as defined in proxy_pass
    proxy_set_header Origin "http://testsysten:8080";  

大佬总结

以上是大佬教程为你收集整理的Nginx反向代理Websocket身份验证 – HTTP 403全部内容,希望文章能够帮你解决Nginx反向代理Websocket身份验证 – HTTP 403所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: