Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

357
Vistas
How to mix csrf enabled and disabled spring websockets in one application

I have an application where I need two different websocket setups:

  1. one for allowing communication between the application and remote Java-based clients
    • uses stateless comms (auth token is included in each request, also in the websocket connect request)
    • csrf needs to be disabled
  2. one for allowing async push notifications from the application to its own web UI.
    • uses normal session authentication
    • csrf needs to be, or should preferably be, enabled (correct me if I'm wrong?)

Now, in Spring, to disable cross origin checking for websockets one needs to extend AbstractSecurityWebSocketMessageBrokerConfigurer e.g. as follows:

@Configuration
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
    @Override
    protected void configureInbound(final MessageSecurityMetadataSourceRegistry messages) {
        messages.anyMessage().authenticated();
    }

    @Override
    protected boolean sameOriginDisabled() {
        return true;
    }
}

The question is, how can I have it disabled for some websockets and enabled for others?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you take a look at the method AbstractSecurityWebSocketMessageBrokerConfigurer#configureClientInboundChannel(ChannelRegistration), when you have the sameOriginDisabled set to false, it simply register a CsrfChannelInterceptor:

if (!sameOriginDisabled()) {
    registration.setInterceptors(this.context.getBean(CsrfChannelInterceptor.class));
}

And after that, it calls the customizeClientInboundChannel(ChannelRegistration) method.

I can't test now, but I think you can override the method customizeClientInboundChannel(ChannelRegistration) and do the following:

@Override
protected void customizeClientInboundChannel(ChannelRegistration registration) {
     registration.addInterceptor(myCustomCsrfChannelInterceptor());
}

private CsrfChannelInterceptor myCustomCsrfChannelInterceptor() {
    return new MyCustomCsrfChannelInterceptor();
}

private static class MyCustomCsrfChannelInterceptor {
    private MessageMatcher<Object> matcher = //create your MessageMatcher with your rules

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        if (!this.matcher.matches(message)) {
            return message;
        }
        //copy the content from `CsrfChannelInterceptor`
    }
}

In summary, what you are doing is creating a custom CsrfChannelInterceptor that will use a custom MessageMatcher with your own rules to check if it should apply to that Message, and the rest is just a copy of the original interceptor.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda