Blame view

src/main/java/com/huaheng/pc/stompwebsocket/SessionAuthHandshakeInterceptor.java 1.54 KB
1
package com.huaheng.pc.stompwebsocket;
周峰 authored
2
3

import com.huaheng.common.utils.security.ShiroUtils;
周峰 authored
4
import com.huaheng.pc.stompwebsocket.service.StompWebSocketServiceImpl;
周峰 authored
5
import com.huaheng.pc.system.user.domain.User;
周峰 authored
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.context.annotation.Lazy;
周峰 authored
8
9
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
周峰 authored
10
import org.springframework.stereotype.Component;
周峰 authored
11
12
13
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
周峰 authored
14
import javax.annotation.Resource;
周峰 authored
15
16
import java.util.Map;
周峰 authored
17
@Component("sessionAuthHandshakeInterceptor")
周峰 authored
18
public class SessionAuthHandshakeInterceptor implements HandshakeInterceptor {
周峰 authored
19
    @Autowired
20
    @Lazy
周峰 authored
21
    private StompWebSocketServiceImpl stompWebSocketService;
周峰 authored
22
23
24
25
26
27
28
29
    @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;
        }
30
//        attributes.put("user ", user.getLoginName());
周峰 authored
31
32
33
34
35
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
周峰 authored
36
37
        User user = ShiroUtils.getUser();
        stompWebSocketService.sendOfflineMessageToUser(user.getLoginName());
周峰 authored
38
39
40
41
    }


}