目录
概述:
简单实现一个跨域重定向功能。
首先安装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;
}