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);
});

クライアントが接続を試みたときに表示されるのは、春が /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

すべてのリクエストがWeb アプリケーション コンテキスト内の Bean に送られ、着信 Web リクエストが適切なハンドラにマップされるため、Spring は を と照合しようとし"/mobile-socket"ます。注釈付きコントローラの導入により、 は、を持つコントローラを含むすべてのBeanで注釈を自動的に検索します。RequestMappingsHandlerMappingRequestMappingHandlerMapping@RequestMapping@Controller@MessageMapping

@MessageMappingは注釈の下でのみ定義できるため@Controller、Spring は他のものも照合しようとしRequestMappingsます。

考えられる解決策の 1 つは、インターセプターを導入して websocket リクエスト URL を処理し、特定のコントローラーに具体的にマップすることです。あなたはそれを試すことができます!

于 2016-06-02T17:12:10.430 に答える