import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class SimpleCORSFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req= (HttpServletRequest) request;
req.getSession().setAttribute("ipAddr",req.getRemoteHost());
HttpServletResponse res = (HttpServletResponse)response;
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
res.setHeader("Access-Control-Max-Age", "3600");
res.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token,timestamp");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
import com.neo.websocket.WebSocketConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
@Component
@ServerEndpoint(value = "/wsTask/{userId}/{tId}", configurator = WebSocketConfig.class)
public class TaskWebSocket {
private static AsynSendMsg asyncSendMsg;
@Autowired
public void setAsynSendMsg(AsynSendMsg asyncSendMsg) {
TaskWebSocket.asyncSendMsg = asyncSendMsg;
}
public Logger log = LogManager.getLogger(getClass());
private Session session;
private String userId;
private static CopyOnWriteArraySet<TaskWebSocket> webSockets =new CopyOnWriteArraySet<>();
private static ConcurrentHashMap<String,Session> sessionPool = new ConcurrentHashMap<String,Session>();
public Session getSession() {
return session;
}
public String getUserId() {
return userId;
}
public static CopyOnWriteArraySet<TaskWebSocket> getWebSockets() {
return webSockets;
}
public static ConcurrentHashMap<String, Session> getSessionPool() {
return sessionPool;
}
@OnOpen
public void onOpen(Session session, @PathParam(value="userId")String userId, @PathParam(value="tId")String tId) {
try {
this.session = session;
this.userId = userId;
webSockets.add(this);
sessionPool.put(userId, session);
log.info("【websocket消息】有新的连接,总数为:"+webSockets.size());
asyncSendMsg.sendOneMessage(userId,tId,session);
} catch (Exception e) {
}
}
@OnClose
public void onClose() {
try {
webSockets.remove(this);
sessionPool.remove(this.userId);
log.info("【websocket消息】连接断开,总数为:"+webSockets.size());
} catch (Exception e) {
}
}
@OnMessage
public void onMessage(String message) throws InterruptedException {
log.info("【websocket消息】收到客户端消息:"+message);
}
@OnError
public void onError(Session session, Throwable error) {
log.error("用户错误,原因:"+error.getMessage());
error.printStackTrace();
}
public void sendAllMessage(String message) {
log.info("【websocket消息】广播消息:"+message);
for(TaskWebSocket webSocket : webSockets) {
try {
if(webSocket.session.isOpen()) {
webSocket.session.getAsyncRemote().sendText(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void sendOneMessage(String userId,String message,Session session) throws InterruptedException {
while (sessionPool.get(userId)!= null&&sessionPool.get(userId).isOpen()) {
try {
log.info("【websocket消息】 单点消息:" + message);
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
Thread.sleep(2000);
}
}
public void sendMoreMessage(String[] userIds, String message) {
for(String userId:userIds) {
Session session = sessionPool.get(userId);
if (session != null&&session.isOpen()) {
try {
log.info("【websocket消息】 单点消息:"+message);
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
import com.alibaba.fastjson.JSONObject;
import com.neo.dao.UserInfoDao;
import com.neo.utils.IpUtils;
import com.neo.utils.JwtUtils;
import com.neo.utils.SpringUtils;
import io.jsonwebtoken.ExpiredJwtException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.List;
@Component
@Configuration
@WebListener
public class WebSocketConfig extends ServerEndpointConfig.Configurator {
public Logger logger = LoggerFactory.getLogger(getClass());
@Bean
public ServerEndpointExporter serverEndpointExporter()
{
return new ServerEndpointExporter();
}
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
List<String> list = request.getHeaders().get("Sec-WebSocket-Protocol");
response.getHeaders().put("Sec-WebSocket-Protocol",list);
if(list == null ||list.size()==0) {
throw new RuntimeException("无token参数");
}else {
for (String token : list) {
Boolean verfy = JwtUtils.verfy(token);
if(!verfy) {
logger.info("token无效");
throw new RuntimeException("token无效");
}else {
try {
HttpSession session = (HttpSession) request.getHttpSession();
String addr = "";
if (session != null) {
addr = session.getAttribute("ipAddr").toString();
}
String subject = JwtUtils.parseJwt(token).getSubject();
JSONObject jsonObject = JSONObject.parseObject(subject);
String ip = jsonObject.getString("ip");
String fingerprintToken = jsonObject.getString("fingerprint");
String userAgent = request.getHeaders().get("User-Agent").get(0);
String fingerprint = userAgent;
if (!ip.equals(addr) || !fingerprintToken.contains(fingerprint)) {
logger.info("网络环境改变,请重新登录!");
throw new RuntimeException("网络环境改变,请重新登录!");
} else {
UserInfoDao userInfoDao = SpringUtils.getBean("userInfoDao", UserInfoDao.class);
int i = userInfoDao.countToken(token);
if (i > 0) {
super.modifyHandshake(sec, request, response);
} else {
logger.info("token已失效,请重新登录!");
throw new RuntimeException("token已失效,请重新登录!");
}
}
} catch (ExpiredJwtException e) {
logger.info("token已过期");
throw new RuntimeException("token已过期");
} catch (Exception e) {
logger.info("token已失效");
e.printStackTrace();
throw new RuntimeException("token已失效");
}
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90