baoshiwei
2025-04-19 9d960ed0058f9087f49e9741a9af06c3f9116eb0
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
package com.zhitan.framework.security.handle;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.zhitan.system.domain.SysSocial;
import com.zhitan.system.service.ISysSocialService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.alibaba.fastjson2.JSON;
import com.zhitan.common.constant.Constants;
import com.zhitan.common.core.domain.AjaxResult;
import com.zhitan.common.core.domain.model.LoginUser;
import com.zhitan.common.utils.MessageUtils;
import com.zhitan.common.utils.ServletUtils;
import com.zhitan.common.utils.StringUtils;
import com.zhitan.framework.manager.AsyncManager;
import com.zhitan.framework.manager.factory.AsyncFactory;
import com.zhitan.framework.web.service.TokenService;
 
/**
 * 自定义退出处理类 返回成功
 * 
 * @author zhitan
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
{
    @Resource
    private TokenService tokenService;
 
    @Value("${keycloak.server-url}")
    private String keycloakServerUrl;
 
    @Value("${keycloak.realm}")
    private String keycloakRealm;
 
 
    @Resource
    private ISysSocialService sysSocialService;
 
    /**
     * 退出处理
     * 
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            String userName = loginUser.getUsername();
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
            // 记录用户退出日志
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(userName, Constants.LOGOUT, MessageUtils.message("user.logout.success")));
            SysSocial social = sysSocialService.selectByUserId(loginUser.getUserId());
            if (social == null) {
                return;
            }
 
            String logoutUrl = keycloakServerUrl + "/realms/" + keycloakRealm + "/protocol/openid-connect/logout";
            HttpRequest req = HttpRequest.get(logoutUrl)
                    .form("refresh_token", social.getRefreshToken())
                    .form("id_token_hint", social.getIdToken());
 
 
            HttpResponse rep = req.execute();
            if (rep.isOk()) {
                System.out.println("1234");
            }
        }
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success(MessageUtils.message("user.logout.success"))));
    }
}