zoukankan      html  css  js  c++  java
  • 注册页面 注册码【欠缺较多 待完善】

    【欠缺较多 待完善】

    【web.xml】

    <servlet>
    <servlet-name>captchaServlet</servlet-name>
    <servlet-class>com.jspxcms.common.captcha.CaptchaServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>captchaServlet</servlet-name>
    <url-pattern>/captcha.servlet</url-pattern>
    </servlet-mapping>

    【验证码工具类】

    package com.jspxcms.common.captcha;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.apache.commons.lang3.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.octo.captcha.service.CaptchaService;
    import com.octo.captcha.service.CaptchaServiceException;
    
    /**
     * 验证码工具类
     * 
     * @author liufang
     * 
     */
    public abstract class Captchas {
        protected static final Logger logger = LoggerFactory
                .getLogger(Captchas.class);
    
        public static boolean isValid(CaptchaService service,
                HttpServletRequest request, String captcha) {
            HttpSession session = request.getSession(false);
            if (session == null) {
                return false;
            }
            try {
                return service.validateResponseForID(session.getId(), captcha);
            } catch (CaptchaServiceException e) {
                logger.warn("captcha exception", e);
                return false;
            }
        }
    
        public static boolean isValidTry(MyImageCaptchaService service,
                HttpServletRequest request, String captcha) {
            return isValidTry(service, request, captcha, false);
        }
    
        public static boolean isValidTry(MyImageCaptchaService service,
                HttpServletRequest request, String captcha, boolean removeOnError) {
            if (StringUtils.isBlank(captcha)) {
                return false;
            }
            HttpSession session = request.getSession(false);
            if (session == null) {
                return false;
            }
            try {
                return service.tryResponseForID(session.getId(), captcha,
                        removeOnError);
            } catch (CaptchaServiceException e) {
                logger.warn("captcha exception", e);
                return false;
            }
        }
    }

    【验证码servlet】

    package com.jspxcms.common.captcha;
    
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.Locale;
    
    import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    import com.jspxcms.common.web.Servlets;
    
    /**
     * 验证码Servlet
     * 
     * @author liufang
     * 
     */
    public class CaptchaServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        public static final String DEFAULT_CAPTCHA_SERVICE_ID = "captchaService";
        public static final String CAPTCHA_SERVICE_ID = "captchaServiceId";
        private MyImageCaptchaService captchaService;
    
        @Override
        public void init() throws ServletException {
            String captchaServiceId = getInitParameter(CAPTCHA_SERVICE_ID);
            if (StringUtils.isBlank(captchaServiceId)) {
                captchaServiceId = DEFAULT_CAPTCHA_SERVICE_ID;
            }
            ApplicationContext context = WebApplicationContextUtils
                    .getWebApplicationContext(getServletContext());
            captchaService = context.getBean(captchaServiceId,
                    MyImageCaptchaService.class);
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            String captcha = req.getParameter("captcha");
            if (captcha == null) {
                challenge(req, resp);
            } else {
                tryResponse(captcha, req, resp);
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            doGet(req, resp);
        }
    
        private void challenge(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            // Set to expire far in the past.
            resp.setDateHeader("Expires", 0);
            // Set standard HTTP/1.1 no-cache headers.
            resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
            resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
            // Set standard HTTP/1.0 no-cache header.
            resp.setHeader("Pragma", "no-cache");
            // return a jpeg
            resp.setContentType("image/jpeg");
    
            ServletOutputStream out = resp.getOutputStream();
    
            try {
                String captchaId = req.getSession(true).getId();
                // create the image with the text
                BufferedImage challenge = captchaService.getImageChallengeForID(
                        captchaId, Locale.ENGLISH);
                // write the data out
                ImageIO.write(challenge, "jpg", out);
                out.flush();
            } finally {
                out.close();
            }
        }
    
        private void tryResponse(String captcha, HttpServletRequest req,
                HttpServletResponse resp) {
            boolean removeOnError = "true"
                    .equals(req.getParameter("removeOnError"));
            if (Captchas.isValidTry(captchaService, req, captcha, removeOnError)) {
                Servlets.writeHtml(resp, "true");
            } else {
                Servlets.writeHtml(resp, "false");
            }
        }
    }

    【验证码页面】

                        <div class="labelbox">
                            <span>验证码:</span>
                            <p>
                                <input type="text" id="captcha" name="captcha"  data-rule-required="true" data-rule-remote='{"url":"${ctx}/captcha.servlet","type":"post"}' data-msg-remote="验证码错误" autocomplete="off" value=""/><em>请填写验证码</em>
                                <img src="${ctx}/captcha.servlet" onclick="this.src='${ctx}/captcha.servlet?d='+new Date()*1;$('#captcha').focus();" style="cursor:pointer;border:1px solid #ccc;margin: 4px 0 0 7px;vertical-align:top;" title="点击重新获取验证码"/>
                            </p>
                        </div>
  • 相关阅读:
    并不对劲的网络流
    并不对劲的[noi2006]网络收费
    并不对劲的spoj1812
    48.孩子们的游戏(圆圈中最后剩下的数)
    47.扑克牌顺子
    46.翻转单词顺序
    45.左旋转字符串
    44.和为S的两个数字
    43.和为S的连续正数序列
    42.数组中只出现一次的数字
  • 原文地址:https://www.cnblogs.com/dixinyunpan/p/5853956.html
Copyright © 2011-2022 走看看