日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

captcha库_将CAPTCHA添加到您的GWT应用程序

發(fā)布時(shí)間:2023/12/3 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 captcha库_将CAPTCHA添加到您的GWT应用程序 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

captcha庫

什么是驗(yàn)證碼?
在一個(gè)充滿惡意機(jī)器人的世界中,您應(yīng)該怎么做才能保護(hù)您寶貴的Web應(yīng)用程序? 您真正應(yīng)該做的基本事情之一就是向其中添加CAPTCHA功能。 如果您不熟悉(聽起來有些奇怪),則CAPTCHA是確保用戶實(shí)際上是真實(shí)人物而不是計(jì)算機(jī)的簡(jiǎn)單方法。 這可以通過挑戰(zhàn)用戶并要求他提供對(duì)“問題”的響應(yīng)來完成。 由于計(jì)算機(jī)無法解決驗(yàn)證碼問題,因此假定輸入正確解決方案的任何用戶都是人。 最常見的方法是要求用戶從出現(xiàn)在屏幕上的變形圖像中鍵入字母或數(shù)字。

注冊(cè)網(wǎng)站時(shí),您很可能已經(jīng)看到了一個(gè)驗(yàn)證碼文件。 以下是來自維基百科的CAPTCHA示例。


將SimpleCaptcha添加到您的應(yīng)用程序
在本教程中,我將CAPTCHA功能集成到Web應(yīng)用程序中。 我將使用SimpeCaptcha框架 ,該框架用于為Java生成CAPTCHA圖像/答案對(duì)。 該站點(diǎn)提供了一個(gè)安裝指南 ,但是該指南指的是普通的基于JSP的舊應(yīng)用程序。 我將向您展示如何將該框架與您喜歡的GWT項(xiàng)目集成。

(我將假定您已經(jīng)在系統(tǒng)上啟動(dòng)并運(yùn)行了GWT ,以及用于Eclipse的Google插件 )

首先,讓我們創(chuàng)建我們的Eclipse項(xiàng)目。 選擇文件 ? Web應(yīng)用程序項(xiàng)目”,并提供必要的信息,如下圖所示。 該項(xiàng)目的名稱將為“ CaptchaGwtProject”。 確保包括對(duì)GWT的支持,但不包括Google App Engine。

在繼續(xù)之前,請(qǐng)注意此處。 SimpleCaptcha大量使用AWT類來執(zhí)行圖像渲染。 但是,您可能知道,App Engine不支持所有JRE類,最具體地說,其中僅包含AWT包中的幾個(gè)。 您可以查看AppEngine JRE類白名單以了解更多詳細(xì)信息。 因此,不可能將框架合并到將在App Engine上部署的應(yīng)用程序中,并且僅應(yīng)將其與在標(biāo)準(zhǔn)JRE上運(yùn)行的平臺(tái)一起使用。

下一步是從SourceForge下載該庫(我使用1.1.1版本 )。 將下載的JAR文件添加到項(xiàng)目的類路徑中。 另外,不要忘記將JAR文件復(fù)制到“ CaptchaGwtProject \ war \ WEB-INF \ lib”文件夾,因?yàn)榍度胧饺萜鞯?#xff08;Jetty)運(yùn)行時(shí)將需要該文件。 您可以在此處找到該項(xiàng)目的JavaDoc頁面。

設(shè)置客戶端
Eclipse將自動(dòng)創(chuàng)建應(yīng)用程序的框架,并創(chuàng)建一些示例文件。 找到CaptchaGwtProject類,它是應(yīng)用程序的入口點(diǎn)。 刪除現(xiàn)有內(nèi)容,并將其替換為以下內(nèi)容:

