单点登出的实现比较简单,就是简单的几个配置。
如果直接调用cas的logout的url进行登出,则会暴露cas的登出界面
显然这不是我们想要的,通常登出的需求是:退出当前账户,然后将url重定向到登录界面。
于是我用了一个蠢笨的方式来实现:
干货模式开启:
1、在页面的退出按钮下的function
function fn_logout(){
$.ajax({
url:"../user/logout",
type:'post'
});
window.location.href="https://cas.demo.com:8443/cas/logout?service=http://127.0.0.1:8089/portal/user/login";
}
老规矩,贴图片详解
2、然后我把Controller里的logout方法贴出来
@RequestMapping("logout")
public void logout(HttpSession session) {
//清楚session相关记录
session.removeAttribute("admin");
session.removeAttribute(com.common.util.SessionListener.LISTENER_NAME);
}
3、进行cas服务器配置文件的修改 cas\WEB-INF\cas-servlet.xml
<bean id="logoutController" class="org.jasig.cas.web.LogoutController"
p:centralAuthenticationService-ref="centralAuthenticationService"
p:logoutView="casLogoutView"
p:warnCookieGenerator-ref="warnCookieGenerator"
p:ticketGrantingTicketCookieGenerator-ref="ticketGrantingTicketCookieGenerator"
p:servicesManager-ref="servicesManager"
p:followServiceRedirects="${cas.logout.followServiceRedirects:true}"/>
注意最后一行的p:followServiceRedirects="${cas.logout.followServiceRedirects:true}"原本属性为false。
意思是:成功Logout后,如果包含Service参数,则重定向到Service指定的网址。
这样就可以在页面logout的时候,同时清理掉当前用户的session,又redirect到当前系统的登录界面,从而省去了caslogout页面的修改或单纯的实现:登出后释放当前用户的session,跳转到系统登录界面。
来源:oschina
链接:https://my.oschina.net/u/697871/blog/803573