|
楼主 |
发表于 2014-4-1 16:46:15
|
显示全部楼层
给手机客户端加验证码
1.新建validate.j文件(/mobileUI/portal/validate.j)生成验证码的代码- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- public class Validate extends com.justep.ui.impl.JProcessorImpl {
- @Override
- public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setHeader("Cache-Control","no-cache");
- int width=60,height=25;
- BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics g=image.getGraphics();
- g.setColor(new Color(200,200,200));
- g.fillRect(0,0, width, height);
- Random random=new Random();
- int randNum=random.nextInt(8999)+1000;
- String randStr=String.valueOf(randNum);
- HttpSession session=request.getSession();
- session.setAttribute("randStr",randStr);
- g.setColor(Color.black);
- g.setFont(new Font("",Font.PLAIN,20));
- g.drawString(randStr, 10, 17);
- for(int i=0;i<100;i++){
- int x=random.nextInt(width);
- int y=random.nextInt(height);
- g.drawOval(x, y, 1, 1);
- }
- ImageIO.write(image, "JPEG", response.getOutputStream());
- }
- }
复制代码 2.新建validateServlet.j文件(/mobileUI/portal/validateServlet.j) 验证验证码代码- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class ValidateServlet extends com.justep.ui.impl.JProcessorImpl {
- public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- int flag=1;
- String verification_code=request.getParameter("verification_code");
- String code=(String) request.getSession().getAttribute("randStr");
- if(verification_code.trim().equals(code)){
- flag=0;
- }else{
- flag=1;
- }
- response.setCharacterEncoding("UTF-8");
- PrintWriter out = response.getWriter();
- out.print(flag);
- }
- }
复制代码 3.将mobileUI/portal/driectLogin.w 源码替换为如下代码:- <?xml version="1.0" encoding="UTF-8"?>
- <html xmlns="http://www.w3.org/1999/xhtml" sys-param="false">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>X5 协同管理系统</title>
- <link rel="stylesheet" type="text/css" href="/UI/portal/x5/css/style.default.css" media="screen" title="defaultTheme"/>
- <script type="text/javascript" src="/mobileUI/portal/system/js/jquery/jquery.js"></script>
- <script type="text/javascript" src="/mobileUI/portal/system/js/md5.js"></script>
- <script type="text/javascript" src="/mobileUI/portal/system/js/portal.js"></script>
- <script type="text/javascript">
- <![CDATA[
- (function($){
- $.getUrlParam = function(name)
- {
- var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
- var r = window.location.search.substr(1).match(reg);
- if (r!=null) return unescape(r[2]); return '';
- }
- })(jQuery);
- jQuery(function(){
- $("#login_button").click(function(event){
- login();
- });
- $("#validate").click(function(event){
- this.src = this.src.replace("\?(.)*", '') + "?" + Math.random();
- });
- });
- login=function(){
- var verification_code=$.trim($("#verification_code").val());
- if(verification_code==""){
- $("#hint_label").text("请输入验证码!").show();
- }else{
- $.ajax({
- async: false,
- type: 'POST',
- data:{
- verification_code:verification_code
- },
- url:'/x5/mobileUI/portal/validateServlet.j',
- error:function(error,status,text){
- alert("调用失败"+text);
- },
- success:function(result){
- if(result==0){
- var password = "${request.query.password?first}";
- var username = "${request.query.username?first}";
- justep.mobile.Portal.baseUrl = "${baseUrl}";
- var isIOS = $.getUrlParam('isIOS');
- justep.mobile.Portal.isIOS = isIOS;
- var result = justep.mobile.Portal.login(username, password, true, true);
- }else{
- $("#hint_label").text("验证码输入错误").show();
- $("#verification_code").val("");
- }
- }
- });
- }
- }
- ]]>
- </script>
- <style type="text/css">
- html, body {
- height: 100%;
- }
-
- body{
- margin: 0;
- padding: 0;
- line-height: normal;
- font-family: 宋体, arial;
- background: -webkit-gradient(linear, left top, left bottom, from(#D2E8F7), to(#fff));
- }
- #verification_code{
- width:200px;
- height:30px;
- }
- #login_button{
- width:120px;
- height:40px;
- }
- </style>
- </head>
- <body onmousewheel="">
- <table border="0" align="center">
- <tr>
- <td>
- <div id="hint_label" style="color:red"></div>
- </td>
- </tr>
- <tr align="center">
- <td>验证码</td>
- </tr>
- <tr>
- <td>
- <input type="text" id="verification_code" value=""></input>
- </td>
- </tr>
- <tr align="center">
- <td>
- <img id="validate" src="validate.j" title="换一张" style="height:30px;width:100px;cursor:pointer;cursor:hand;"></img>
- </td>
- </tr>
- <tr>
- <td align="center">
- <button id="login_button" onclick="return false;">登录</button>
- </td>
- </tr>
- </table>
- </body>
- </html>
复制代码 4.效果如下:
|
|