2

我在现有 Web 应用程序中设置 websocket 配置时遇到问题。

@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer{

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/mobile");
        config.setApplicationDestinationPrefixes("/mobile-server");
        config.setUserDestinationPrefix("/mobile-user");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/mobile-socket")
        .withSockJS()
        .setInterceptors(new HttpSessionHandshakeInterceptor());
    }
}

控制器

@Controller
public class WebSocketInboxController{

    @MessageMapping("/inbox")
    @SendToUser("/mobile")
    public Map<String,Object> inbox(
    ){

        Map<String,Object> res = new HashMap<>();

        res.put("hello", "hello");

        return res;
    }

客户

const webstomp = require('webstomp-client');

const socket = webstomp.client('ws://www.dev.server.com/mobile-socket',{
  debug:true
});

socket.connect('marc@gmail.com', '123456', (client) => {
  console.log('connected');

  socket.send("/mobile-server/inbox",)
  socket.subscribe("/mobile/inbox");
}, (client, err) => {
  console.log(err);
});

当客户端尝试连接时,我看到的是 spring 尝试将 /mobile-socket 与现有 Web 应用程序的 RequestMappings 进行匹配,最后通过 @RequestMapping("/{somevar}") 找到与之匹配的一个。

我是 WebSockets 的新手,但我希望端点注册是这类连接的统称?

即使删除了错误的 RequestMapping 被击中,我似乎也无法让 MessageMapping 被击中。我在我的日志中看到了这个

AntPathRequestMatcher.matches(150) | Request '/mobile-socket' matched by universal pattern '/**'
[MSA] DEBUG [2016-06-03T11:16:21,025] FilterSecurityInterceptor.beforeInvocation(219) | Secure object: FilterInvocation: URL: /mobile-socket; Attributes: [permitAll]
[MSA] DEBUG [2016-06-03T11:16:21,025] FilterSecurityInterceptor.authenticateIfRequired(348) | Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@9055e4a6: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
[MSA] DEBUG [2016-06-03T11:16:21,025] AffirmativeBased.decide(66) | Voter: org.springframework.security.web.access.expression.WebExpressionVoter@444af45, returned: 1
[MSA] DEBUG [2016-06-03T11:16:21,025] FilterSecurityInterceptor.beforeInvocation(243) | Authorization successful
[MSA] DEBUG [2016-06-03T11:16:21,026] FilterSecurityInterceptor.beforeInvocation(256) | RunAsManager did not change Authentication object
[MSA] DEBUG [2016-06-03T11:16:21,026] FilterChainProxy.doFilter(325) | /mobile-socket at position 16 of 16 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
[MSA] DEBUG [2016-06-03T11:16:21,026] FilterChainProxy.doFilter(310) | /mobile-socket reached end of additional filter chain; proceeding with original chain
[MSA] DEBUG [2016-06-03T11:16:21,027] ExceptionTranslationFilter.doFilter(117) | Chain processed normally
[MSA] DEBUG [2016-06-03T11:16:21,027] HstsHeaderWriter.writeHeaders(130) | Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@53cc2afb
[MSA] DEBUG [2016-06-03T11:16:21,027] HttpSessionSecurityContextRepository.saveContext(352) | SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
[MSA] DEBUG [2016-06-03T11:16:21,028] SecurityContextPersistenceFilter.doFilter(120) | SecurityContextHolder now cleared, as request processing completed
4

1 回答 1

1

Spring 尝试与 匹配,"/mobile-socket"因为RequestMappings所有请求都转到HandlerMappingWeb 应用程序上下文中的 bean 以将传入的 Web 请求映射到适当的处理程序。随着注解控制器的引入,RequestMappingHandlerMapping自动查找@RequestMapping所有@Controllerbean 上的注解,包括具有@MessageMapping.

由于@MessageMapping只能在@Controller注解下定义,Spring 也会尝试匹配其他RequestMappings的。

一种可能的解决方案是引入拦截器来处理 websocket 请求 url 以专门映射到特定的控制器。你可以试一试!

于 2016-06-02T17:12:10.430 回答