小编sch*_*ach的帖子

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

我正在使用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; …
Run Code Online (Sandbox Code Playgroud)

nginx spring-security websocket spring-boot

8
推荐指数
2
解决办法
9138
查看次数

标签 统计

nginx ×1

spring-boot ×1

spring-security ×1

websocket ×1