高校登录界面预览
模拟登录分析
说明:
Request URL 登录请求的地址(固定)
Request Method 登录请求的方式(固定)
Location 登录成功后跳转的页面(请求返回的结果)
Cookie 当前会话信息(第一次访问登录界面,会返回一个Cookie信息)
Form Data 需要Post的参数
- __VIEWSTATE (固定)
txtUserName (学号)
btnLogin.x (登录按钮点击的位置)
btnLogin.y (登录按钮点击的位置)
txtPassword (密码)
CheckCode (验证码) - __EVENTVALIDATION (固定)
使用Spring Boot 开发模拟登录功能
效果图
//模拟登陆的服务层
@Service
@Log4j
public class CtguService {
//登录请求地址
private final static String LOGIN = "http://210.42.38.26:84/jwc_glxt/Login.aspx?__VIEWSTATE=%s&__EVENTVALIDATION=%s&txtUserName=%s&btnLogin.x=0&btnLogin.y=0&txtPassword=%s&CheckCode=%s";
//登录界面
private final static String LOGIN_VIEW = "http://210.42.38.26:84/jwc_glxt/";
//验证码请求地址
private final static String CHECK_CODE = "http://210.42.38.26:84/jwc_glxt/ValidateCode.aspx";
//固定的参数值(URL编码)
private static final String VIEWSTATE = "%2FwEPDwUKMTQ4NjM5NDA3OWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFCGJ0bkxvZ2luU077LK9itKNe3fhI7aoZZ%2BS5Ryo%3D";
private static final String EVENTVALIDATION = "%2FwEWBQKOmrqLAwKl1bKzCQKC3IeGDAK1qbSRCwLO44u1DVzfq830wXTY29pyqB1kTMdgWLfG";
private Map<String, String> cookies;
//验证码
private byte[] checkPic;
//模拟访问登录界面
public void getCookiesFromJwc(){
try {
//第一次访问登录界面
Response response = Jsoup.connect(LOGIN_VIEW).timeout(60000).execute();
//得到系统返回的Cookies
cookies = response.cookies();
//请求获得验证码的内容
checkPic = Jsoup.connect(CHECK_CODE).cookies(cookies).ignoreContentType(true).execute().bodyAsBytes();
} catch (IOException e) {
e.printStackTrace();
}
}
//登录校验
public boolean loginCheck(String username, String password, String checkCode){
try {
String loginUrl = String.format(LOGIN, VIEWSTATE, EVENTVALIDATION, username, password, checkCode);
log.info("学号:" + username + " 密码:" + password);
Document document = Jsoup.connect(loginUrl).cookies(cookies).timeout(60000).ignoreContentType(true).get();
//登录成功后,返回的Document有此节点,作为是否登录成功的依据
Elements elements = document.select("span#ctl00_lblSignIn");
if (!elements.isEmpty()){
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public byte[] getCheckPic(){
return checkPic;
}
}
//模拟登录视图层
@Controller
public class HomeController {
@Autowired
private CtguService ctguService;
//登录界面
@RequestMapping("login")
public String login(Model model){
ctguService.getCookiesFromJwc();
return "login";
}
//验证码显示
@RequestMapping("/ctgu/check")
public void getPic(HttpServletResponse response) throws IOException {
byte[] picBytes = ctguService.getCheckPic();
response.setContentType("image/png");
OutputStream stream = response.getOutputStream();
stream.write(picBytes);
stream.flush();
stream.close();
}
}
//html前端代码
<p class="lead col-md-6 col-sm-12 col-md-offset-3">
<input type="text" id="username" placeholder="学号" class="form-control">
</p>
<p class="lead col-md-6 col-sm-12 col-md-offset-3">
<input type="text" id="password" placeholder="教务处密码" class="form-control">
</p>
<p class="lead col-md-6 col-sm-10 col-md-offset-3">
<input type="text" id="checkCode" placeholder="验证码" class="form-control">
</p>
<p class="lead col-md-2 col-sm-2">
<img th:src="@{/ctgu/check}" style="border-width:0px;">
</p>
<p class="lead col-md-6 col-sm-12 col-md-offset-3">
<a href="#" class="btn btn-lg btn-success" id="signIn">注册</a>
</p>
疑点
支持高并发的话,应该要对Service层的Cookies 和 checkPic额外处理,这就留给读者们去思考了,并发量不大的话,这样处理就没什么问题了
补充
- 个人原因,不能提供完整项目代码,有问题可以留言;
- 核心框架SpringBoot,依赖了Jsoup 和 Lombok;
- 中间有很多细节,为什么要在拿到cookie后直接去获取验证码?不获取会发生?有兴趣的可以自行尝试