【问题标题】:JWT authentication blocking SockJS handshake when trying to make WebSocket connection尝试建立 WebSocket 连接时,JWT 身份验证阻止 SockJS 握手
【发布时间】:2020-06-25 16:28:27
【问题描述】:

我有一个带有身份验证/授权的工作 Spring Boot 服务器。当试图打开与 SockJS 的连接时,它被我的其他安全措施阻止了。

我还不完全了解 Java 中的安全流程是如何工作的,但我很想在尝试与 SockJS 连接时需要通过握手传递 JWT 令牌。据我了解,这对于 SockJS 是不可能的。所以我只是想知道从 JWT 开始使用套接字的最佳方法是什么。另外,我知道我没有启用 CSRF,所以一些提示也会很好。

在 WebSecurityConfig.java 中:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.cors();
    httpSecurity.csrf().disable()
    .authorizeRequests()
        .antMatchers("/authenticate", "/login", "/register").permitAll()
    .anyRequest().authenticated().and()
    .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
}

在 WebSocketConfig.java 中:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")
            .setAllowedOrigins("http://localhost:5000")
            .withSockJS();
    }

}

在我的 Vue 应用程序中。尝试连接JS:

      connect() {
            this.socket = new SockJS("http://localhost:8080/ws");
            this.stompClient = Stomp.over(this.socket);
            this.stompClient.connect(
                {
                    token: 'Bearer ' +  localStorage.getItem('user-token')
                },
                frame => {
                this.connected = true;
                console.log(frame);
                this.stompClient.subscribe("/topic/greetings", tick => {
                    console.log(tick);
                    this.received_messages.push(JSON.parse(tick.body).content);
                });
                },
                error => {
                console.log(error);
                this.connected = false;
                })
      },

如果我删除所有安全配置,我可以很好地连接到 WebSocket。

我希望能够连接到 WebSocket,但现在我收到了 401,并且在握手时我找不到验证方法。

【问题讨论】:

  • 这可能会有所帮助。 baeldung.com/spring-security-websockets 看起来你需要添加一个类似于 webSecurityConf 的配置
  • 我试过这个,但这似乎可以在创建套接字后保护它们。我的问题是我似乎无法绕过自己的安全性来进行握手。我在这篇文章中遗漏了什么吗?
  • 您是否还向网络安全添加了项目 (.antMatchers( "/secured/**/**","/secured/success", "/secured/socket", "/secured/success").authenticated())?喜欢.permitAll() 或其他组的示例。您的端点可能不同
  • 您可能会遇到与那些特定路径不同的路径。你也可以在你的套接字请求中添加一个令牌。
  • 您可以将您点击的路径添加到网络安全配置的 .permitAll() 部分吗?

标签: java spring-boot vue.js jwt sockjs


【解决方案1】:

自助遮阳篷

我通过禁用WebSecurityConfig.java 中的握手路径的安全性来解决这个问题

.antMatchers(
  "/authenticate", 
  "/login", 
  "/register",
  "/secured/chat/**").permitAll()

我想我现在必须通过套接字对用户进行身份验证,因为这会使套接字对任何人开放,这是我并不真正想要的。

【讨论】:

    猜你喜欢
    • 2015-02-25
    • 2021-08-22
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多