CAS单点登录小技巧

来源:互联网 发布:杨中科c语言也能干大事 编辑:程序博客网 时间:2024/06/07 02:12

一、如何实现退出,并且退出后跳向指定位置?

@GetMapping(value = "/logout")    public String logout(HttpSession session){        logger.debug("enter logout");        session.invalidate();        return "redirect:http://localhost:8080/logout?service=http://localhost:8080/login";    }
注意在cas.properties文件中应该开启如下配置:

cas.logout.followServiceRedirects=true
从上面的代码可以看出,在退出的时候我们需要使session无效,同时应该重定向到退出的地址。

二、如何获取登录用户的用户名?

在这个地方我们将获取登录用户的用户名的功能封装成一个方法:

/**     * 从cas中获取用户名     *     * @param request     * @return     */    public static String getAccountNameFromCas(HttpServletRequest request) {        Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);        AttributePrincipal principal = assertion.getPrincipal();        return principal.getName();    }
三、如何实现免登录功能?

(1)确保casLoginView.jsp中有如下的div

 <div class="row check"> <input id="rememberMe" name="rememberMe" value="true" tabindex="4" type="checkbox" /><label for="rememberMe">remeberMe</label> </div>
(2)确保在deployerConfigContext.xml中的配置中包含以下:

 <util:list id="authenticationMetadataPopulators">        <ref bean="successfulHandlerMetaDataPopulator" />        <ref bean="rememberMeAuthenticationMetaDataPopulator" /> </util:list>
(3)确保login-webflow.xml中有以下配置:

 <binder>            <binding property="username" required="true"/>            <binding property="password" required="true"/>            <binding property="rememberMe" /> </binder>

(4)在cas.properties中开启如下配置

tgc.remember.me.maxAge=1209600
通过以上的步骤就可以实现免登录功能了。