起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4326|回复: 1

给手机客户端加验证码

[复制链接]

11

主题

1134

帖子

1261

积分

金牌会员

Rank: 6Rank: 6

积分
1261
QQ
发表于 2014-4-2 15:35:06 | 显示全部楼层 |阅读模式
1.新建validate.j文件(/mobileUI/portal/validate.j)生成验证码的代码
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.image.BufferedImage;
  5. import java.io.IOException;
  6. import java.io.Writer;
  7. import java.util.Random;

  8. import javax.imageio.ImageIO;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import javax.servlet.http.HttpSession;


  13. public class Validate extends com.justep.ui.impl.JProcessorImpl  {
  14.         @Override
  15.         public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16.                 response.setHeader("Cache-Control","no-cache");
  17.                 int width=60,height=25;
  18.                 BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  19.                 Graphics g=image.getGraphics();
  20.                 g.setColor(new Color(200,200,200));
  21.                 g.fillRect(0,0, width, height);
  22.                 Random random=new Random();
  23.                 int randNum=random.nextInt(8999)+1000;
  24.                 String randStr=String.valueOf(randNum);
  25.                 HttpSession session=request.getSession();
  26.                 session.setAttribute("randStr",randStr);
  27.                 g.setColor(Color.black);
  28.                 g.setFont(new Font("",Font.PLAIN,20));
  29.                 g.drawString(randStr, 10, 17);
  30.                 for(int i=0;i<100;i++){
  31.                         int x=random.nextInt(width);
  32.                         int y=random.nextInt(height);
  33.                         g.drawOval(x, y, 1, 1);
  34.                 }
  35.                 ImageIO.write(image, "JPEG", response.getOutputStream());
  36.         }
  37. }
复制代码
2.新建validateServlet.j文件(/mobileUI/portal/validateServlet.j) 验证验证码代码
  1. import java.io.IOException;
  2. import java.io.PrintWriter;

  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;


  6. public class ValidateServlet extends com.justep.ui.impl.JProcessorImpl  {
  7.         public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  8.                 int flag=1;
  9.                 String verification_code=request.getParameter("verification_code");
  10.                 String code=(String) request.getSession().getAttribute("randStr");
  11.                 if(verification_code.trim().equals(code)){
  12.                         flag=0;
  13.                 }else{
  14.                         flag=1;
  15.                 }
  16.         response.setCharacterEncoding("UTF-8");
  17.         PrintWriter out = response.getWriter();
  18.         out.print(flag);
  19.         }
  20. }
