cas 系统实例 服务端配置(三) 自定义登录 不使用webFlow

1,修改org.jasig.cas.web.flow.InitialFlowSetupAction.java将pathPopulated属性改为public

2,在web.xm中添加

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

 3,在cas-servlet.xml中添加

<bean id="noflowLoginController" class="org.jasig.cas.web.my.noflowlogin.noFlowLoginAction"
    		p:argumentExtractors-ref="argumentExtractors"
    		p:warnCookieGenerator-ref="warnCookieGenerator"
    		p:centralAuthenticationService-ref="centralAuthenticationService"
    		p:ticketGrantingTicketCookieGenerator-ref="ticketGrantingTicketCookieGenerator"
    		p:initialFlowSetupAction-ref="initialFlowSetupAction"
    	></bean>

 修改bean,黑体为新增

<bean
		id="handlerMappingC"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property
			name="mappings">
			<props>
				<prop
					key="/logout">
					logoutController
				</prop>
				<prop
					key="/serviceValidate">
					serviceValidateController
				</prop>
				<prop
					key="/validate">
					legacyValidateController
				</prop>
				<prop
					key="noflow">
					noflowLoginController
				</prop>
				 

				<prop
					key="/proxy">
					proxyController
				</prop>
				<prop
					key="/proxyValidate">
					proxyValidateController
				</prop>
				<prop
					key="/samlValidate">
					samlValidateController
				</prop>
				
				<prop
					key="/services/add.html">
					addRegisteredServiceSimpleFormController
				</prop>
				
				<prop
					key="/services/edit.html">
					editRegisteredServiceSimpleFormController
				</prop>
				
				<prop
					key="/services/loggedOut.html">
					serviceLogoutViewController
				</prop>

                <prop key="/services/viewStatistics.html">
                    viewStatisticsController
                </prop>
			
				<prop key="/services/*">manageRegisteredServicesMultiActionController</prop>
				<prop key="/openid/*">openIdProviderController</prop>
                <prop key="/authorizationFailure.html">passThroughController</prop>
                <prop key="/403.html">passThroughController</prop>
			</props>
		</property>
		<property
			name="alwaysUseFullPath" value="true" />
		<!--
		uncomment this to enable sending PageRequest events. 
		<property
			name="interceptors">
			<list>
				<ref bean="pageRequestHandlerInterceptorAdapter" />
			</list>
		</property>
		 -->
	</bean>

 4,noFlowLoginAction类的具体内容为:

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

public class noFlowLoginAction extends AbstractController {
	
	
	   @NotNull
	   private CentralAuthenticationService centralAuthenticationService;
	     
	  	@NotNull
	    private CookieRetrievingCookieGenerator warnCookieGenerator;
	 	@NotNull
	    private CookieRetrievingCookieGenerator ticketGrantingTicketCookieGenerator;
	 	
	 	private InitialFlowSetupAction initialFlowSetupAction;
	 
	 
	   
	    /** Extractors for finding the service. */
	    @NotEmpty
	    private List<ArgumentExtractor> argumentExtractors;
	    protected ModelAndView handleRequestInternal(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
	    	
	    	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;
	        }
	    	 final Service service = WebUtils.getService( this.argumentExtractors, request);
	    	 String ticketGrantingTicketId="";
	    	 String serviceTicket = "";
	 		try {
	 			ticketGrantingTicketId = this.centralAuthenticationService.createTicketGrantingTicket(credentials);
	 			
	 			/***
	        	 * 产生新的票据,并将票据及服务记录在缓存中
	        	 */
	 			serviceTicket=	 this.centralAuthenticationService.grantServiceTicket(ticketGrantingTicketId,service);
	 			
	 			this.ticketGrantingTicketCookieGenerator.removeCookie(response);
	 			
	 			this.ticketGrantingTicketCookieGenerator.addCookie(request, response, ticketGrantingTicketId);
	 		 
	 			this.warnCookieGenerator.addCookie(request, response, "true");
	 			 
	 		} catch (TicketException e) {
	 			 
	 			e.printStackTrace();
	 		}
	 		return new  ModelAndView(new RedirectView(request.getParameter("service")+"?ticket="+serviceTicket));
		}
	    
  
	    
	    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 setTicketGrantingTicketCookieGenerator(
	            final CookieRetrievingCookieGenerator ticketGrantingTicketCookieGenerator) {
	            this.ticketGrantingTicketCookieGenerator = ticketGrantingTicketCookieGenerator;
	        }



		public void setInitialFlowSetupAction(
				InitialFlowSetupAction initialFlowSetupAction) {
			this.initialFlowSetupAction = initialFlowSetupAction;
		}	
	  
}
 

5,使用方法是:

 

    <form action="http://localhost:8081/casserver/noflow" method="post">
   	 <table>
   	 		<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>
   	 </table>
    
    </form>
 

 

 6,你可以自己测试了

 

你可能感兴趣的:(自定义登录)