SessionAuthHandshakeInterceptor.java 1.28 KB
package com.huaheng.framework.websocket;

import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.pc.system.user.domain.User;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;

public class SessionAuthHandshakeInterceptor implements HandshakeInterceptor {
    @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;
        }
        attributes.put("user ", user.getLoginName());
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        System.out.println("\n\n\n===========================================================【begin】===");
        System.out.println(wsHandler);
        System.out.println("===========================================================【end】===\n\n\n");
    }


}