不用單點登錄,模擬遠程項目的登錄頁面表單,在訪問這個頁面的時候自動提交表單到此項目的登錄action,就可以實現登錄到其他系統。
ssh框架項目
1.以下是本地系統的action代碼:
import java.io.IOException;import java.util.List;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;public class myLoginAction { /** * 查詢是否用戶已注冊 * @return * @throws Exception */ public void checkUser() throws Exception{ Loginer loginer = (Loginer) request.getSession() .getAttribute("loginer"); String url = "http://www.youtest.com/login.php"; //遠程系統登錄action地址 String param = "username=Tom&password=123456"; //參數 String temp = "alert('用戶名或密碼錯誤');"; //返回的信息,此處是錯誤信息,用于比較。 視情況而定 boolean result =false ; //驗證數據是否能登錄 result = sendPost(url, param, temp); if(result){ return "login"; }else{ return "register"; } } //訪問遠程登錄action并獲取返回的信息 public static boolean sendPost(String url, String param, String temp) { PrintWriter out = null; BufferedReader in = null; boolean result = true; try { URL realUrl = new URL(url); // 打開和URL之間的連接 URLConnection conn = realUrl.openConnection(); // 設置通用的請求屬性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 發送POST請求必須設置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection對象對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數 out.print(param); // flush輸出流的緩沖 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { if(temp.equals((line.trim()))) { System.out.println("錯誤的line:"+line); result = false; } } } catch (Exception e) { result = false; logger.error("發送 POST 請求出現異常!"+e); System.out.println("發送 POST 請求出現異常!"+e); e.printStackTrace(); }finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } }catch(IOException ex){ logger.error(ex); ex.printStackTrace(); } } return result; } }2.模擬的登錄頁面:
<html><head></head><body> <script type="text/javascript"> var iframe = document.createElement("iframe"); iframe.src = "http://www.youtest.com/login.php?UNAME=<%=userName%>&UPWD=<%=pwd%>"; iframe.style.display="none"; var sta="false;" if (iframe.attachEvent){ iframe.attachEvent("onload", function(){ window.location.; }); } else { iframe.onload = function(){ window.location.; }; } document.body.appendChild(iframe); </script> </body></html>以上所述是小編給大家介紹的Java傳入用戶名和密碼并自動提交表單實現登錄到其他系統,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答