CAPTCHA 全称 Completely Automated Public Turing Test to Tell Computers and Humans Apart,最早作为卡内基梅隆大学的一个科研项目,用于生成一个人类容易通过而计算机难以通过的测试,目前广泛应用于网络应用,用于阻止机器人发布垃圾信息。JCaptcha 即为 Java 版本的 CAPTCHA 项目,其是一个开源项目,支持生成图形和声音版的验证码,在生成声音版的验证码时,需要使用到 FreeTTS。目前,JCaptcha 官方网站显示有 2.0 版本,但二进制版只有 1.0 版可供下载,本文亦是基于 1.0 版本展开。
回页首
一个简单的图形验证码
JCaptcha 提供了一定的可扩展能力,用于开发人员创建出复杂的图形验证码。下面,首先利用 JCaptcha 提供的 API 来快速开发一个简单示例。本文的示例为 Web 应用,可以运行在 Tomcat 和 WebSphere 上,除了准备 Web 服务器外,我们还需要准备好 JCaptcha 运行时所必须的 Jar 包。
准备所需的 Jar 包
JCaptcha 项目在实现中,还引用了 commons-collections 和 commons-logging 两个开源项目,再加上 JCaptcha 本身的实现,我们共需要三个包,具体信息如下:
- jcaptcha-1.0-all.jar
- commons-logging-1.1.1.jar
- commons-collections-3.2.jar
创建生成图形验证码的 Servlet
在示例 1 中,我们采用 Servlet 生成验证码图片,并将这个图片写到 ServletOutputStream 中,同时将 Servlet 的 response 的类型设置为 image/jpeg。在前台页面中,我们使用 img 标签,并将 src 指向这个 Servlet,这样我们就可以在页面中显示这个生成的验证码图片。清单 1 是 Servlet 代码片段。
清单 1. 生成图形验证码代码片段
// set content type as jpeg httpServletResponse.setHeader("Cache-Control", "no-store"); httpServletResponse.setHeader("Pragma", "no-cache"); httpServletResponse.setDateHeader("Expires", 0); httpServletResponse.setContentType("image/jpeg"); // create the image using session ID logger.fine("tring to get image captcha service"); BufferedImage bufferedImage = service .getImageChallengeForID(httpServletRequest.getSession(true) .getId()); ServletOutputStream servletOutputStream = httpServletResponse .getOutputStream(); // write the image to the servlet output stream logger.fine("tring to output buffered image to servlet output stream"); ImageIO.write(bufferedImage, "jpg", servletOutputStream); try { servletOutputStream.flush(); } finally { servletOutputStream.close(); }
清单 1 中的 service 对象,我们采用了一个 JCaptcha 的默认实现类 DefaultManageableImageCaptchaService,在代码中,我们需要通过获取 service 对象,根据 Session ID 来生成验证码图片。除了使用该默认实现类外,我们也可以通过实现 ImageCaptchaService 接口来生成更为复杂的验证码图片(参见示例 2)。清单 2 为获取 service 对象的示例代码,JCaptcha 建议使用单例来生成 service 对象。
清单 2. 生成 ImageCaptchaService 对象
public class SampleImageCaptchaService { private static ImageCaptchaService instance; /** * Get default manageable image captcha service * @return ImageCaptchaService */ public static ImageCaptchaService getInstance() { if (instance == null) { instance = new DefaultManageableImageCaptchaService(); } return instance; } }
示例 1 提供了 2 个简单的页面,一个用于显示图形验证码,另一个则在验证成功后显示成功信息。清单 3 为用于显示图形验证码的页面代码片段。
清单 3. 显示图形验证码页面代码片段
<form action="CaptchaValidationServlet" method="post"> <table> <tr> <td colspan="2"><img src=/blog_article/"ImageCaptchaServlet"/index.html /></td> </tr> <tr> <td> 请输入您所看到的字符 :</td> <td><input type="text" name="captcha_input" value="" /> <%=request.getAttribute("ERROR") == null ? "" : request.getAttribute("ERROR")%></td> </tr> <tr> <td><input type="submit" value="提交" /></td> </tr> </table> </form>
This walkthrough demonstrates how to import a reusable workflow created in SharePoint Designer 2013 into a Visual Studio SharePoint workflow project.
Workflows created in SharePoint Designer, or declarative workflows, consist of XML statements instead of code. SharePoint Designer 2013 introducesreusable workflows, which are portable, declarative workflows that can be used by different lists in SharePoint sites.
Workflows created in Visual Studio 2013, such as sequential and state machine workflows, are calledcode workflows. Code workflows consist of XML files and code modules in which users can customize the workflow's behavior.
Visual Studio allows you to import reusable workflows created in SharePoint Designer 2013 and convert them to code workflows for use in your SharePoint sites.
This walkthrough demonstrates the following tasks:
-
Creating a simple, reusable workflow in SharePoint Designer.
-
Exporting the SharePoint Designer reusable workflow to a .wsp file and into SharePoint.
-
Importing the .wsp file into Visual Studio by using the Import Reusable Workflow project.
-
Altering the workflow by adding code.
-
Using the imported workflow in a SharePoint site.
Because SharePoint does not include any reusable workflows that you can use for this example, you will create one.In this simple workflow, when a user enters a new task in the Task list that has a specific title, the task is assigned to that user.
First you create two new SharePoint subsites: one to host the reusable workflows from SharePoint Designer, another to host the converted workflows.
To create SharePoint subsitesIn SharePoint Designer 2010, on the menu bar, chooseFile,New Blank Web Site
In theNew Blank Web Site dialog box, browse to a SharePoint site where you want to create the workflow, or use the value of http://SystemName/ and then choose theOK button.The Home page appears.
In the Subsites section, choose the New button.
In the New dialog box, choose SharePoint Templates from the list in the left pane, and chooseTeam Site from the list in the right pane.
In the Specify the location of the Web site box, replace the word subsite in the URL with SPD1, and then choose the OK button.
This opens the new subsite into SharePoint Designer.Close this instance of SharePoint Designer and go back to the first instance (the top-level site).
Repeat steps 3 - 5 to create the second subsite, this time replacing the wordsubsite in the URL with SPD2.
To create a SharePoint Designer reusable workflow
In the Subsites section, choose theSPD1 site to modify it.
On the ribbon, choose the Reusable Workflow button.
The Create Reusable Workflow wizard appears.
In the Name box, enter SPD Task Workflow.
In the Content Type list, chooseTask, and then choose theOK button.
The workflow opens in the SharePoint Designer workflow designer.
In the workflow designer, choose Step 1, and then, on the ribbon, choose theCondition button.
In the list of conditions, choose If current item field equals value.
This step adds a condition that’s named If field equals value.
In the If field equals value condition, choose thefield link.
In the list of values, choose Title.
In the If field equals value condition, choose thevalue link.
In the box, enter New task.
The condition statement now reads If Current Item:Title equals New task.
Choose the line under the condition statement, and then, on the ribbon, choose theAction button.
In the list of actions, choose Set field in current item.
In the Set field to value action, choose thefield link, and then, in the list, chooseAssigned to.
In the Set field to value action, choose thevalue link, and then, in the list of existing users and groups, chooseUser who created the item.
Choose the Add button, and then choose theOK button.
The action statement now reads Set Assigned To to Current Item:CreatedBy.
Because Visual Studio can import only .wsp files, you must save the reusable workflow as a .wsp file and deploy it to SharePoint before importing it into Visual Studio.