优化授权中心登出接口,支持自定义跳转地址
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.central.oauth.config;
|
package com.central.oauth.config;
|
||||||
|
|
||||||
import com.central.common.constant.SecurityConstants;
|
import com.central.common.constant.SecurityConstants;
|
||||||
|
import com.central.oauth.handler.OauthLogoutSuccessHandler;
|
||||||
import com.central.oauth.mobile.MobileAuthenticationSecurityConfig;
|
import com.central.oauth.mobile.MobileAuthenticationSecurityConfig;
|
||||||
import com.central.oauth.openid.OpenIdAuthenticationSecurityConfig;
|
import com.central.oauth.openid.OpenIdAuthenticationSecurityConfig;
|
||||||
import com.central.common.config.DefaultPasswordConfig;
|
import com.central.common.config.DefaultPasswordConfig;
|
||||||
@@ -84,8 +85,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
.and()
|
.and()
|
||||||
.logout()
|
.logout()
|
||||||
.logoutUrl(SecurityConstants.LOGOUT_URL)
|
.logoutUrl(SecurityConstants.LOGOUT_URL)
|
||||||
.logoutSuccessUrl(SecurityConstants.LOGIN_PAGE)
|
.logoutSuccessHandler(new OauthLogoutSuccessHandler())
|
||||||
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
|
|
||||||
.addLogoutHandler(oauthLogoutHandler)
|
.addLogoutHandler(oauthLogoutHandler)
|
||||||
.clearAuthentication(true)
|
.clearAuthentication(true)
|
||||||
.and()
|
.and()
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.central.oauth.handler;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.central.common.model.Result;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.web.DefaultRedirectStrategy;
|
||||||
|
import org.springframework.security.web.RedirectStrategy;
|
||||||
|
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2020/3/10
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class OauthLogoutSuccessHandler implements LogoutSuccessHandler {
|
||||||
|
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
||||||
|
String redirectUri = request.getParameter("redirectUri");
|
||||||
|
if (StrUtil.isNotEmpty(redirectUri)) {
|
||||||
|
//重定向指定的地址
|
||||||
|
redirectStrategy.sendRedirect(request, response, redirectUri);
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.OK.value());
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
|
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||||
|
PrintWriter writer = response.getWriter();
|
||||||
|
String jsonStr = JSON.toJSONString(Result.succeed("登出成功"));
|
||||||
|
writer.write(jsonStr);
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user