1. 程式人生 > >cas-單點登入邏輯模擬學習

cas-單點登入邏輯模擬學習

Cas-單點登入學習記錄

實現邏輯:

進入首頁index.jsp,通過過濾器filter進行攔截,補充操作。

進入filter,判斷request中是否包含指定需要的值t。

如存在,使用httpclient傳送get請求,請求地址返回一個八位隨機數ticket,將t,ticket寫入cookie中,結束過濾,執行頁面請求。

如不存在,判斷cookies中是否包含指定需要的值t。

如存在,使用httpclient傳送get請求,請求地址返回一個八位隨機數ticket,將t,ticket寫入cookie中,結束過濾,執行頁面請求。

如不存在,將index.jsp的URL作為引數,帶值重定向到物件建立,建立物件,生成t值,取得傳入的引數,加入生成的t值重定向到index.jsp頁面,執行過濾。

進入上面邏輯中,直到過濾完成。

實現過程:

建立類LoginFilter繼承HttpServlet同時實現Filter。

在doFilter方法中實現邏輯:

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

/**
 * 
 * 模擬單點登入過濾
 * @author Chenjunxu
 * @date 2016/11/14
 *
 */

public class LoginFilter extends HttpServlet implements Filter {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3281360506578676969L;
	//validate方法路徑,validate方法實現八位隨機數的生成並返回
	private static final String VALIDATE_URL = "http://localhost:8080/platform/system/ticket/validate/";
	//login方法路徑,login方法實現物件生成(生成t),重定向到傳入的路徑
	private static final String LOGIN_URL = "http://localhost:8080/platform/system/ticket/login";
	//首頁路徑
	private static final String INDEX_URL = "http://localhost:8080/client/index.jsp";

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		//建立一個httpclient
		HttpClient httpClient = HttpClients.custom().build();
		
		String tRequest = request.getParameter("t");    //通過request取得t值
		String tCookies = GetReturn.getCookie(request, "t");    //通過cookies取得t值
		System.out.println("獲取的tCookies  :  " + tCookies);
		//如果request中包含t值
		if(tRequest != null){
			System.out.println("tRequest:" + tRequest);
			//建立get請求,請求路徑為validate方法路徑,傳入t值
			HttpGet httpGet = new HttpGet(VALIDATE_URL + tRequest);
			HttpResponse httpResponse = null;
			httpResponse = httpClient.execute(httpGet);   //傳送get請求,同時將響應結果存入HttpResponse
			//通過HttpResponse取得ticket的值
			String ticket = GetReturn.getValue(httpResponse);
			System.out.println("通過response獲取ticket值:" + ticket);
	        //將t,ticket寫入cookie中
	        Cookie cookie1 = new Cookie("ticket", ticket);
	        Cookie cookie2 = new Cookie("t", tRequest);
	        response.addCookie(cookie1);
	        response.addCookie(cookie2);
	        //結束此過濾,執行下一個過濾或進入頁面請求
	        filterChain.doFilter(request, response);
	        
		}else{
			//如果cookie中包含t
			if(tCookies != null){
				System.out.println("cookies有T");
				
				HttpGet httpGet = new HttpGet(VALIDATE_URL + tCookies);
				HttpResponse httpResponse = null;
				httpResponse = httpClient.execute(httpGet);
				
				String ticket = GetReturn.getValue(httpResponse);
				System.out.println(ticket);
		        
		        Cookie cookie1 = new Cookie("ticket", ticket);
		        Cookie cookie2 = new Cookie("t", tRequest);
		        response.addCookie(cookie1);
		        response.addCookie(cookie2);
		        filterChain.doFilter(request, response);
			}else{
				//重定向到login方法,並傳入指定引數
				response.sendRedirect(LOGIN_URL + "?service=" + INDEX_URL);
			}
			
		}
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("=======   loginFilter Start   =======");
		
	}

}


其中的login和validate方法為業務方法,所做業務已在路徑註釋中寫明,不再貼程式碼

編寫取值類GetReturn,實現完成兩個方法,一個用於取得httpclient傳送get請求後的返回值,一個用於獲取cookie中t的值

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

public class GetReturn {

	public static String getValue(HttpResponse response) {
		String content = "";
		try {
			HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity);
            System.out.println("content  :  " + content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}
	
	public static String getCookie(HttpServletRequest request, String name){
		javax.servlet.http.Cookie[] cookies = request.getCookies();
		if(cookies != null){
			for(javax.servlet.http.Cookie cookie : cookies){
				if("t".equals(cookie.getName())){
					return cookie.getValue();
				}
			}
		}
		return null;
    }

}
最後在web.xml中配置過濾器
  <filter>
  	<display-name>LoginFilter</display-name>
  	<filter-name>LoginFilter</filter-name>
  	<filter-class>包名.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>LoginFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>


這樣就基本實現了cas-單點登入的邏輯。