1、注入 tokenStore
@Autowired
private RedisTokenStore tokenStore;
// 通过token值进行验证
OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(token);
if (null != oAuth2AccessToken) {
OAuth2Authentication auth2Authentication = tokenStore.readAuthentication(token);
OpenUserDetails userDetails = (OpenUserDetails) auth2Authentication.getUserAuthentication().getPrincipal();
}
如:socket链接认证token有效性
import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
/**
* socket 配置
*
* @author kou
*/
@Slf4j
@Configuration
public class SocketIOConfig {
@Autowired
private SocketIOProperties socketIOProperties;
@Autowired
private RedisTokenStore tokenStore;
@Bean
public SocketIOServer socketIOServer() {
SocketConfig socketConfig = new SocketConfig();
socketConfig.setTcpNoDelay(true);
socketConfig.setSoLinger(0);
socketConfig.setReuseAddress(true);
com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
config.setSocketConfig(socketConfig);
// config.setHostname(socketIOProperties.getHost());
config.setPort(socketIOProperties.getPort());
config.setBossThreads(socketIOProperties.getBossCount());
config.setWorkerThreads(socketIOProperties.getWorkCount());
config.setAllowCustomRequests(socketIOProperties.isAllowCustomRequests());
config.setUpgradeTimeout(socketIOProperties.getUpgradeTimeout());
config.setPingTimeout(socketIOProperties.getPingTimeout());
config.setPingInterval(socketIOProperties.getPingInterval());
int workThreads = !StringUtils.isBlank(socketIOProperties.getThreads()) && socketIOProperties.getThreads().matches("[\\d]{1,6}") ? Integer.parseInt(socketIOProperties.getThreads()) : 100;
config.setWorkerThreads(workThreads);
// 连接认证
config.setAuthorizationListener(new AuthorizationListener() {
@Override
public boolean isAuthorized(HandshakeData data) {
String token = data.getSingleUrlParam("token");
if (StringUtils.isNotBlank(token)) {
// /oauth/check_token
OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(token);
if (null != oAuth2AccessToken) {
// 认证成功
return true;
}
}
log.info("认证失败");
return false;
}
});
// 性能优化
config.getSocketConfig().setReuseAddress(true);
config.getSocketConfig().setSoLinger(0);
config.getSocketConfig().setTcpNoDelay(true);
config.getSocketConfig().setTcpKeepAlive(true);
return new SocketIOServer(config);
}
/**
* 开启SocketIOServer注解支持
*
* @param socketServer
* @return
*/
@Bean
public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
return new SpringAnnotationScanner(socketServer);
}
@OnConnect
public void onConnect(SocketIOClient client) {
// 获取token
String token = client.getHandshakeData().getSingleUrlParam("token");
if (StringUtils.isNotBlank(token)) {
// 查询 token
OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(token);
if (null != oAuth2AccessToken) {
log.info("token store 获取token信息成功");
OAuth2Authentication auth2Authentication = tokenStore.readAuthentication(token);
OpenUserDetails userDetails = (OpenUserDetails) auth2Authentication.getUserAuthentication().getPrincipal();
return;
}
}
// 断开链接
log.info("认证失败,断开链接 {}", client.getSessionId().toString());
client.disconnect();
}
}