
com.healthy.security.browser.logout.HealthyLogoutSuccessHandler Maven / Gradle / Ivy
package com.healthy.security.browser.logout;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.healthy.common.core.support.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.social.security.SocialUserDetails;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
import static com.healthy.common.core.constant.CommonConstants.APPLICATION_JSON_UTF8_VALUE;
/**
* HealthyLogoutSuccessHandler
*
* The default exit success handler.
* If healthy.security.browser.signOutUrl is set, then jump to the configured address,
* if not configured, then return the response in json format.
*
*
* @author xiaomingzhang
*/
@Slf4j
public class HealthyLogoutSuccessHandler implements LogoutSuccessHandler {
private final String signOutSuccessUrl;
public HealthyLogoutSuccessHandler(String signOutSuccessUrl) {
this.signOutSuccessUrl = signOutSuccessUrl;
}
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
if (Objects.isNull(authentication)) {
log.debug("未登录.");
response.setContentType(APPLICATION_JSON_UTF8_VALUE);
response.getWriter().write(JSONUtil.toJsonStr(Message.failed("未登录")));
} else {
Object principal = authentication.getPrincipal();
String userName = principal instanceof SocialUserDetails ? ((SocialUserDetails) principal).getUserId() : (String) principal;
log.debug("当前用户:[{}] 退出成功.", userName);
if (StrUtil.isBlank(signOutSuccessUrl)) {
response.setContentType(APPLICATION_JSON_UTF8_VALUE);
response.getWriter().write(JSONUtil.toJsonStr(Message.ok("退出成功")));
} else {
response.sendRedirect(signOutSuccessUrl);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy