java 傳入使用者名稱和密碼並自動認可表單實現登入到其他系統

來源:互聯網
上載者:User

標籤:use   str   attach   splay   發送post請求   event   mozilla   cep   create   

不用單點登入,類比遠程項目的登入頁面表單,在訪問這個頁面的時候自動認可表單到此項目的登入action,就可以實現登入到其他系統。

ssh架構項目

1.以下是本地系統的action代碼:

 1 import java.io.IOException; 2 import java.util.List; 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.URL; 8 import java.net.URLConnection; 9 10 public class myLoginAction {11 12     /**13      * 查詢是否使用者登入14      * @return15      * @throws Exception 16      */17     public void checkUser() throws Exception{18         Loginer loginer = (Loginer) request.getSession()19         .getAttribute("loginer");20         21         String url = "http://www.youtest.com/login.php"; //遠程系統登入action地址22         String param = "username=Tom&password=123456"; //參數23         String temp = "alert(‘使用者名稱或密碼錯誤‘);";   //返回的資訊,此處是錯誤資訊,用於比較。   視情況而定24         boolean result =false ;25         //驗證資料是否能登入26         result = sendPost(url, param, temp);27         if(result){28             return "login";29         }else{30             return "register";31         }32         }33 34     //訪問遠程登入action並擷取返回的資訊35     public static boolean sendPost(String url, String param, String temp) {36             PrintWriter out = null;37             BufferedReader in = null;38             boolean result = true;39             try {40                 URL realUrl = new URL(url);41                     // 開啟和URL之間的串連42                    URLConnection conn = realUrl.openConnection();43                     // 設定通用的請求屬性44                     conn.setRequestProperty("accept", "*/*");45                     conn.setRequestProperty("connection", "Keep-Alive");46                     conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");47                     // 發送POST請求必須設定如下兩行48                     conn.setDoOutput(true);49                     conn.setDoInput(true);50                     // 擷取URLConnection對象對應的輸出資料流51                     out = new PrintWriter(conn.getOutputStream());52                     // 發送請求參數53                     out.print(param);54                     // flush輸出資料流的緩衝55                     out.flush();56                     // 定義BufferedReader輸入資料流來讀取URL的響應57                     in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));58                     String line;59                     while ((line = in.readLine()) != null) {60                         if(temp.equals((line.trim()))) {61                             System.out.println("錯誤的line:"+line);62                             result = false;63                         }64                     }65             } catch (Exception e) {66                 result = false;67                 logger.error("發送 POST 請求出現異常!"+e);68                     System.out.println("發送 POST 請求出現異常!"+e);69                     e.printStackTrace();70             }finally{71                     try{72                         if(out!=null){73                                 out.close();74                         }75                         if(in!=null){76                                 in.close();77                         }78                     }catch(IOException ex){79                         logger.error(ex);80                         ex.printStackTrace();81                     }82             }83             return result;84         } 85 }

2.類比的登入頁面:

 1 <html> 2 <head></head> 3 <body> 4        <script type="text/javascript"> 5          var iframe = document.createElement("iframe"); 6          iframe.src = "http://www.youtest.com/login.php?UNAME=<%=userName%>&UPWD=<%=pwd%>"; 7          iframe.style.display="none"; 8           9          var sta="false;"10          if (iframe.attachEvent){11              iframe.attachEvent("onload", function(){12                  window.location.href="http://www.youtest.com/index.html";13              });14          } else {15              iframe.onload = function(){16                  window.location.href="http://www.youtest.com/index.html";17              };18          }19          document.body.appendChild(iframe);20  </script>21    </body>22 </html>

 

java 傳入使用者名稱和密碼並自動認可表單實現登入到其他系統

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.