|
1
|
package com.huaheng.pc.stompwebsocket;
|
|
2
3
|
import com.huaheng.common.utils.security.ShiroUtils;
|
|
4
|
import com.huaheng.pc.stompwebsocket.service.StompWebSocketServiceImpl;
|
|
5
|
import com.huaheng.pc.system.user.domain.User;
|
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
7
8
|
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
|
|
9
|
import org.springframework.stereotype.Component;
|
|
10
11
12
|
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
|
|
13
|
import javax.annotation.Resource;
|
|
14
15
|
import java.util.Map;
|
|
16
|
@Component("sessionAuthHandshakeInterceptor")
|
|
17
|
public class SessionAuthHandshakeInterceptor implements HandshakeInterceptor {
|
|
18
19
|
@Autowired
private StompWebSocketServiceImpl stompWebSocketService;
|
|
20
21
22
23
24
25
26
27
|
@Override
public boolean beforeHandshake (ServerHttpRequest Request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {
User user = ShiroUtils.getUser();
if (user == null) {
return false;
}
|
|
28
|
// attributes.put("user ", user.getLoginName());
|
|
29
30
31
32
33
|
return true;
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
|
|
34
35
|
User user = ShiroUtils.getUser();
stompWebSocketService.sendOfflineMessageToUser(user.getLoginName());
|
|
36
37
38
39
|
}
}
|