cas后端返回html直接跳转,Cas登录服务端退出不提供跳转到登录解决

1、由于使用单点登录时服务端只提供地址重定向后可注销,但是并没有回调,只能重新输入地址进去登录,项目使用的是SpringSecurity,AbstractAuthenticationTargetUrlRequestHandler中的方法只有重定向的做法

*/

protected void handle(HttpServletRequest request, HttpServletResponse response,

Authentication authentication) throws IOException, ServletException {

String targetUrl = determineTargetUrl(request, response);

if (response.isCommitted()) {

logger.debug("Response has already been committed. Unable to redirect to "

+ targetUrl);

return;

}

redirectStrategy.sendRedirect(request, response, targetUrl);

}

初步想法是使用ajax调用,重写这个方法判断

自定义的退出的handler:

public class MyCasLogoutHandler implementsLogoutSuccessHandler{

private String logoutSuccessUrl;

private LogoutSuccessHandler urlLogoutSuccessHandler;

/**

* @return the logoutSuccessUrl

*/

public String getLogoutSuccessUrl() {

return logoutSuccessUrl;

}

/**

* @param logoutSuccessUrl

* the logoutSuccessUrl to set

*/

public void setLogoutSuccessUrl(String logoutSuccessUrl) {

Assert.isTrue(!StringUtils.hasLength(logoutSuccessUrl) || UrlUtils.isValidRedirectUrl(logoutSuccessUrl), logoutSuccessUrl + " isn't a valid redirect URL");

MySimpleUrlLogoutSuccessHandler urlLogoutSuccessHandler1 = new MtySimpleUrlLogoutSuccessHandler();

if (StringUtils.hasText(logoutSuccessUrl)) {

urlLogoutSuccessHandler1.setDefaultTargetUrl(logoutSuccessUrl);

urlLogoutSuccessHandler1.setAlwaysUseDefaultTargetUrl(true);

}

this.urlLogoutSuccessHandler = urlLogoutSuccessHandler1;

}

/**

* 成功退出

*/

@Override

@Override

public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {

this.urlLogoutSuccessHandler.onLogoutSuccess(request, response, authentication);

}

}

重写SimpleUrlLogoutSuccessHandler

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

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.SimpleUrlLogoutSuccessHandler;

public class SdpSimpleUrlLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler{

private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

@Override

public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)

throws IOException, ServletException {

this.handle(request, response, authentication);

}

/**

* 解决ajax登出问题

*/

@Override

protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication)

throws IOException, ServletException {

String targetUrl = super.determineTargetUrl(request, response);

if (response.isCommitted()) {

logger.debug("Response has already been committed. Unable to redirect to "

+ targetUrl);

return;

}

this.redirect(request, response, targetUrl);

}

/**

* 对于请求是ajax请求重定向问题的处理方法

* @param request

* @param response

* @throws IOException

*/

public void redirect(HttpServletRequest request, HttpServletResponse response,String targetUrl) throws IOException {

// 获取当前请求的路径

String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()

+ request.getContextPath();

// 判断是否为ajax请求

if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {

// 在请求头设置重定向标识、跳转的地址,ajax判断是否为重定向

response.setHeader("REDIRECT", "REDIRECT");

response.setHeader("TARGETURL", targetUrl);

response.setStatus(HttpServletResponse.SC_ACCEPTED);//设置状态码

} else {

//调用原来的重定向

redirectStrategy.sendRedirect(request, response, targetUrl);

}

}

}

前端处理:

// 退出

methods_main.loginout = function() {

var g=this;

var url="/user/logout";

var jqxhr=$.ajax({

type: "post",

dataType: "json",

url: url,

contentType : "application/json; charset=utf-8",

success: function (data,status, xhr) {

if("REDIRECT" == xhr.getResponseHeader("REDIRECT")){ //若HEADER中含有REDIRECT说明后端想重定向,

var win = window;

while(win != win.top){

win = win.top;

}

var targetUrl=jqxhr.getResponseHeader("TARGETURL");

var iframe = document.getElementById("myiframe");

iframe.src=targetUrl;//将后端重定向的地址取出来,使用iframe加载

//iframe加载完成后 跳转到登录页面

if (iframe.attachEvent) {

iframe.attachEvent("onload", function() {

//iframe加载完成后你需要进行的操作

win.location.href = "/html/main.html";

});

} else {

iframe.onload = function() {

//iframe加载完成后你需要进行的操作

win.location.href = "/html/main.html";

};

}

}

},

error:function(xhr,textStatus){

},

complete:function(xhr,textStatus){

}

});

});

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值