因爲項目須要,須要內外網都能同時訪問,本來的不能符合要求,只能着手修改,如下是修改步驟。我用的是cas-client-core-3.3.3.jar的客戶端版本java
1.得到cas-client-core-3.3.3.jar的源碼。服務器
2.新增工具類CustomConfigUtil,我把工具類放在org.jasig.cas.client.util下。網絡
代碼爲:session
package org.jasig.cas.client.util; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; public class CustomConfigUtil { /** * @Description 獲取屬性文件中的屬性信息 * @return */ public static Map<String, String> getCustomConfig( final ServletContext servletContext, final HttpServletRequest request) { Map<String, String> map = new HashMap<String, String>(); Map<String, String> map2 = HttpConnectionUtil.getServiceUrlMap(); //讀取配置文件 Map<String, String> segmentMap = PropertiesUtil.readProperties(CustomConfigUtil.class.getResource("/").getPath() + "segment.properties"); boolean falg = false; for (String key : segmentMap.keySet()) { //判斷是否屬於某個網段 falg = HttpConnectionUtil.ipIsValid(segmentMap.get(key), request.getRemoteAddr()); if(falg){ break; } } // 判斷請求是從外網訪問仍是從內網訪問 if (falg) { //client客戶端地址 map.put("client", map2.get("cas.inClient")); //cas服務器地址 map.put("casServerTicketUrl", map2.get("cas.inCasServer")); //cas服務器地址 map.put("casServerTicket", map2.get("cas.inCasServerTicket")); } else { //client客戶端地址 map.put("client", map2.get("cas.outClient")); //cas服務器地址 map.put("casServerTicketUrl", map2.get("cas.outCasServer")); //cas服務器地址 boolean flag = HttpConnectionUtil.isConnection(map2.get("cas.outCasServerTicket")); if(flag){ map.put("casServerTicket", map2.get("cas.outCasServerTicket")); }else{ map.put("casServerTicket", map2.get("cas.inCasServerTicket")); } } return map; } }
3.新增工具類HttpConnectionUtil 代碼以下工具
package org.jasig.cas.client.util; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; public class HttpConnectionUtil { /** * 測試網絡是否能鏈接通暢 * * @param serviceURL * @return */ public static boolean isConnection(String serviceURL) { try { URL url = new URL(serviceURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(2000);// 2秒則超時 conn.setReadTimeout(2000); int state = conn.getResponseCode(); if (state == 200) { return true; } } catch (Exception e) { return false; } return false; } public static boolean ipIsValid(String ipSection, String ip) { if (ipSection == null) throw new NullPointerException("IP段不能爲空!"); if (ip == null) throw new NullPointerException("IP不能爲空!"); ipSection = ipSection.trim(); ip = ip.trim(); final String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"; final String REGX_IPB = REGX_IP + "\\-" + REGX_IP; if (!ipSection.matches(REGX_IPB) || !ip.matches(REGX_IP)) return false; int idx = ipSection.indexOf('-'); String[] sips = ipSection.substring(0, idx).split("\\."); String[] sipe = ipSection.substring(idx + 1).split("\\."); String[] sipt = ip.split("\\."); long ips = 0L, ipe = 0L, ipt = 0L; for (int i = 0; i < 4; ++i) { ips = ips << 8 | Integer.parseInt(sips[i]); ipe = ipe << 8 | Integer.parseInt(sipe[i]); ipt = ipt << 8 | Integer.parseInt(sipt[i]); } if (ips > ipe) { long t = ips; ips = ipe; ipe = t; } return ips <= ipt && ipt <= ipe; } public static void main(String[] args) { if (ipIsValid("127.0.0.1-127.0.0.1", "127.0.0.1")) { System.out.println("ip屬於該網段"); } else{ System.out.println("ip不屬於該網段"); } } /** * 獲取配置文件信息 * * @return */ public static Map<String, String> getServiceUrlMap() { return PropertiesUtil.readProperties(HttpConnectionUtil.class .getResource("/").getPath() + "cas-service.properties"); } }
5.新增工具類:PropertiesUtil: 測試
package org.jasig.cas.client.util; import java.io.FileInputStream; import java.io.IOException; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class PropertiesUtil { public static Map<String, String> readProperties(String path) { Map<String, String> map = new HashMap<String, String>(); try { Properties props = new Properties(); // System.out.println(path); props.load(new FileInputStream(path)); Enumeration<?> enum1 = props.propertyNames(); while(enum1.hasMoreElements()) { String strKey = (String) enum1.nextElement(); String strValue = props.getProperty(strKey); map.put(strKey, strValue); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } }
6.所屬的兩個配置文件this
cas-service.properties文件:url
#cas服務端的內網和外網 cas.inCasServer=http://10.206.20.52:8982/cas cas.outCasServer=http://218.6.169.98:18982/cas #客戶端的內網和外網 cas.inClient=http://10.206.20.52:8982/tickets cas.outClient=http://218.6.169.98:18982/tickets #服務端的內網和外網 cas.inCasServerTicket=http://10.206.20.52:8982/cas cas.outCasServerTicket=http://218.6.169.98:18982/cas
segment.properties文件:spa
#網段 segment_1 =10.0.0.0-10.255.255.255 segment_2 =172.16.0.0-172.31.255.255 segment_3 =192.168.0.0-192.168.255.255 segment_4 =172.10.0.0-172.31.255.255
7.作好以上工做開始修改源碼,首先修改AuthenticationFilte ,添加靜態屬性.net
public static final String CONST_CAS_GATEWAY = "_const_cas_gateway_";
而後找到方法:doFilter 方法內容修改成:
public final void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; HttpSession session = request.getSession(false); String ticket = request.getParameter(getArtifactParameterName()); Assertion assertion = session != null ? (Assertion)session.getAttribute("_const_cas_assertion_") : null; boolean wasGatewayed = (session != null) && (session.getAttribute("_const_cas_gateway_") != null); String isToLogout = request.getParameter("isToLogout"); if ((CommonUtils.isBlank(ticket)) && (assertion == null) && (!wasGatewayed) && (!"1".equals(isToLogout))) { this.logger.debug("noticket and no assertion found"); if (this.gateway) { this.logger.debug("settinggateway attribute in session"); request.getSession(true).setAttribute("_const_cas_gateway_", "yes"); }
//修改部分 String serviceUrl = constructServiceUrl(request, response, "auth"); Map<String, String> config = CustomConfigUtil.getCustomConfig( request.getServletContext(), request); this.casServerLoginUrl = ((String)config.get("casServerTicketUrl")).toString(); setCasServerLoginUrl(((String)config.get("casServerTicketUrl")).toString()); String urlToRedirectTo = CommonUtils.constructRedirectUrl( this.casServerLoginUrl, getServiceParameterName(), serviceUrl, this.renew, this.gateway); if (this.logger.isDebugEnabled()) { this.logger.debug("redirectingto \"" + urlToRedirectTo + "\""); } response.sendRedirect(urlToRedirectTo); return; } if (session != null) { this.logger.debug("removinggateway attribute from session"); session.setAttribute("_const_cas_gateway_", null); } filterChain.doFilter(request, response); }
8.修改後constructServiceUrl報錯 由於以前該方法只有兩個參數 如今修改成3個 找到改方法的類AbstractCasFilter
源碼在113行那樣constructServiceUrl重載該方法:
protected final String constructServiceUrl(final HttpServletRequest request, final HttpServletResponse response,final String type) { //從配置文件中取出cas服務器的登錄地址 Map<String,String> config = CustomConfigUtil.getCustomConfig(request.getServletContext(),request); if("auth".equals(type)){ this.serverName = config.get("client").toString(); this.service = config.get("client").toString(); }else if("validation".equals(type)){ this.serverName = config.get("casServerTicket").toString(); this.service = config.get("client").toString(); }
注意 該方法調用的地方還有幾個 須要所有都修改成重載的方法 能夠先不忙重載, 先在原方法修改 看到源碼報錯的地方修改後在作調整,其餘地方調整後傳人蔘數爲:「validation」
如此便實現了內網和外網的通用訪問。
具體項目和說明示例可下載:
http://pan.baidu.com/s/1sklXCK1