Technology Sharing

Explore springboot integrated websocket, can't connect, but can connect using netty-websocket

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Reason: Spring-security is integrated. Websocket requests are intercepted and no logs are output (because the general spring-related log level is warn. There are too many logs in debug mode).

Then you can modify the undertow log level to view the log

  1. # 日志配置
  2. logging:
  3. level:
  4. com.cas.wict: debug
  5. org.springframework: warn
  6. org.springframework.web: debug
  7. io.undertow: debug

Key log output

  1. 2024-07-11 10:15:43.292 [XNIO-1 task-1] DEBUG io.undertow.request.security
  2. - Attempting to authenticate /ws, authentication required: false
  3. 2024-07-11 10:15:43.293 [XNIO-1 task-1] DEBUG io.undertow.request.security
  4. - Authentication outcome was NOT_ATTEMPTED with method io.undertow.security.impl.CachedAuthenticatedSessionMechanism@7fedbc60 for /ws
  5. 2024-07-11 10:15:43.293 [XNIO-1 task-1] DEBUG io.undertow.request.security
  6. - Authentication result was ATTEMPTED for /ws

It was found that the security might have intercepted the ws request path (why it was intercepted? I guess that although it was written as ws, it was still http when it was initiated, and was upgraded to websocket by the background)

Solution: Release the verification and write a separate filter to verify the ws request

Why was it possible to connect using netty-websocket before? That's because netty didn't use the filter chain of spring-security