CAS logout的時候,默認是會重定向到cas server端的logout頁面。
現在的需求是重定向到登錄頁面。
在應用中, 我還遇到直接ogout(只做清空session, cookie), 而不去重定向到cas 的logout頁面,這種情況下:
如果不關閉瀏覽器, 直接再次輸入Application的URL, 會繞過CAS認證,照常登入。
吃問題困擾了幾天,看了一下CASserver端的代碼,恍然大悟:
LogoutController有一個開關變量:
private boolean followServiceRedirects;
並有一段邏輯:
if (this.followServiceRedirects && service != null) {
return new ModelAndView(new RedirectView(service));
}
service是個啥:
final String service = request.getParameter("service");
如果在logout的url後面加上你要重定向的那個頁面, 就可以自定義登出頁面了:
所以,我的方案就出來了:
在cas-servlet.xml中,設置logoutController的followServiceRedirects=true
<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:followServiceRedirects="true"/>
application在登出的時候,除了清空session和cookie外, 重定向的url加上:
var apphostname = window.location.hostname
var appport = window.location.port;
var callbackurl='?service=http%3A%2F%2F'+apphostname +'%3A'+appport + '%2Fapplication'
window.location = logoutUrl+ callbackurl;