不用单点登录,模拟远程项目的登录页面表单,在访问这个页面的时候自动提交表单到此项目的登录action,就可以实现登录到其他系统。
ssh框架项目
1.以下是本地系统的action代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
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.模拟的登录页面:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<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.href="http://www.youtest.com/index.html";
});
} else {
iframe.onload = function(){
window.location.href="http://www.youtest.com/index.html";
};
}
document.body.appendChild(iframe);
</script>
</body>
</html>
|
以上所述是小编给大家介绍的Java传入用户名和密码并自动提交表单实现登录到其他系统,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://www.cnblogs.com/yeqrblog/p/4626667.html
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 39
-
thinkphp6使用mysql悲观锁解决商品超卖问题的实现
2025-05-27 81 -
2025-05-29 72
-
2025-05-25 20
-
2025-05-29 44
热门评论

