我正在使用Spring 4 websockets和stomp进行实验,我很难弄清楚如何在注释的消息处理方法中获取/设置当前用户和其他会话属性@MessageMapping.
该文件说,处理方法的消息可以采取委托作为参数,我发现主要是由春检索调用getUserPrincipal()在本机插座会话,然后与插座会话相关的,但我没有发现任何方式除了编写servlet过滤器并将原始请求包装回一个返回我的cookie中找到的主体的包装器之外,可以轻松地自定义此行为.
所以我的问题是:
我有一个带有websocket功能的简单弹簧应用程序,到目前为止一切正常.现在我想使用@SendToUser注释从我的服务器向特定客户端发送消息.这给了我错误"忽略消息,没有可用的主要信息".我知道我的服务器上没有任何登录信息,因此每个用户都是"匿名"且没有委托人(我现在不使用Spring安全性).但是每个用户都有一个session-id.是否有可能以某种方式使用会话ID来区分用户?我如何实现这一点,以便我的用户获得与session-id相对应的主体?
java spring spring-security spring-messaging spring-websocket
总结 我想通过STOMP实现websocket通信.在第一次(HTTP请求)websocket握手时验证用户,并使用此Principal稍后授权websocket消息.
问题 系统在第一次尝试连接到websocket端点(HTTP握手的时间)时对客户端进行身份验证.我的Spring安全过滤器和身份验证提供程序完成其工作并正确验证客户端.在此之后,我可以检查客户端是否获得了角色,我的身份验证对象也存储在SecurityContext中.(此时已建立websocket连接,并且已丢弃HTTP协议.)但是,从第一个websocket通信我得到Authentication对象是Anonymous,因为SecurityContextHolder以某种方式被清除,因为SecurityContextChannelInterceptor清除它.
Spring文档声明如下:http://docs.spring.io/autorepo/docs/spring-security/current/reference/htmlsingle/#websocket-authentication
WebSockets重用与WebSocket连接时在HTTP请求中找到的相同身份验证信息.这意味着HttpServletRequest上的Principal将被移交给WebSockets.如果您使用的是Spring Security,则会自动覆盖HttpServletRequest上的Principal.
我非常简单的过滤器
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
Authentication authResult =
new CertAuthenticationToken(null, Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN")));
authResult = getAuthenticationManager().authenticate(authResult);
if (authResult.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(authResult);
LOGGER.info("Client was authenticated.");
}
chain.doFilter(request, response);
} catch (AuthenticationException ae) {
LOGGER.error("Client was not authenticated. {}", ae);
SecurityContextHolder.clearContext();
onUnsuccessfulAuthentication((HttpServletRequest) request, (HttpServletResponse) response, ae);
throw ae;
}
}
Run Code Online (Sandbox Code Playgroud)
我非常简单的认证提供商
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
authentication.setAuthenticated(true);
return …Run Code Online (Sandbox Code Playgroud) spring spring-security spring-boot spring-messaging spring-websocket
我正在尝试将 Spring Security 与 websockets 一起使用。作为示例,我正在使用“深入研究 websockets”演讲中的spring-websocket-chat ( https://github.com/salmar/spring-websocket-chat ) \xe2\x80\x94 演示应用程序。在该应用程序中,CookieHttpSessionStrategy 用于存储会话 ID,在身份验证期间存储的 Cookie 与 /info 请求一起发送。以下是演示通过 sockjs 连接到服务器的代码(此请求发送 cookie)https://github.com/salmar/spring-websocket-chat/blob/master/src/main/webapp/js/services.js。我编写了自己的客户端,使用 sockjs 和 stomp,但在 /info 请求期间没有发送 cookie。这是我的代码
\n\n$connectButton.click(function () {\n var serverHost = $host.val();\n console.log("sockjs created");\n stomp = Stomp.over(new SockJS(serverHost));\n console.log("stomp over");\n stomp.connect({},\n function () {\n console.log("connected");\n },\n function () {\n console.log("error");\n })\n console.log("pressed");\n});\nRun Code Online (Sandbox Code Playgroud)\n 与工作Spring Boot 1.2.1.RELEASE和Spring Websockets.有一个部署运行时问题,当运行嵌入式时Jetty 9,我不能java.security.Principal成功地伪装用户()当app部署在除localhost之外的任何其他地方.
我咨过过
下面的配置(我相信)已经"升级"了一个请求
@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
// see http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-handle-broker-relay
// requires an external broker like AMQP or RabbitMQ
//registry.enableStompBrokerRelay("/queue/", "/topic/");
// XXX This might wind up being the impl we actually deploy; but be aware it has certain constraints
// see http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-message-flow
registry.enableSimpleBroker("/queue/", "/topic/");
registry.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/cards").setHandshakeHandler(new UserHandler()).withSockJS(); …Run Code Online (Sandbox Code Playgroud)