Nginx反向代理Websocket身份验证 - HTTP 403

sch*_*ach 8 nginx spring-security websocket spring-boot

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

这是上下文配置.

<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/localization" >
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic" />
</websocket:message-broker>
Run Code Online (Sandbox Code Playgroud)

这是客户端代码:

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
    });
});
Run Code Online (Sandbox Code Playgroud)

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

@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();
    }

}
Run Code Online (Sandbox Code Playgroud)

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

    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;
Run Code Online (Sandbox Code Playgroud)

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

我正在使用1.9.7版.

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

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

更新

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

    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;
Run Code Online (Sandbox Code Playgroud)

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

o.s.w.s.s.t.h.DefaultSockJsService - Processing transport request: GET http://testsystem:80/localization/226/3mbmu212/websocket
Run Code Online (Sandbox Code Playgroud)

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

sch*_*ach 9

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

    # 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";  
Run Code Online (Sandbox Code Playgroud)


Yan*_* Vo 5

尽管我使用的是非常经典的 HTTPS 配置,但已接受的解决方案对我不起作用:

server {
    listen 443 ssl;
    location /ws {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:8888;
    }
...
Run Code Online (Sandbox Code Playgroud)

问题是 Spring 检查源,特别是该代码给我带来了麻烦:

// in org.springframework.web.util.UriComponentsBuilder.adaptFromForwardedHeaders(HttpHeaders):
        if ((this.scheme.equals("http") && "80".equals(this.port)) ||
                (this.scheme.equals("https") && "443".equals(this.port))) {
            this.port = null;
        }
Run Code Online (Sandbox Code Playgroud)

在该代码中,方案是“http”,端口是 8888,它没有被丢弃,因为它不是标准端口。

然而,浏览器点击https://myserver/并且 443 端口被省略,因为它是默认的 HTTPS 端口。

因此端口不匹配(空!= 8888)并且来源检查失败。

您可以在 Spring WebSockets 中禁用源检查:

registry.addHandler( resgisterHandler(), "/ws" ).setAllowedOrigins( "*" );
Run Code Online (Sandbox Code Playgroud)

或者(可能更安全)您可以将方案和端口添加到 NGINX 代理配置中:

    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Port $server_port;
Run Code Online (Sandbox Code Playgroud)

如果您有兴趣,请阅读这些标题

org.springframework.web.util.UriComponentsBuilder.adaptFromForwardedHeaders(HttpHeaders)
Run Code Online (Sandbox Code Playgroud)

对于 Spring Boot 2.2.2+

从 Spring Boot 版本 2.2.2 开始,您应该X-Forwarded-*为要考虑的这些标头添加以下设置:

server.forward-headers-strategy=native
Run Code Online (Sandbox Code Playgroud)

application.properties例如)