package com.javacodegeeks.captcha.client;import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox;public class CaptchaGwtProject implements EntryPoint {private final SignupServiceAsync signupService = GWT.create(SignupService.class);private final Button sendButton = new Button("Sign Up");public void onModuleLoad() {final TextBox usernameField = new TextBox();usernameField.setText("Username here");final TextBox passwordField = new TextBox();passwordField.setText("Password here");final TextBox captchaField = new TextBox();captchaField.setText("CAPTCHA Word here");final Label responseLabel = new Label(); final Image captchaImage = new Image("/SimpleCaptcha.jpg");usernameField.setFocus(true);sendButton.addStyleName("sendButton");RootPanel.get("usernameFieldContainer").add(usernameField);RootPanel.get("passwordFieldContainer").add(passwordField);RootPanel.get("captchaFieldContainer").add(captchaField);RootPanel.get("sendButtonContainer").add(sendButton);RootPanel.get("captchaImageContainer").add(captchaImage);RootPanel.get("responseLabelContainer").add(responseLabel);class MyHandler implements ClickHandler, KeyUpHandler {public void onClick(ClickEvent event) {sendDataToServer();}public void onKeyUp(KeyUpEvent event) {if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {sendDataToServer();}}private void sendDataToServer() { String username = usernameField.getText();String password = passwordField.getText();String captcha = captchaField.getText();sendButton.setEnabled(false); signupService.performSignup(username, password, captcha, signupCallback); }}MyHandler handler = new MyHandler();sendButton.addClickHandler(handler);usernameField.addKeyUpHandler(handler);}private AsyncCallback signupCallback = new AsyncCallback() { @Overridepublic void onSuccess(Boolean result) {if (result) {Window.alert("CAPTCHA was valid");}else {Window.alert("CAPTCHA was invalid");} sendButton.setEnabled(true);} @Overridepublic void onFailure(Throwable caught) {Window.alert("Error occurred while communicating with server");sendButton.setEnabled(true);} };}

該代碼簡(jiǎn)單明了,并且基于自動(dòng)創(chuàng)建的示例類。 我們?yōu)橛脩糨斎胩砑恿藘蓚€(gè)TextBox ,為服務(wù)器響應(yīng)添加了Label 。 我們還添加了一個(gè)Image實(shí)例,它將作為我們的驗(yàn)證碼的占位符。 我們將其URL設(shè)置為“ /SimpleCaptcha.jpg”,該URL將由框架處理。 最后,使用一個(gè)Button來調(diào)用對(duì)服務(wù)器的調(diào)用。 AsyncCallback使用布爾值來表示服務(wù)器上驗(yàn)證碼驗(yàn)證失敗的成功。 請(qǐng)注意,故意遺漏了Web應(yīng)用程序的某些基本部分(例如用戶輸入驗(yàn)證),因此我們可以專注于CAPTCHA部分。

接下來,在項(xiàng)目的“ war”文件中找到名為“ CaptchaGwtProject.html”HTML文件。 編輯文件并為我們的GWT對(duì)象添加一些容器。 代碼如下:

<!doctype html> <!-- The DOCTYPE declaration above will set the --> <!-- browser's rendering engine into --> <!-- "Standards Mode". Replacing this declaration --> <!-- with a "Quirks Mode" doctype may lead to some --> <!-- differences in layout. --><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!-- --><!-- Consider inlining CSS to reduce the number of requested files --><!-- --><link type="text/css" rel="stylesheet" href="CaptchaGwtProject.css"><!-- --><!-- Any title is fine --><!-- --><title>Web Application Starter Project</title><!-- --><!-- This script loads your compiled module. --><!-- If you add any GWT meta tags, they must --><!-- be added before this line. --><!-- --><script type="text/javascript" language="javascript" src="captchagwtproject/captchagwtproject.nocache.js"></script></head><!-- --><!-- The body can have arbitrary html, or --><!-- you can leave the body empty if you want --><!-- to create a completely dynamic UI. --><!-- --><body><!-- OPTIONAL: include this if you want history support --><iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe><!-- RECOMMENDED if your web app will not function without JavaScript enabled --><noscript><div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">Your web browser must have JavaScript enabledin order for this application to display correctly.</div></noscript><h1>CAPTCHA Secured Web Application</h1><table align="center"><tr><td colspan="2" style="font-weight:bold;">Please enter your username:</td><td id="usernameFieldContainer"></td></tr><tr><td colspan="2" style="font-weight:bold;">Please enter your password:</td><td id="passwordFieldContainer"></td></tr><tr><td colspan="2" style="font-weight:bold;">Please enter the word:</td><td id="captchaFieldContainer"></td></tr><tr><td id="sendButtonContainer"></td> </tr><tr><td id="captchaImageContainer"></td> </tr><tr><td colspan="2" style="color:red;" id="responseLabelContainer"></td></tr></table></body> </html>

