java 传入用户名和密码并自动提交表单实现登录到其他系统
不用单点登录,模拟远程项目的登录页面表单,在访问这个页面的时候自动提交表单到此项目的登录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.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 传入用户名和密码并自动提交表单实现登录到其他系统的更多相关文章
- ssi-uploader上传图片插件,点击选择文件按钮自动提交表单解决办法
先介绍一下这款插件,然后再谈使用中可能遇到的问题 ssi-uploader是一个JQuery的图片上传插件,界面比较美观 github地址:https://github.com/ssbeefeater ...
- form表单下的button按钮会自动提交表单的问题
form表单下的button按钮会自动提交表单的问题 2017年01月05日 18:02:44 蓝色水 阅读数:18012更多 个人分类: asp.net form表单下的按钮在没有指定type类 ...
- 按钮特效-Enter键自动提交表单
—————————————————————— <script type="text/javascript"> //当用户按 ...
- 去掉chrome记住密码后自动填充表单的黄色背景
chrome表单自动填充后,input文本框的背景会变成黄色的,通过审查元素可以看到这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对 ...
- alert警告框点击确定后自动提交表单
转载于 :https://zhidao.baidu.com/question/619375120140398412.html 在页面中有多个input type="text"的文 ...
- 前端表单中有按钮button自动提交表单
问题描述 在设计表单时,表单内有一个按钮<button>,该按钮是用来获取其他数据或执行其他操作的.并不是让他提交表单. 解决方案 1) 设置 form 的 onsubmit='retur ...
- JS倒计时,自动提交表单!
<form id="frm" action="http://www.baidu.com"> 考试还剩余<div id="time&q ...
- <button>会自动提交表单吗?
点击button以后,表单先由ajax提交,然后无论后台返回什么结果,页面都会跳转到表单action属性指定的路劲,也就是login.html使用的是html.jquery.javascript,后台 ...
- Java后台防止客户端重复请求、提交表单
前言 在Web / App项目中,有一些请求或操作会对数据产生影响(比如新增.删除.修改),针对这类请求一般都需要做一些保护,以防止用户有意或无意的重复发起这样的请求导致的数据错乱. 常见处理方案 1 ...
随机推荐
- [webpack]webpack打包优化
1.import优化 a.tree-shaking 把没用到的代码删除掉,import 在生产环境下 会自动去除掉没用的代码 b.scope hosting 作用域提升,在webpack中会自动省略一 ...
- python开发-实现redis中的发布订阅功能
Python3学习(二十七):python实现Redis的订阅与发布(sub-pub机制) 先介绍一下redis的pub/sub功能: Pub/Sub功能(means Publish, Subscri ...
- pytorch常用损失函数
损失函数的基本用法: criterion = LossCriterion() #构造函数有自己的参数 loss = criterion(x, y) #调用标准时也有参数 得到的loss结果已经对min ...
- 怎么用群晖webdav实现外网映射网络驱动器
前几天刚作好群晖nas局域网内的磁盘映射功能,今天老板又想实现在家里也能跟在公司一样的方便访问映射功能,因为使用网页操作实在太麻烦了.这可怎么办官方提供的 Assistant工具只能操作局域网的,又没 ...
- osg fbx模型点击节点,对应节点染色
class CPickHandler :public osgGA::GUIEventHandler { public: CPickHandler(osgViewer::Viewer *viewer) ...
- poi导出excel2007版本
/** * 导出excel2007版本 * * @param titles * 表头集合 * @param sheetNames * sheet名称 * @param datas * 数据集合 * @ ...
- 123457---com.threeObj.Baobaoshizi01--- 宝宝识字01
com.threeObj.Baobaoshizi01--- 宝宝识字01
- Python项目开发之CMDB理解与分析
CMDB的由来--ITIL ITIL就是IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库),由英国政府部门CC ...
- VS在项目范围内禁止关闭特定警告
要在项目范围内禁止警告,请在项目文件中定义要忽略的以分号分隔的警告代码列表.附加警告代码也$(NoWarn); 1.首先找到该项目的 .csproj 文件 >> 在<Property ...
- jQuery插件—获取URL参数
做的项目中需要用到通过JS获取GET参数,上网找了一下,找到如下插件: 例如 当前你的URL是: http://www.xxx.com/index.php?test=1&kk=2 //如果想获 ...