前两天接到一个任务,nc6的portal端要做集成,提供单点登录打开单据节点的方案;nc本身有sso单点登录,但是不满足需求,于是自己写了个注册类,如下:
1 package nc.sso.bs; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.io.PrintWriter; 10 import java.text.MessageFormat; 11 import java.util.List; 12 import java.util.Properties; 13 import java.util.UUID; 14 15 import javax.servlet.ServletException; 16 import javax.servlet.http.HttpServlet; 17 import javax.servlet.http.HttpServletRequest; 18 import javax.servlet.http.HttpServletResponse; 19 20 import nc.bs.dao.BaseDAO; 21 import nc.bs.dao.DAOException; 22 import nc.bs.framework.adaptor.IHttpServletAdaptor; 23 import nc.bs.logging.Logger; 24 import nc.bs.trade.business.HYPubBO; 25 import nc.jdbc.framework.SQLParameter; 26 import nc.jdbc.framework.processor.BeanListProcessor; 27 import nc.jdbc.framework.processor.ColumnProcessor; 28 import nc.uap.cpb.org.vos.CpUserVO; 29 import nc.uap.portal.vo.PtTrdauthVO; 30 import nc.vo.ecpubapp.pattern.exception.ExceptionUtils; 31 import nc.vo.pub.lang.UFDateTime; 32 33 import org.apache.commons.collections.CollectionUtils; 34 35 @SuppressWarnings("restriction") 36 public class PortalTaskSSORegServlet extends HttpServlet implements IHttpServletAdaptor{ 37 38 private static final long serialVersionUID = -5182365813018553230L; 39 40 private BaseDAO baseDAO = null; 41 private BaseDAO getBaseDAO() { 42 if(null == baseDAO){ 43 baseDAO = new BaseDAO(GetConfigValues("ds")); 44 } 45 return baseDAO; 46 } 47 private HYPubBO bo = null; 48 public HYPubBO getBo() { 49 if(bo == null) 50 bo = new HYPubBO(); 51 return bo; 52 } 53 /** 54 * 请求的服务器地址 55 */ 56 private String _ServerName = ""; 57 public String GetServerName(){ 58 return _ServerName; 59 } 60 public void SetServerName(String name){ 61 _ServerName = name; 62 } 63 64 @Override 65 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 66 doAction(req, resp); 67 } 68 69 @Override 70 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 71 doAction(req, resp); 72 } 73 74 @Override 75 public void doAction(HttpServletRequest request, HttpServletResponse response) 76 throws ServletException, IOException { 77 78 response.setContentType("text/html;charset=UTF-8"); 79 response.setCharacterEncoding("UTF-8"); 80 PrintWriter pw = response.getWriter(); 81 // CheckRegIP(request.getRemoteAddr()); 82 this.SetServerName(MessageFormat.format("http://{0}:{1}", request.getServerName(), String.valueOf(request.getServerPort()))); 83 String uid = request.getParameter("userCode"); 84 try{ 85 String url = ""; 86 if(request.getParameter("cz") != null && ! request.getParameter("cz").isEmpty() && request.getParameter("cz").equals("0")){ 87 url = GetConfigValues("FinancialSharingURL"); 88 }else if(request.getParameter("cz") != null && ! request.getParameter("cz").isEmpty() && request.getParameter("cz").equals("1")){ 89 url = GetConfigValues("MoreURL"); 90 }else{ 91 String groupid = request.getParameter("gpPara"); //集团pk 92 String tradetypecode = request.getParameter("btPara").trim(); //单据类型编码 93 String billtypecode = tradetypecode.trim(); //portal模板类型 94 String tnodecode = (String) getBaseDAO().executeQuery("select classname from bd_billtype where pk_billtypecode = '"+tradetypecode+"' and rownum = 1", new ColumnProcessor()); 95 if(tnodecode != null && !tnodecode.isEmpty()) 96 tnodecode = tnodecode.substring(3); 97 if(billtypecode.trim().equals("2647")) 98 billtypecode = "2647_IWEB"; 99 if(billtypecode.trim().equals("2641")) 100 billtypecode = "2641_IWEB"; 101 String tempid = (String) getBaseDAO().executeQuery("select templateid from pub_systemplate where dr = 0 and tempstyle = 0 and nodekey = '"+billtypecode+"' and rownum = 1", new ColumnProcessor()); 102 if(tempid == null|| tempid.isEmpty()){ 103 tempid = (String) getBaseDAO().executeQuery("select templateid from pub_systemplate_base where dr = 0 and tempstyle = 0 and nodekey = '"+billtypecode+"' and rownum = 1", new ColumnProcessor()); 104 } 105 String countryid = "0001Z010000000079UJJ"; //国家pk 106 String billid = request.getParameter("bidPara"); //单据pk 107 if(tradetypecode == null || tradetypecode.isEmpty() || tempid == null || tempid.isEmpty() || groupid == null || groupid.isEmpty() || billid == null || billid.isEmpty()) 108 ExceptionUtils.wrappBusinessException("参数空值"); 109 String tempname = GetConfigValues(billtypecode); 110 if(tempname == null || tempname.isEmpty()) 111 ExceptionUtils.wrappBusinessException("未找到对应模板,请检查模板配置类"); 112 else 113 tempname = java.net.URLEncoder.encode(tempname, "utf-8"); 114 url = GetConfigValues("DetailURL"); 115 url = url.replaceAll("tnodecode", tnodecode); 116 url = url.replaceAll("billtypecode", billtypecode); 117 url = url.replaceAll("tradetypecode", tradetypecode); 118 url = url.replaceAll("tempPara", tempid); 119 url = url.replaceAll("gpPara", groupid); 120 url = url.replaceAll("ctPara", countryid); 121 url = url.replaceAll("bidPara", billid); 122 url = url.replaceAll("tempname", tempname); 123 } 124 String ssoKey = RegistToPortal(MappingPortalUserPK(uid),url); 125 pw.print(ssoKey); 126 }catch(Exception ex){ 127 pw.print("ERROR:"+ex.getMessage()); 128 } 129 } 130 131 /** 132 * 把帐号映射成pk 133 * @param username 134 * @return 135 * @throws ServletException 136 * @throws DAOException 137 */ 138 @SuppressWarnings("unchecked") 139 private String MappingPortalUserPK(String username) throws ServletException, DAOException { 140 //----当前账号和portal账号一致时的处理 141 ListcpuserList = null; 142 String sql = "select * from cp_user where user_code_q = ?"; 143 SQLParameter param = new SQLParameter(); 144 param.addParam(username.toUpperCase()); 145 cpuserList = (List )getBaseDAO().executeQuery(sql, param, new BeanListProcessor(CpUserVO.class)); 146 if(CollectionUtils.isEmpty(cpuserList)){ 147 throw new ServletException("当前要求登录的帐号并没开通portal账号"); 148 } else { 149 return (cpuserList.get(0)).getCuserid(); 150 } 151 } 152 153 /** 154 * 验收发送注册请求机器的IP 155 * @return 156 */ 157 private boolean CheckRegIP(String regIP) { 158 String[] ipList = GetConfigValues("ip").split(","); 159 for(String ip : ipList){ 160 if(ip.equals(regIP)){ 161 return true; 162 } 163 } 164 return false; 165 } 166 167 /** 168 * 读取配置文件的值 169 * @param propName 170 * @return 171 */ 172 public static String GetConfigValues(String propName){ 173 Properties pros = new Properties(); 174 InputStream in = null; 175 try { 176 in = new FileInputStream(new File(PortalTaskSSORegServlet.class.getResource(".").getPath().toString()+"PortalTaskRegIP.properties")); 177 pros.load(new InputStreamReader(in, "utf-8")); 178 } catch (FileNotFoundException e) { 179 Logger.info("PortalReg配置文件找不到!"+e.getMessage()); 180 } catch (IOException e) { 181 Logger.info("PortalReg读取IO异常!"+e.getMessage()); 182 } finally { 183 if(in != null){ 184 try { 185 in.close(); 186 } catch (IOException e) { 187 e.printStackTrace(); 188 } 189 } 190 } 191 String configValues = pros.getProperty(propName); 192 return configValues; 193 } 194 195 /** 196 * 获取发起请求的机器的ip地址 197 * @param request 198 * @return 199 */ 200 public String getIpAddr(HttpServletRequest request) { 201 String ip = request.getHeader("X-Forwarded-For"); 202 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 203 ip = request.getHeader("Proxy-Client-IP"); 204 } 205 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 206 ip = request.getHeader("WL-Proxy-Client-IP"); 207 } 208 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 209 ip = request.getHeader("HTTP_CLIENT_IP"); 210 } 211 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 212 ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 213 } 214 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 215 ip = request.getRemoteAddr(); 216 } 217 return ip; 218 } 219 220 /** 221 * 向portal注册单点登录 222 * @param ssoKey 223 * @param nc_uid 224 * @throws DAOException 225 */ 226 private String RegistToPortal(String nc_uid,String url) throws DAOException { 227 PtTrdauthVO ssoVO = new PtTrdauthVO(); 228 ssoVO.setPk_trdauth(UUID.randomUUID().toString().replaceAll("-", "")); 229 ssoVO.setPk_user(nc_uid); 230 ssoVO.setTtl(new UFDateTime(System.currentTimeMillis() + + 3*60*1000)); //有效期1分钟 231 // ssoVO.setUrl("http://erp.8531.cn/portal/pt/home/index"); 232 ssoVO.setUrl(MessageFormat.format("{0}/{1}", this.GetServerName(), url)); 233 getBaseDAO().insertVO(ssoVO); 234 return ssoVO.getAkey(); 235 } 236 237 }
以下是配置文件:
#数据源 ds=ceshi2 #ds=design #可配置信任ip,可根据网关过滤,当前ip过滤方法以注掉 ip=172.20.130.249,127.0.0.1,172.21.10.49,172.21.10.41,172.20.12.254 #单据详情界面 DetailURL=iwebap/pages/tnodecode/billtypecode/0/tempPara/gpPara/ctPara/0/simpchn/approvecard.html?&tradetype=tradetypecode&nodecode=tnodecode&templateName=tempname#/view/bidPara #审批人门户界面 MoreURL=portal/pt/home/view?pageModule=weberm&pageName=MYEXPPORTAL #报账人门户界面 FinancialSharingURL=portal/pt/home/view?pageModule=weberm&pageName=MyExpBillPortlet #[单据类型编码,查询方式:select * from pub_systemplate where funnode = '20110BMLB' and dr = 0 and tempstyle = 0]=[单据模板名称] 2641_IWEB=%e5%b7%ae%e6%97%85%e8%b4%b9%e6%8a%a5%e9%94%80%e5%8d%95 264X-Cxx-002=%e5%85%ac%e5%8f%b8%e7%a6%8f%e5%88%a9%e6%8a%a5%e9%94%80%e5%8d%95 264X-Cxx-003=%e4%b8%aa%e4%ba%ba%e7%a6%8f%e5%88%a9%e6%8a%a5%e9%94%80%e5%8d%95%ef%bc%88%e6%97%a5%e5%b8%b8%ef%bc%89 264X-Cxx-004=%e9%80%9a%e7%94%a8%e7%b1%bb%e8%b4%b9%e7%94%a8%e6%8a%a5%e9%94%80%e5%8d%95 264X-Cxx-005=%e5%85%ac%e8%bd%a6%e8%b4%b9%e7%94%a8%e6%8a%a5%e9%94%80%e5%8d%95 264X-Cxx-006=%e6%88%90%e6%9c%ac%e7%b1%bb%e6%8a%a5%e9%94%80%e5%8d%95 264X-Cxx-007=%e6%8b%9b%e5%be%85%e8%b4%b9%e6%8a%a5%e9%94%80%e5%8d%95 264X-Cxx-009=%e7%94%b5%e8%84%91%e8%a1%a5%e8%b4%b4%e6%8a%a5%e9%94%80%e5%8d%95 264X-Cxx-010=%e9%94%80%e5%94%ae%e5%ae%a3%e4%bc%a0%e8%b4%b9%e7%94%a8%e6%8a%a5%e9%94%80%e5%8d%95 263X-Cxx-001=%e5%80%9f%e6%ac%be%e5%8d%95%ef%bc%88%e4%b8%aa%e4%ba%ba%ef%bc%89 263X-Cxx-002=%e5%80%9f%e6%ac%be%e5%8d%95%ef%bc%88%e5%af%b9%e5%85%ac%ef%bc%89 261X-Cxx-001=%e6%8b%9b%e5%be%85%e8%b4%b9%e7%94%b3%e8%af%b7%e5%8d%95 261X-Cxx-002=%e6%b2%b9%e5%8d%a1%e5%85%85%e5%80%bc%e7%94%b3%e8%af%b7%e5%8d%95 2647_IWEB=%e8%bf%98%e6%ac%be%e5%8d%95
注册并验证成功之后会跳转到指定地址,nc用的是重定向:
门户系统单点登录的时候发现除了ie内核浏览器,其他浏览器无法直接打开,但是在地址栏却可以打开,尝试了各种方法也行不通;任务比较紧,许多猜测没有去验证了,想到一招治标的方法。安装ie tab插件,并设置 自动地址 ,可以在访问某类地址时自动切换ie内核打开:
任务暂时得到解决,上述问题原理下回分解