請(qǐng)注意,對(duì)自動(dòng)生成的文件的唯一更改是在<h1>標(biāo)記之后。

我們的異步GWT服務(wù)將非常簡(jiǎn)單,僅執(zhí)行一項(xiàng)功能。 相應(yīng)的兩個(gè)接口如下所示:

package com.javacodegeeks.captcha.client;import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;@RemoteServiceRelativePath("signup") public interface SignupService extends RemoteService {boolean performSignup(String username, String password, String userCaptcha);}package com.javacodegeeks.captcha.client;import com.google.gwt.user.client.rpc.AsyncCallback;public interface SignupServiceAsync {void performSignup(String username, String password, String userCaptcha,AsyncCallback callback);}

(請(qǐng)注意,自動(dòng)生成的“ greetingService”類已被刪除)

準(zhǔn)備服務(wù)器端
在服務(wù)器端,我們從庫中使用的主要對(duì)象是Captcha 。 要檢索驗(yàn)證碼的值(并將其與用戶輸入進(jìn)行比較),我們必須獲取對(duì)與特定會(huì)話關(guān)聯(lián)的HttpSession對(duì)象的引用。 可以通過相應(yīng)的HttpServletRequest對(duì)象檢索HttpSession。 這是標(biāo)準(zhǔn)的Java EE內(nèi)容。 不要忘記服務(wù)器端GWT服務(wù)繼承自RemoteServiceServlet ,而RemoteServiceServlet繼承自HttpServletRequest。 可以通過調(diào)用getThreadLocalRequest方法獲得基礎(chǔ)請(qǐng)求。 請(qǐng)注意,正如API所提到的,這是本地存儲(chǔ)在線程中的,因此同時(shí)調(diào)用可以具有不同的請(qǐng)求對(duì)象。

服務(wù)器端的具體實(shí)現(xiàn)如下:

package com.javacodegeeks.captcha.server;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession;import nl.captcha.Captcha;import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.javacodegeeks.captcha.client.SignupService;@SuppressWarnings("serial") public class SignupServiceImpl extends RemoteServiceServlet implements SignupService {public boolean performSignup(String username, String password, String userCaptcha) {HttpServletRequest request = getThreadLocalRequest();HttpSession session = request.getSession();Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);return captcha.isCorrect(userCaptcha);} }

擴(kuò)展SimpleCaptcha
最后一步是設(shè)置Servlet,該Servlet將生成顯示給用戶的圖像。 通過創(chuàng)建從提供的SimpeCaptchaServlet類繼承的類,可以輕松擴(kuò)展SimpleCaptcha。 相關(guān)代碼如下:

package com.javacodegeeks.captcha.server.servlet;import static nl.captcha.Captcha.NAME;import java.io.IOException;import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;import nl.captcha.Captcha; import nl.captcha.backgrounds.GradiatedBackgroundProducer; import nl.captcha.servlet.CaptchaServletUtil; import nl.captcha.servlet.SimpleCaptchaServlet;public class ExtendedCaptchaServlet extends SimpleCaptchaServlet {private static final long serialVersionUID = 6560171562324177699L;@Overridepublic void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {HttpSession session = req.getSession();Captcha captcha = new Captcha.Builder(_width, _height).addText().addBackground(new GradiatedBackgroundProducer()).gimp().addNoise().addBorder().build();session.setAttribute(NAME, captcha);CaptchaServletUtil.writeImage(resp, captcha.getImage());}}

