Security oauth2 通过 token 验证token及获取用户信息

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();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值