单点登录 CAS 5.* -如何单点退出 - 3

登录已经完成了,如何实现单点退出,即一处退出,处处退出。

在client增加类

import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: L.swallow
 * @Date: 2019/1/14 15:48
 */
@Configuration
public class CasConfig {

    /**
     * 用于实现单点登出功能
     */
    @Bean
    public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> singleSignOutHttpSessionListener() {
        ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> listener = new ServletListenerRegistrationBean<>();
        listener.setEnabled(true);
        listener.setListener(new SingleSignOutHttpSessionListener());
        listener.setOrder(1);
        return listener;
    }

    /**
     * 该过滤器用于实现单点登出功能
     */
    @Bean
    public FilterRegistrationBean singleSignOutFilter() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new SingleSignOutFilter());
        filterRegistration.setEnabled(true);
        filterRegistration.addUrlPatterns("/*");
        filterRegistration.addInitParameter("casServerUrlPrefix", "http://192.168.10.69:8080/singin");
        filterRegistration.addInitParameter("serverName","SingleSignOutFilter");
        filterRegistration.setOrder(3);
        return filterRegistration;
    }

//    @Bean
//    @Order(1)
//    public FilterRegistrationBean logoutFilter(){
//        FilterRegistrationBean bean= new FilterRegistrationBean();
//        SingleSignOutFilter filter= new SingleSignOutFilter();
//        filter.setCasServerUrlPrefix("http://localhost:8080/singin");
//        bean.setName("SingleSignOutFilter");
//        bean.setFilter(filter);
//        return bean;
//    }


}

修改LoginController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;

/**
 * @Author: L.swallow
 * @Date: 2019/1/14 14:52
 */
@RestController
public class LoginController {

    @RequestMapping("login")
    public String login(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        return "Welcome to app1 " + principal.getName()+"<a href=\"http://192.168.10.69:8080/singin/logout?service=http://192.168.10.69:8081/loginout\">\n" +
                "     退出登录\n" +
                "</a>";
    }
    @RequestMapping("loginout")
    public String loginout(HttpSession session) {
        session.invalidate();
        return "Login out success";
    }

}

  然后在8082子项目选择退出

刷新 8081子项目

已成功退出,刷新8082  其实也退出了

8082退出使用的

session.invalidate();重置session,
如若使用的shiro或者springsecret,没啥区别,调用其登出接口就OK

 

posted @ 2019-01-14 17:57  LewsKay  阅读(1711)  评论(0编辑  收藏  举报