IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Servlets/JSP Java Discussion :

Formulaire Captcha en J2ee ?


Sujet :

Servlets/JSP Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de silverfab34
    Inscrit en
    Mars 2006
    Messages
    203
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Mars 2006
    Messages : 203
    Par défaut Formulaire Captcha en J2ee ?
    Bonjour à tous,
    je suis à la recherche d'information sur Captcha pour créer une image de validation sur un formulaire.
    J'ai trouvé pas mal de docs sur Captcha......mais en Php, moi je suis en Java/J2ee et mes pages sont des Ftl !!! Ceci dit, mon formulaire est des plus basique !
    Si quelqu'un à une idée sur comment faire, je suis preneur !
    Merci et bonne journée !

  2. #2
    Membre confirmé Avatar de silverfab34
    Inscrit en
    Mars 2006
    Messages
    203
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Mars 2006
    Messages : 203
    Par défaut
    Bon, j'ai trouvé JCaptcha mais il n'y a pas bcp de docs !

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    802
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 802
    Par défaut
    Regarde aussi SimpleCaptcha, leur site à l'air bien documenté :
    http://simplecaptcha.sourceforge.net/index.html

  4. #4
    Membre confirmé Avatar de silverfab34
    Inscrit en
    Mars 2006
    Messages
    203
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Mars 2006
    Messages : 203
    Par défaut
    Oui j'ai, merci
    je vu qu'il fallait passer par un fichier web.xml...pour le moment, mon formulaire je le valide par du javascript...et c'est une page FTL !
    Je vois pas encore comment je vais faire ca mais bon il faut creuser....

  5. #5
    Membre confirmé Avatar de silverfab34
    Inscrit en
    Mars 2006
    Messages
    203
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Mars 2006
    Messages : 203
    Par défaut
    Bon, j'ai compris le principe mais ca ne fonctionne pas !
    Je n'ai pas d'affichae de l'image :
    Comme j'ai un fichier controller.xml, j'y ais mis dedans :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
      <request-map uri="Captcha">
    	    <security https="false" auth="false"/>
          	<event type="java" path="nl.captcha.servlet.CaptchaServlet" invoke="doGet"/>
     	  	<response name="success" type="view" value="Captcha"/>
        </request-map>
        <!-- Fin Captcha  -->
     
     
         <!--Captcha  -->
        <view-map name="Captcha" page="/Captcha.jpg"/>
    Ensuite dans ma FTL :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    <input type="text" name="imageText" maxlength="4"/>
     
    <img src="/Captcha">
     
    <%
            String imageText = (String)session.getAttribute(nl.captcha.servlet.Constants.SIMPLE_CAPCHA_SESSION_KEY);
     
    %>
    et dans mon web.xml :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
     
    <servlet>
    		<servlet-name>SimpleCaptcha</servlet-name>
    		<servlet-class>nl.captcha.servlet.SimpleCaptcha</servlet-class>
    	</servlet>
     
    	<servlet-mapping>
    		<servlet-name>SimpleCaptcha</servlet-name>
    		<url-pattern>/SimpleCaptcha.jpg</url-pattern>
    	</servlet-mapping>
     
     
    	<servlet>
    		<servlet-name>Captcha</servlet-name>
    		<display-name>Captcha</display-name>
    		<servlet-class>nl.captcha.servlet.CaptchaServlet</servlet-class>
     
    		<!--border around captcha. Legal values are yes or no. Defaults to yes Other values will be ignored-->
    		<init-param>
    			<param-name>cap.border</param-name>
    			<param-value>yes</param-value>
    		</init-param>
    		<!--color of the border. Legal values are r,g,b(and optional alpaha) or white,black,etc. Other values will be ignored-->
    		<init-param>
    			<param-name>cap.border.c</param-name>
    			<param-value>black</param-value>
    		</init-param>
    		<!--thickness of the border around captcha. Legal values are > 0 other values will be ignored. Defaults to one-->
    		<init-param>
    			<param-name>cap.border.th</param-name>
    			<param-value>1</param-value>
    		</init-param>
     
    		<init-param>
    			<param-name>cap.obscurificator</param-name>
    			<!--<param-value>nl.captcha.obscurity.imp.ShadowGimpyImp</param-value>-->
    			<!--<param-value>nl.captcha.obscurity.imp.WaterRiple</param-value>-->
    			<param-value>nl.captcha.sandbox.TestGimpyImp</param-value>
    		</init-param>
     
    		<init-param>
    			<param-name>cap.cap.producer</param-name>
    			<param-value>nl.captcha.obscurity.DefaultCaptchaIml</param-value>
    		</init-param>
     
     
    		<init-param>
    			<param-name>cap.word.imp</param-name>
    			<param-value>nl.captcha.text.imp.DefaultWordRenderer</param-value>
    		</init-param>
     
    		<init-param>
    			<param-name>cap.text.producer</param-name>
    			<!--<param-value>nl.captcha.sandbox.ChineseTextProducer</param-value>-->
    			<param-value>nl.captcha.text.imp.FiveLetterFirstNameTextCreator</param-value>
    		</init-param>
    		<init-param>
    			<param-name>cap.char.arr</param-name>
    			<param-value>2,3,4,6,7,8,a,b,c,d,e,f,g,h,k,o,r,s,t,x,y</param-value>
    		</init-param>
    		<!--if default captcha producer is slelected this value represents the amount of chars that are supplied in the captcha. values below 2 are not exepted when omitted defaults to 5-->
    		<init-param>
    			<param-name>cap.char.arr.l</param-name>
    			<param-value>6</param-value>
    		</init-param>
    		<init-param>
    			<param-name>cap.font.arr</param-name>
     
    			<param-value>Arial,Helvetica,Courier,TimesRoman</param-value>
     
    		</init-param>
    		<init-param>
    			<param-name>cap.font.size</param-name>
    			<param-value>40</param-value>
    		</init-param>
     
    		<init-param>
    			<param-name>cap.font.color</param-name>
    			<param-value>166,31,255</param-value>
    		</init-param>
     
     
    		<init-param>
    			<param-name>cap.bg.imp</param-name>
    			<param-value>nl.captcha.obscurity.imp.DefaultBackgroundImp</param-value>
    		</init-param>
     
    		<!--values can be either red,yellow,blue,cya etc or r,g,b,alpha value-->
     
    		<init-param>
    			<param-name>cap.background.c.to</param-name>
    			<param-value>255,255,255</param-value>
    		</init-param>
     
    		<init-param>
    			<param-name>cap.background.c.from</param-name>
    			<param-value>75,75,255</param-value>
    		</init-param>
     
     
    		<init-param>
    			<param-name>cap.noise.imp</param-name>
    			<param-value>nl.captcha.obscurity.imp.DefaultNoiseImp</param-value>
    		</init-param>
     
    		<init-param>
    			<param-name>cap.noise.c</param-name>
    			<param-value>166,31,255</param-value>
    		</init-param>
     
    	</servlet>
     
    	<servlet-mapping>
    		<servlet-name>Captcha</servlet-name>
    		<url-pattern>/Captcha.jpg</url-pattern>
    	</servlet-mapping>
    Ca ne fonctionne pas !!

    Et meme sans le controller.xml mais rien qu'avec la FTL :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    input type="text" name="imageText" maxlength="4"/>
     
    <img src="Captcha.jpg">
     
    <%
            String imageText = (String)session.getAttribute(nl.captcha.servlet.Constants.SIMPLE_CAPCHA_SESSION_KEY);
     
    %>
    Et le même web.xml, ca ne fonctionne pas !!
    Si quelqu'un a une idée, je suis preneur !
    MErci

  6. #6
    Membre confirmé Avatar de silverfab34
    Inscrit en
    Mars 2006
    Messages
    203
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Mars 2006
    Messages : 203
    Par défaut
    Bon, voici ma solution :
    alors tout d'abord, j'ajoute la lib jcaptcha-all-1.0-RC6.jar.

    J'ajoute dans mon controller :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    <request-map uri="jcaptcha">
    	    <security https="false" auth="false"/>
          	<event type="java" path="com.myAppli.captcha.ImageCaptchaServlet" invoke="doGet"/>
     		<response name="success" type="view" value="jcaptcha"/>
    </request-map>
     
    <view-map name="jcaptcha" page="/jcaptcha"/>
    Ensuite, je créais les classes :
    ImageCaptchaServlet

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
     
    public class ImageCaptchaServlet implements Serializable {
     
       	public static final String module = ImageCaptchaServlet.class.getName();
     
     
        public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
            doGet(httpServletRequest, httpServletResponse);
        }
     
     
        public static String doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
     
           byte[] captchaChallengeAsJpeg = null;
           // the output stream to render the captcha image as jpeg into
            ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
            try {
     
            // get the session id that will identify the generated captcha.
            //the same id must be used to validate the response, the session id is a good candidate!
            String captchaId = httpServletRequest.getSession().getId();
     
            // call the ImageCaptchaService getChallenge method
                BufferedImage challenge =
                        CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,
                                httpServletRequest.getLocale());
     
                // a jpeg encoder
                JPEGImageEncoder jpegEncoder =
                        JPEGCodec.createJPEGEncoder(jpegOutputStream);
                jpegEncoder.encode(challenge);
            } catch (IllegalArgumentException e) {
                httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
                return "error";
            } catch (CaptchaServiceException e) {
                httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return "error";
            }
     
            captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
     
            // flush it in the response
            httpServletResponse.setHeader("Cache-Control", "no-store");
            httpServletResponse.setHeader("Pragma", "no-cache");
            httpServletResponse.setDateHeader("Expires", 0);
            httpServletResponse.setContentType("image/jpeg");
            ServletOutputStream responseOutputStream =
                    httpServletResponse.getOutputStream();
            responseOutputStream.write(captchaChallengeAsJpeg);
            responseOutputStream.flush();
            responseOutputStream.close();
            return "success";
        }
    }
    et CaptchaServiceSingleton

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    public class CaptchaServiceSingleton {
     
        private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();
     
        public static ImageCaptchaService getInstance(){
            return instance;
        }
    }
    dans ma FTL :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    <script type="text/Javascript">
    function update_img() {
                        theImage = new Image;
                    theImage.src = "/control/jcaptcha?"+(new Date()).getTime();
                    document.getElementById("imageCaptcha").src = theImage.src;
                    }
    </script>
     
    <form name="fTellAFriend" id="fTellAFriend" action="/emailFriend" onload="javascript:update_img()">
    Your code : <input type="text" name="imageText" maxlength="10"/>
    <img id="imageCaptcha" name="imageCaptcha" src="/jcaptcha" >
    </form>
    La fonction update_img() permet de recharger l'image Captcha à chaque chargement du formulaire


    Et dans mon action :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    Boolean isResponseCorrect =Boolean.FALSE;
    		//remenber that we need an id to validate!
    		String captchaId = request.getSession().getId();
    		//retrieve the response
    		String responseCaptcha= request.getParameter("imageText");
     
    		boolean reponseValue = false;
    		// Call the Service method
    		try {
    			isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,responseCaptcha);
    			if (isResponseCorrect.equals(Boolean.TRUE)){
    				reponseValue = true;
     
    			}else{
    				reponseValue = false;
    			}
    		} catch (CaptchaServiceException e) {
    			//should not happen, may be thrown if the id is not valid
    			Debug.logError(e, "CaptchaServiceException", module);
    		}
    Voilouuu !!!
    Si vous avez des questions !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. aide formulaire + captcha
    Par lilalilaloum dans le forum Langage
    Réponses: 1
    Dernier message: 04/02/2009, 17h54
  2. Formulaire CAPTCHA Bdd
    Par pod1978 dans le forum Langage
    Réponses: 3
    Dernier message: 14/10/2007, 18h47
  3. Formulaire d'inscription et captcha.
    Par fabreizhad dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 13/03/2007, 21h52
  4. Formulaire d'inscription et captcha.
    Par fabreizhad dans le forum Langage
    Réponses: 1
    Dernier message: 11/03/2007, 22h54
  5. Réponses: 4
    Dernier message: 28/07/2004, 10h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo