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

阅读更多

学习一下,自定义登录

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


cas
/myRemoteLogin

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


       
       
   

                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"
            />
 

mylogin-webflow.xml代码如下:




	
    
    
    	
        
    
    
    
    
		
	
	
	
        
        
     
	
        
		
	
	
		
	
	
	
	
	
        
		 
		
	
	
	
	
	
        
        
    
	
	
        
            
            
        
    
    
    
    
    
		
        
		
	

	

 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 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 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 parameters = new HashMap();
		 	
		 	
	        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;
	    }

}

 客户端端方法:

用户名:
密  码:
验证码
 

 

你可能感兴趣的:(cas 系统实例 服务端配置(二) 自定义登录)