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

单点登录 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() {
        ServletListenerRegistrationBean 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()+"\n" +
                "     退出登录\n" +
                "";
    }
    @RequestMapping("loginout")
    public String loginout(HttpSession session) {
        session.invalidate();
        return "Login out success";
    }

}

  然后在8082子项目选择退出

单点登录 CAS 5.* -如何单点退出 - 3_第1张图片

刷新 8081子项目

单点登录 CAS 5.* -如何单点退出 - 3_第2张图片

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

单点登录 CAS 5.* -如何单点退出 - 3_第3张图片

8082退出使用的

session.invalidate();方法进行的退出,请注意

 

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

你可能感兴趣的:(单点登录 CAS 5.* -如何单点退出 - 3)