复制代码
3.将mobileUI/portal/driectLogin.w 源码替换为如下代码:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <html xmlns="http://www.w3.org/1999/xhtml" sys-param="false">
  3.         <head>
  4.                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5.                 <title>X5 协同管理系统</title>
  6.                 <link rel="stylesheet" type="text/css" href="/UI/portal/x5/css/style.default.css" media="screen" title="defaultTheme"/>
  7.             <script type="text/javascript" src="/mobileUI/portal/system/js/jquery/jquery.js"></script>
  8.             <script type="text/javascript" src="/mobileUI/portal/system/js/md5.js"></script>
  9.             <script type="text/javascript" src="/mobileUI/portal/system/js/portal.js"></script>
  10.                 <script type="text/javascript">
  11.                 <![CDATA[
  12.                                 (function($){
  13.                             $.getUrlParam = function(name)
  14.                             {
  15.                                 var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  16.                                 var r = window.location.search.substr(1).match(reg);
  17.                                 if (r!=null) return unescape(r[2]); return '';
  18.                             }
  19.                         })(jQuery);
  20.                         jQuery(function(){
  21.                                 $("#login_button").click(function(event){
  22.                                         login();
  23.                                 });        
  24.                                 $("#validate").click(function(event){
  25.                                         this.src = this.src.replace("\?(.)*", '') + "?" + Math.random();
  26.                                 });               
  27.                         });
  28.                         login=function(){
  29.                                 var verification_code=$.trim($("#verification_code").val());
  30.                                 if(verification_code==""){
  31.                                         $("#hint_label").text("请输入验证码!").show();
  32.                                 }else{
  33.                                         $.ajax({
  34.                                                 async: false,
  35.                                                 type: 'POST',
  36.                                                 data:{
  37.                                                         verification_code:verification_code
  38.                                                 },
  39.                                                 url:'/x5/mobileUI/portal/validateServlet.j',
  40.                                                 error:function(error,status,text){
  41.                                                         alert("调用失败"+text);
  42.                                                 },
  43.                                                 success:function(result){
  44.                                                         if(result==0){
  45.                                                                 var password = "${request.query.password?first}";
  46.                                                                 var username = "${request.query.username?first}";
  47.                                                                 justep.mobile.Portal.baseUrl = "${baseUrl}";
  48.                                                                 var isIOS = $.getUrlParam('isIOS');
  49.                                                                 justep.mobile.Portal.isIOS = isIOS;
  50.                                                                 var result = justep.mobile.Portal.login(username, password, true, true);
  51.                                                         }else{
  52.                                                                 $("#hint_label").text("验证码输入错误").show();
  53.                                                                 $("#verification_code").val("");
  54.                                                         }
  55.                                                 }
  56.                                         });
  57.                                 }
  58.                         }
  59.                         ]]>
  60.                 </script>
  61.                 <style type="text/css">
  62.                         html, body {
  63.                             height: 100%;
  64.                     }
  65.             
  66.                     body{
  67.                             margin: 0;
  68.                             padding: 0;
  69.                             line-height: normal;
  70.                             font-family: 宋体, arial;
  71.                                 background: -webkit-gradient(linear, left top, left bottom, from(#D2E8F7), to(#fff));
  72.                     }
  73.                         #verification_code{
  74.                                 width:200px;
  75.                                 height:30px;
  76.                         }
  77.                         #login_button{
  78.                                 width:120px;
  79.                                 height:40px;
  80.                         }
  81.                 </style>
  82.         </head>   
  83.         <body onmousewheel="">
  84.                 <table border="0" align="center">
  85.                         <tr>
  86.                                 <td>
  87.                                         <div id="hint_label" style="color:red"></div>
  88.                                 </td>
  89.                         </tr>
  90.                         <tr align="center">
  91.                                 <td>验证码</td>
  92.                         </tr>
  93.                         <tr>
  94.                                 <td>
  95.                                         <input type="text" id="verification_code" value=""></input>
  96.                                 </td>
  97.                         </tr>
  98.                         <tr align="center">
  99.                                 <td>
  100.                                         <img id="validate" src="validate.j" title="换一张" style="height:30px;width:100px;cursor:pointer;cursor:hand;"></img>
  101.                                 </td>
  102.                         </tr>
  103.                         <tr>
  104.                                 <td align="center">
  105.                                         <button id="login_button" onclick="return false;">登录</button>
  106.                                 </td>
  107.                         </tr>
  108.                 </table>
  109.         </body>
  110. </html>
复制代码
4.效果如下
1025014xecei6eeez2tpce.jpg
远程的联系方法QQ2025089647。添加好友时,需要填写论坛账号
发远程时同时也发一下帖子的地址,方便了解要

0

主题

3

帖子

7

积分

新手上路

Rank: 1

积分
7
QQ
发表于 2023-8-1 12:15:18 | 显示全部楼层
"最好的朋友是你们静坐在游廊上,一句话也不说,当你们各自走开的时候,仍感到你们经历了一场十分精彩的对话。,100%福彩3D 死亡教会人一切,如同考试之后公布的结果——虽然恍然大悟,但为时晚矣!
要让事情改变,先改变我自己;要让事情变得更好,先让自己变得更好。 查看168排列3,每个人都有性格、更有自己的底线。做什么事千万不要去超过那个人的底线,否者后果真地不堪设想。
希望是本无所谓有,无所谓无的。这正如地上的路,其实地上本没有路,走的人多了,也便成了路。 查看快乐8 ,支配战士行动的力量是信仰。他能够忍受一切艰难、痛苦,达到他所选定的目标。每个人都有性格、更有自己的底线。做什么事千万不要去超过那个人的底线,否者后果真地不堪设想。,查看168开奖"


高级模式
B Color Image Link Quote Code Smilies

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-4-24 15:22 , Processed in 0.116631 second(s), 26 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表