Cas登录服务端退出不提供跳转到登录解决

2018-06-23  本文已影响0人  掖莯圷

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){
                    
                }
            });
        });
    };
上一篇下一篇

猜你喜欢

热点阅读