`
01jiangwei01
  • 浏览: 532668 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

cas 系统实例 服务端配置(二) 自定义登录

    博客分类:
  • cas
 
阅读更多

学习一下,自定义登录

在web.xml中增加remoteLogin,例如:

<servlet-mapping>
<servlet-name>cas</servlet-name>
<url-pattern>/myRemoteLogin</url-pattern>
</servlet-mapping>

 在cas-servlet.xml 中增加新的bean并添加相应的映射

<webflow:flow-registry id="flowRegistry" flow-builder-services="builder">
        <webflow:flow-location path="/WEB-INF/login-webflow.xml" id="login" />
        <webflow:flow-location path="/WEB-INF/mylogin-webflow.xml" id="myRemoteLogin" />
    </webflow:flow-registry>
<bean id="remoteLoginAction"
                class="org.jasig.cas.web.my.login.RemoteLoginAction"
            p:argumentExtractors-ref="argumentExtractors"
            p:warnCookieGenerator-ref="warnCookieGenerator"
            p:centralAuthenticationService-ref="centralAuthenticationService"
            p:initialFlowSetupAction-ref="initialFlowSetupAction"
            p:ticketGrantingTicketCookieGenerator-ref="ticketGrantingTicketCookieGenerator"
            />
<bean id="generateResponse" class="org.jasig.cas.web.my.login.GenerateResponse"></bean>
 

mylogin-webflow.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
                          http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

	
    <!-- 开始 -->
    <on-start>
    	<!-- 定义执行方法  到数据库相关验证 -->
        <evaluate expression="remoteLoginAction" />
    </on-start>
    
    <!-- 判断是否有错误 -->
    <decision-state id="hasError">
		<if test="flowScope.error neq null  &amp;&amp; flowScope.error neq '' " then="hasfailPageCheck" else="sendTicketGrantingTicket" />
	</decision-state>
	<!-- 取得错误信息,及返回页面 -->
	<action-state id="hasfailPageCheck">
        <evaluate 
        expression="generateResponse.getResponse( flowRequestContext,true)" 
        result-type="org.jasig.cas.authentication.principal.Response" 
        result="flowScope.response" />
        <transition to="postView" />
    </action-state> 
	<action-state id="sendTicketGrantingTicket">
        <evaluate expression="sendTicketGrantingTicketAction" />
		<transition to="serviceCheck" />
	</action-state>
	<decision-state id="serviceCheck">
		<if test="flowScope.service neq null" then="generateServiceTicket" else="viewGenericLoginSuccess" />
	</decision-state>
	
	
	<!-- 产生service票据 -->
	<action-state id="generateServiceTicket">
        <evaluate expression="generateServiceTicketAction" />
		<transition on="success" to ="getReturnResponse" /> 
		<transition on="gateway" to="hasError" />
	</action-state>
	
	
	<!-- 取得正常返回,及返回页面 -->
	<action-state id="getReturnResponse">
        <evaluate 
        expression="generateResponse.getResponse( flowRequestContext,false)" 
        result-type="org.jasig.cas.authentication.principal.Response" 
        result="flowScope.response" />
        <transition to="redirectView" />
    </action-state>
	
	<end-state id="postView" view="postResponseView">
        <on-entry>
            <set name="requestScope.parameters" value="flowScope.response.attributes" />
            <set name="requestScope.originalUrl" value="flowScope.response.url" />
        </on-entry>
    </end-state>
    <end-state id="viewGenericLoginSuccess" view="casLoginGenericSuccessView" />
    <end-state id="redirectView" view="externalRedirect:http://localhost:8081/casclient4/sso/index.jsp" />
    
    <global-transitions>
		<transition to="viewServiceErrorView" on-exception="org.springframework.webflow.execution.repository.NoSuchFlowExecutionException" />
        <transition to="viewServiceSsoErrorView" on-exception="org.jasig.cas.services.UnauthorizedSsoServiceException" />
		<transition to="viewServiceErrorView" on-exception="org.jasig.cas.services.UnauthorizedServiceException" />
	</global-transitions>

	
</flow>

 RemoteLoginAction.java

package org.jasig.cas.web.my.login;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;
import org.jasig.cas.CentralAuthenticationService;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.jasig.cas.ticket.TicketException;
import org.jasig.cas.web.flow.InitialFlowSetupAction;
import org.jasig.cas.web.support.ArgumentExtractor;
import org.jasig.cas.web.support.CookieRetrievingCookieGenerator;
import org.jasig.cas.web.support.WebUtils;
import org.springframework.util.StringUtils;
import org.springframework.webflow.action.AbstractAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

/**
* 远程登陆票据提供Action.
* 根据InitialFlowSetupAction修改.
* 由于InitialFlowSetupAction为final类,因此只能将代码复制过来再进行修改.
* 
* @author GuoLin
*/
public class RemoteLoginAction extends AbstractAction {
    /** CookieGenerator for the Warnings. */
    @NotNull
    private CookieRetrievingCookieGenerator warnCookieGenerator;
   
    /** Extractors for finding the service. */
    @NotEmpty
    private List<ArgumentExtractor> argumentExtractors;
     
    
    /** Core we delegate to for handling all ticket related tasks. */
    @NotNull
    private CentralAuthenticationService centralAuthenticationService;
    
 	@NotNull
    private CookieRetrievingCookieGenerator ticketGrantingTicketCookieGenerator;
    
    private InitialFlowSetupAction initialFlowSetupAction;
    
    protected Event doExecute(final RequestContext context)   {
        final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
    	
        /***
         * 必须的
         */
        context.getFlowScope().put( "warnCookieValue", Boolean.valueOf(this.warnCookieGenerator.retrieveCookieValue(request)));
        
        
    	String uName = request.getParameter("username");
    	String password = request.getParameter("password");
    	
    	
    	Credentials credentials =new UsernamePasswordCredentials(uName,password);
    	if (!this.initialFlowSetupAction.pathPopulated) {
            final String contextPath = request.getContextPath();
            final String cookiePath = StringUtils.hasText(contextPath) ? contextPath + "/" : "/";
            logger.info("Setting path for cookies to: "
                + cookiePath);
            this.warnCookieGenerator.setCookiePath(cookiePath);
            this.ticketGrantingTicketCookieGenerator.setCookiePath(cookiePath);
            this.initialFlowSetupAction.pathPopulated = true;
        }
    	
    	context.getFlowScope().put("credentials", credentials);
    	
    	 
    	String createTicketGrantingTicket;
		try {
			createTicketGrantingTicket = this.centralAuthenticationService.createTicketGrantingTicket(credentials);
			/***
			 * 必须的
			 */
			 WebUtils.putTicketGrantingTicketInRequestScope(context,createTicketGrantingTicket );
		} catch (TicketException e) {
			context.getFlowScope().put("error", "error.userOrPassword.error");
			e.printStackTrace();
		}
    	 
        // putWarnCookieIfRequestParameterPresent(context);
         
         
         final Service service = WebUtils.getService(this.argumentExtractors,  context);
             

         if (service != null && logger.isDebugEnabled()) {
             logger.debug("Placing service in FlowScope: " + service.getId());
         }

          context.getFlowScope().put("service", service);
    	
        return result("submit");
    }
    
    public void setWarnCookieGenerator(final CookieRetrievingCookieGenerator warnCookieGenerator) {
        this.warnCookieGenerator = warnCookieGenerator;
    }
    public void setArgumentExtractors(
        final List<ArgumentExtractor> argumentExtractors) {
        this.argumentExtractors = argumentExtractors;
    }
    public final void setCentralAuthenticationService(final CentralAuthenticationService centralAuthenticationService) {
        this.centralAuthenticationService = centralAuthenticationService;
    }
    
	public void setInitialFlowSetupAction(
			InitialFlowSetupAction initialFlowSetupAction) {
		this.initialFlowSetupAction = initialFlowSetupAction;
	}
	 public void setTicketGrantingTicketCookieGenerator(
	            final CookieRetrievingCookieGenerator ticketGrantingTicketCookieGenerator) {
	            this.ticketGrantingTicketCookieGenerator = ticketGrantingTicketCookieGenerator;
	        }
    
}

 GenerateResponse.java

package org.jasig.cas.web.my.login;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jasig.cas.authentication.principal.Response;
import org.jasig.cas.web.support.WebUtils;
import org.springframework.webflow.execution.RequestContext;

public class GenerateResponse {
	 public Response getResponse( final RequestContext context,boolean haveError) {
		 	String orgUrl = "";

		 	final Map<String, String> parameters = new HashMap<String, String>();
		 	
		 	
	        final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
	        if(haveError)
	        {
	        	orgUrl =request.getParameter("failpae");
	        	String error = (String) context.getFlowScope().get("error");
	        	parameters.put("error", error);
	        	parameters.put("result", "false");
	        }else
	        {
	        	orgUrl =request.getParameter("service");
	        	parameters.put("result", "true");
	        }
	        context.getFlowScope().put("responseUrl", orgUrl);
 
	        Response ret =  Response.getRedirectResponse(orgUrl, parameters);
	        
	        return ret;
	    }

}

 客户端端方法:

<form id="myLoginForm" action="http://localhost:8081/casserver/myRemoteLogin" method="post">
            <input type="hidden" id="targetService" name="service" value="http://localhost:8081/casclient4/sso/index.jsp">
            <input type="hidden" name="failpae" value="http://localhost:8081/casclient4/index.jsp">

 
            <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <td>密&nbsp;&nbsp;码:</td>
                    <td><input type="password" name="password"></td>
                </tr>
                <tr><td>验证码</td>
                <td><input type="text" /><img
									src="http://localhost:8081/casserver/random"
									class="sign_img fl mt5"  /></td></tr>
                <tr>
                    <td colspan="2"><input type="submit" value="登陆" /></td>
                </tr>
                
            </table>
        </form>
 

 

分享到:
评论
4 楼 xiaobadi 2014-10-30  
你每次登陆都是跳到http://localhost:8081/casclient4/sso/index.jsp。如果有多个客户端就有问题了吧
3 楼 Han_Zhou 2013-09-28  
确实不行啊,可否发送一份配置好了的cas-server给我asher_sys@163.com
2 楼 01jiangwei01 2012-06-05  
配合着几个可以登录运行啊。
1 楼 snowspice 2012-05-29  
这个能正常运行吗?我试了下,不行呀

相关推荐

    落雨博客基于CAS框架的单点登录技术讲解(ppt+code实例+doc)配套资料

    [置顶] SSO单点登录系列3:cas-server端配置认证方式实践(数据源+自定义java类认证) http://blog.csdn.net/ae6623/article/details/8851801 [置顶] SSO单点登录系列2:cas客户端和cas服务端交互原理动画图解,cas...

    JAVA上百实例源码以及开源项目源代码

     Tcp服务端与客户端的JAVA实例源代码,一个简单的Java TCP服务器端程序,别外还有一个客户端的程序,两者互相配合可以开发出超多的网络程序,这是最基础的部分。 递归遍历矩阵 1个目标文件,简单! 多人聊天室 3...

    JAVA上百实例源码以及开源项目

     Tcp服务端与客户端的JAVA实例源代码,一个简单的Java TCP服务器端程序,别外还有一个客户端的程序,两者互相配合可以开发出超多的网络程序,这是最基础的部分。 递归遍历矩阵 1个目标文件,简单! 多人聊天室 3...

    网管教程 从入门到精通软件篇.txt

     注意: 如果不带任何参数,fixboot 命令将向用户登录的系统分区写入新的分区引导扇区。  Fixmbr  修复启动磁盘的 主启动记录。fixmbr 命令仅在使用故障恢复控制台时才可用。  fixmbr [ device_name]  参数 ...

    java开源包1

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包2

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包3

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包6

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包5

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包10

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包8

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包7

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包9

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包11

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包4

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包101

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    Java资源包01

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

Global site tag (gtag.js) - Google Analytics