通过httpClient模拟登陆并重定向到其他系统

目录

 

概述:

首先安装tesseract

获取验证码

获取验证码的函数

重定向Controller

httpClient模拟登录


概述:

简单实现一个跨域重定向功能。

首先安装tesseract

https://blog.csdn.net/showgea/article/details/82656515

OCR,即Optical Character Recognition,光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程。对于图形验证码来说,它们都是一些不规则的字符,这些字符确实是由字符稍加扭曲变换得到的内容,但是对于一些复杂的图片识别会有问题。

获取验证码

登录页面找到 获取验证码的地址如下图:

 

http://demo/servlet/captchaCode

 

获取验证码的函数

public static String getCode() {
        //new一个URL对象
        URL url;
        String s="";
        try {
            url = new URL("http://demo/servlet/captchaCode");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置请求方式为"GET"
            conn.setRequestMethod("GET");
            //超时响应时间为5秒
            conn.setConnectTimeout(5 * 1000);
            //通过输入流获取图片数据
            InputStream inStream = conn.getInputStream();
            //得到图片的二进制数据,以二进制封装得到数据,具有通用性
            byte[] data = readInputStream(inStream);
            //new一个文件对象用来保存图片,默认保存当前工程根目录
            File imageFile = new File("C:\\test\\timg5.jpg");
            //创建输出流
            FileOutputStream outStream = new FileOutputStream(imageFile);
            //写入数据
            outStream.write(data);
            //关闭输出流
            outStream.close();
            
            // OCR 
            ITesseract instance = new Tesseract();
            //设置训练库的位置
            instance.setDatapath("C:\\test\\");
            //chi_sim :简体中文, eng根据需求选择语言库
            instance.setLanguage("eng");
            String result = null;
            if(imageFile.exists()) {
            	   try {
                       long startTime = System.currentTimeMillis();
                        result =  instance.doOCR(imageFile);
                       long endTime = System.currentTimeMillis();
                       System.out.println("Time is:" + (endTime - startTime) + " 毫秒");
                   } catch (TesseractException e) {
                       e.printStackTrace();
                   }
                System.out.println("=========result:"+result);
            } else {
                System.out.print("文件不存在");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

重定向Controller

 


    @RequestMapping("/login")
    public void login(User user,HttpServletResponse response)throws Exception {
        String userName = user.getName();
        int password = user.getPassword();
        //模拟登陆返回结果
        Map<String,Object> restult  = ImitateLogin.HttpClientLogin();//body为获取的html代码
        String body = (String)restult.get("html");
        @SuppressWarnings("unchecked")
		List<Cookie> cookieList=(List<Cookie>)restult.get("cookies");
        
        //模拟登陆后 ,得到的Header
        @SuppressWarnings("unused")
   		List<Header>  headerList =(List<Header>)restult.get("headers");;
   		for(Header hd:headerList) {
   			 response.setHeader(hd.getName(), hd.getValue());
   	    }
   		 
        response.setContentType("text/html;charset=utf-8");
        //模拟登陆后,得到的Cookie
        for(Cookie ck:cookieList) {
        	javax.servlet.http.Cookie  c= new javax.servlet.http.Cookie(ck.getName(),ck.getValue());
        	c.setDomain(ck.getDomain());
        	c.setPath(ck.getPath());
        	c.setVersion(ck.getVersion());
        	c.setSecure(ck.getSecure());
        	c.setHttpOnly(true);
        	response.addCookie(c);
        }
        response.setStatus(302);
        // 重定向到第三方网站
        response.sendRedirect("http://localhost:8080/Thingworx/Runtime/index.html#mashup=dd123");
    }

httpClient模拟登录


    public static Map<String,Object> HttpClientLogin( ) {
    	Map<String,Object> restult = new HashMap<>();
    	String loginUrl = "http://localhost:8080/demo/action-login";
        String dataUrl  = "http://localhost:8080/demo/Runtime/index.html#mashup=dd123";
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        
        PostMethod postMethod = new PostMethod(loginUrl);
        NameValuePair[] postData = {
        		new NameValuePair("userid", "Administrator"),
                        new NameValuePair("password", "admin"),
                        new NameValuePair("code", getCode())
        };
        
        //设置登陆密码
        postMethod.setRequestBody(postData);
        
        //设置header
        postMethod.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        postMethod.setRequestHeader("Accept-Encoding:","gzip, deflate");
        postMethod.setRequestHeader("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        postMethod.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0");
        postMethod.setRequestHeader("Connection","keep-alive");
        postMethod.setRequestHeader("Content-Length","148");
        postMethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        postMethod.setRequestHeader("Host","localhost:8080");
        postMethod.setRequestHeader("Referer","http://localhost:8080/demo/FormLogin/DD");
        postMethod.setRequestHeader("Upgrade-Insecure-Requests","1");

        try {
        	//获取登陆后Cookie,Cookie中有JSESSIONID
            Cookie[] cookies = httpClient.getState().getCookies();
            StringBuffer stringBuffer = new StringBuffer();
            for (Cookie c : cookies) {
                stringBuffer.append(c.toString() + ";");
            }
            
            //获取登陆后首页数据
            GetMethod getMethod = new GetMethod(dataUrl);
            getMethod.setRequestHeader("Cookie", stringBuffer.toString());
            postMethod.setRequestHeader("Host", "localhost:8080");
            postMethod.setRequestHeader("Referer", "http://localhost:8080/demo/Runtime/index.html");
            postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0");
            httpClient.executeMethod(getMethod);

            String html = getMethod.getResponseBodyAsString();
            Header[] headers =  getMethod.getResponseHeaders();
            List<Header>  headerList = Arrays.asList(headers);
            System.out.println(html);
            //设置返回参数
            restult.put("html", html);
            restult.put("cookies", Arrays.asList(cookies));
            restult.put("headers", headerList);
            return restult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

October-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值