Sto utilizzando Nginx come proxy inverso di un'applicazione di avvio Spring. Uso anche Websockets con i messaggi sockjs e stomp.Autenticazione Websocket proxy inverso Nginx - HTTP 403
Ecco la configurazione del contesto.
<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>
Ecco il codice del client:
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
});
});
ho anche Primavera di sicurezza per proteggere alcuni contenuti.
@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();
}
}
Tutto funziona alla grande, mi aspetto quando eseguo questa applicazione dietro un reverse proxy Nginx. Ecco la configurazione inversa:
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;
La connessione fallisce sempre con un codice HTTP 403.
Sto usando la versione 1.9.7.
Avete qualche idea, perché il client non viene autenticato?
Conosco domande simili, come this one ma le soluzioni non funzionano affatto.
Aggiornamento
Sono riuscito a eseguire l'applicazione su HTTP. Devo passare il token CSRF nella configurazione Nginx. Nuova configurazione:
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;
Solo il reindirizzamento manca su HTTPS. Nei registri primavera è vedere seguente voce:
o.s.w.s.s.t.h.DefaultSockJsService - Processing transport request: GET http://testsystem:80/localization/226/3mbmu212/websocket
Sembra come Nginx Proxy ha bisogno di riscrivere il alla porta giusta.