作為初始化參數(shù)傳遞并從父類讀取的“ _width”和“ _height”變量。 為了創(chuàng)建一個(gè)新的Captcha對(duì)象,我們使用Captcha.Builder類(依賴于構(gòu)建器模式)。 然后,我們將對(duì)象傳遞到特定會(huì)話,并將關(guān)聯(lián)的BufferedImage流傳輸?shù)絪ervlet響應(yīng)。

請(qǐng)注意,我們的實(shí)現(xiàn)每次用戶執(zhí)行頁面請(qǐng)求時(shí)都會(huì)生成一個(gè)新圖像。 這與在給定會(huì)話中使用相同映像的默認(rèn)SimpleCaptcha實(shí)現(xiàn)不同。

配置Web應(yīng)用程序
所有組件都通過Web應(yīng)用程序的“ web.xml”描述符捆綁在一起,在我們的示例中,描述符如下:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-appPUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><!-- Default page to serve --><welcome-file-list><welcome-file>CaptchaGwtProject.html</welcome-file></welcome-file-list><!-- Servlets --><servlet><servlet-name>signupServlet</servlet-name><servlet-class>com.javacodegeeks.captcha.server.SignupServiceImpl</servlet-class></servlet><servlet-mapping><servlet-name>signupServlet</servlet-name><url-pattern>/captchagwtproject/signup</url-pattern></servlet-mapping><servlet><servlet-name>SimpleCaptcha</servlet-name><servlet-class>com.javacodegeeks.captcha.server.servlet.ExtendedCaptchaServlet</servlet-class><init-param><param-name>width</param-name><param-value>200</param-value></init-param><init-param><param-name>height</param-name><param-value>50</param-value></init-param></servlet><servlet-mapping><servlet-name>SimpleCaptcha</servlet-name><url-pattern>/SimpleCaptcha.jpg</url-pattern></servlet-mapping></web-app>

我們聲明GWT服務(wù)(類“ SignupServiceImpl”)和歡迎文件。 這里沒什么特別的。 最后,我們聲明將負(fù)責(zé)圖像生成并在“'/SimpleCaptcha.jpg”URL下處理請(qǐng)求的servlet(請(qǐng)記住,此請(qǐng)求在GWT入口點(diǎn)中使用)。 我們還為servlet提供了初始化參數(shù)(寬度和高度)。

而已! 運(yùn)行該項(xiàng)目,您應(yīng)該看到類似以下內(nèi)容的內(nèi)容:

每次刷新頁面時(shí),都會(huì)創(chuàng)建一個(gè)新的CAPTCHA映像。 單擊“注冊(cè)”按鈕后,字段內(nèi)容將發(fā)送到服務(wù)器,在服務(wù)器上用戶提供的CAPTCHA值將針對(duì)當(dāng)前會(huì)話中存在的值進(jìn)行測(cè)試。 如果發(fā)生故障,我們將看到:


而已。 現(xiàn)在,您的應(yīng)用程序可以更安全一些。

您可以在此處找到完整的Eclipse項(xiàng)目。

更新:一位讀者詢問是否可以添加“重新加載圖像”功能。 確實(shí)有可能,但是從服務(wù)器重新獲取圖像時(shí)必須小心。 如以下討論所述,瀏覽器將不會(huì)重新獲取圖像(因?yàn)樗哂邢嗤腢RL)。 訣竅是添加一個(gè)虛擬參數(shù),該參數(shù)將在每次刷新時(shí)更改。 在這里看看文章:

http://groups.google.gy/group/google-web-toolkit/browse_thread/thread/be9f1da56b5b1c18

事實(shí)是,我創(chuàng)建了該項(xiàng)目的新版本,可以在此處獲取(按雙關(guān)目標(biāo))。

請(qǐng)享用!

相關(guān)文章 :
  • 使用Spring Security保護(hù)GWT應(yīng)用程序的安全
  • GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示
  • 建立自己的GWT Spring Maven原型
  • GWT EJB3 Maven JBoss 5.1集成教程

翻譯自: https://www.javacodegeeks.com/2010/06/add-captcha-gwt-application.html

captcha庫

總結(jié)

以上是生活随笔為你收集整理的captcha库_将CAPTCHA添加到您的GWT应用程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。