您的位置:首页 > 其它

cas自定义登录页面

2014-06-12 14:58 260 查看
我在做cas自定义页面时,在网上查了很多资料,真是经历波折,总算是弄出来了。使用的cas版本是cas-server-3.5.2。

有时候各个应用希望有独立风格的登录页面,而不是cas提供的统一登录页面,网上现在有几种方式:iframe, javascript。本文参考了https://wiki.jasig.org/display/CAS/Using+CAS+without+the+Login+Screen, 并在其基础上实现了登录出错重定向至自定义登录页面。
由于cas登录首先要获取lt与execution这两个参数,因此,主要实现方案就是通过在自定义登录页面加入标志位,提交到cas的登录页面casLoginView.jsp,这个页面会根据标志位进行二次提交。如果用户名密码失败还要求能够返回到原登录页面并给出提示,主要实现方式是改变cas的web flow,在提交的时候判断是否是自定义页面登录,是的话进入一个新的状态,这个状态完成页面跳转,并回传错误信息。
1.自定义登录页面:login.html,
<!DOCTYPE html>
<html>
<head>
<title>login.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<script type="text/javascript">
function getQueryStringByName(name) {
var result = location.search.match(new RegExp("[\?\&]" + name+ "=([^\&]+)","i"));
if(result == null || result.length < 1){
return "";
}
return result[1];
}
var info = getQueryStringByName('info');
if (info == "error")
alert("用户名密码错误!");
</script>
<form method="GET" action="https://cas.demo.com:8443/cas/login">
<p>Username : <input type="text" name="username" /></p>
<p>Password : <input type="password" name="password" /></p>
<p>Remember me : <input type="checkbox" name="rememberMe" value="true" /></p>
<p><input type="submit" value="Login !" /></p>
<input type="hidden" name="auto" value="true" />
<input type="hidden" name="login_from" value="http://app1.demo.com:5090/Client1/login.html" />
<input type="hidden" name="service" value="http://app1.demo.com:5090/Client1/fit/index.jsp" />
</form>
</body>
</html>

login.html这个页面放在自己的项目中,在web.xml中的配置cas拦截器的时候要放行;

例如:<filter-mapping>
<filter-name>CAS Filter</filter-name>
<url-pattern>/fit/*</url-pattern>

  </filter-mapping>

这里只对fit文件夹下的页面请求拦截,把login.html放在外面。不拦截

<form method="GET" action="http://域名或ip:端口号/cas/login">这个就是请求cas登录的页面。

<input type="hidden" name="login_from" value="http://app1.demo.com:5090/Client1/login.html" />这个的路径就是你这个login.html的请求路径;

<input type="hidden" name="service" value="http://app1.demo.com:5090/Client1/fit/index.jsp" />这个是你输入用户名密码验证通过之后前往的页面。

2.接着修改cas的默认登录页面:WEB-INF/view/jsp/default/ui/casLoginView.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%
String auto = request.getParameter("auto");
if (auto != null && auto.equals("true")) {
%>
<html>
    <head>
        <script language="javascript">
            function doAutoLogin() {
                document.forms[0].submit();
            }
        </script>
    </head>
    <body onload="doAutoLogin();">
        <form id="credentials" method="POST" action="<%= request.getContextPath() %>/login?service=<%= request.getParameter("service") %>">
            <input type="hidden" name="lt" value="${loginTicket}" />
            <input type="hidden" name="execution" value="${flowExecutionKey}" />
            <input type="hidden" name="_eventId" value="submit" />
            <input type="hidden" name="username" value="<%= request.getParameter("username") %>" />
            <input type="hidden" name="password" value="<%= request.getParameter("password") %>" />
            <input type="hidden" name="login_from" value="<%= request.getParameter("login_from") %>" />
            <% if ("true".equals(request.getParameter("rememberMe"))) {%>
                <input type="hidden" name="rememberMe" value="true" />
            <% } %>
             
            <input type="submit" value="Submit" style="visibility: hidden;" />
        </form>
    </body>
</html>
<%
} else {
%>
<jsp:directive.include file="includes/top.jsp" />
......
<jsp:directive.include file="includes/bottom.jsp" />
<%
}
%>

3.修改AuthenticationViaFormAction.java文件在:cas-server-3.5.2\cas-server-core\src\main\java\org\jasig\cas\web\flow中

修改submit方法

try {
            WebUtils.putTicketGrantingTicketInRequestScope(context, this.centralAuthenticationService.createTicketGrantingTicket(credentials));
            putWarnCookieIfRequestParameterPresent(context);
            return "success";
        } catch (final TicketException e) {
            String login_from = context.getRequestParameters().get("login_from");
            if (login_from != null && login_from.length() > 0) {
                context.getRequestScope().put("redirectUrl", login_from + "?info=error");
                return "customizedRedirect";
            }
            populateErrorsInstance(e, messageContext);
            if (isCauseAuthenticationException(e)) {
                return getAuthenticationExceptionEventId(e);
            }
            return "error";
        }
其中
 String login_from = context.getRequestParameters().get("login_from");
            if (login_from != null && login_from.length() > 0) {
                context.getRequestScope().put("redirectUrl", login_from + "?info=error");
                return "customizedRedirect";
            }

为添加的部分。修改后编译为class文件,将包cas-server-core-3.5.2.jar用压缩包方式打开找到AuthenticationViaFormAction.class文件替换。

4.cas-server-webapp工程中,修改WEB-INF/login-webflow.xml

<action-state id="realSubmit">
        <evaluate expression="authenticationViaFormAction.submit(flowRequestContext, flowScope.credentials, messageContext)" />
        <!--
          To enable LPPE on the 'warn' replace the below transition with:
          <transition on="warn" to="passwordPolicyCheck" />

          CAS will attempt to transition to the 'warn' when there's a 'renew' parameter
          and there exists a ticketGrantingId and a service for the incoming request.
        -->
        <transition on="warn" to="warn" />
        <!--
          To enable LPPE on the 'success' replace the below transition with:
          <transition on="success" to="passwordPolicyCheck" />
        -->
        <transition on="success" to="sendTicketGrantingTicket" />
        <transition on="error" to="generateLoginTicket" />
       <!--加入下面这句话该transition , 当验证失败之后转到自定义页面 -->  
        <transition on="customizedRedirect" to="customizedRedirectView" />
        <transition on="accountDisabled" to="casAccountDisabledView" />
        <transition on="mustChangePassword" to="casMustChangePassView" />
        <transition on="accountLocked" to="casAccountLockedView" />
        <transition on="badHours" to="casBadHoursView" />
        <transition on="badWorkstation" to="casBadWorkstationView" />
        <transition on="passwordExpired" to="casExpiredPassView" />
</action-state>


然后在下面找到,

<end-state id="redirectView" view="externalRedirect:${requestScope.response.url}" />
<!-- this is add -->
<end-state id="customizedRedirectView" view="externalRedirect:${requestScope.redirectUrl}" />
其中

<!-- this is add -->

<end-state id="customizedRedirectView" view="externalRedirect:${requestScope.redirectUrl}" />

为添加部分;

ok这样就完成了。

参考:http://blog.csdn.net/just_lion/article/details/17204979#java

感谢这篇文章。

在写这篇文章的时候因为没怎么写过博客,它自动添加了很多span标签,不知道怎么回事。如果有的话自己删掉。

已经编译好的cas-server可以直接使用:
http://download.csdn.net/detail/qq421664436/7554931 http://download.csdn.net/detail/qq421664436/7544207


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: