Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!