forked from kidgrow-microservices-platform

zhaoxiaohao
2021-03-18 deb1110ca94cb0ac7bcdc51b4e8dd00407792a94
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
package com.kidgrow.zuul.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.kidgrow.authclient.util.AuthUtils;
import com.kidgrow.zuul.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
 
import javax.servlet.http.HttpServletRequest;
@Service
public class TokenServiceImpl implements TokenService {
 
    @Autowired
    private TokenStore tokenStore;
    /**
     * 退出的接口
     * @param request
     */
    @Override
    public void logout(HttpServletRequest request) {
        Assert.notNull(tokenStore, "tokenStore must be set");
        String token = request.getParameter("token");
        if (StrUtil.isEmpty(token)) {
            token = AuthUtils.extractToken(request);
        }
        if(StrUtil.isNotEmpty(token)){
            OAuth2AccessToken existingAccessToken = tokenStore.readAccessToken(token);
            OAuth2RefreshToken refreshToken;
            if (existingAccessToken != null) {
                if (existingAccessToken.getRefreshToken() != null) {
                    refreshToken = existingAccessToken.getRefreshToken();
                    tokenStore.removeRefreshToken(refreshToken);
                }
                tokenStore.removeAccessToken(existingAccessToken);
            }
        }
    }
}