不用单点登录,模拟远程项目的登录页面表单,在访问这个页面的时候自动提交表单到此项目的登录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/"; }); } else { iframe.onload = function(){ window.location.href="http://www.youtest.com/"; }; } document.body.appendchild(iframe); </script> </body> </html>
以上所述是小编给大家介绍的java传入用户名和密码并自动提交表单实现登录到其他系统,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
Springboot项目因为kackson版本问题启动报错解决方案
Java多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask详解
网友评论