Пример #1
0
 /**
  * Creates a {@link Captcha} and stores it in the session.
  *
  * @param width the width of the image
  * @param height the height of the image
  * @return {@link BufferedImage} containing the captcha image
  */
 public BufferedImage createCaptcha(int width, int height) {
   Captcha captcha =
       new Captcha.Builder(width, height)
           .addText()
           .addBackground(new GradiatedBackgroundProducer())
           .gimp()
           .addNoise()
           .addBorder()
           .build();
   session.setAttribute(Captcha.NAME, captcha);
   return captcha.getImage();
 }
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    ColoredEdgesWordRenderer wordRenderer = new ColoredEdgesWordRenderer(COLORS, FONTS);
    Captcha captcha =
        new Captcha.Builder(_width, _height)
            .addText(wordRenderer)
            .gimp()
            .addNoise()
            .addBackground(new GradiatedBackgroundProducer())
            .build();

    req.getSession().setAttribute(Captcha.NAME, captcha);
    CaptchaServletUtil.writeImage(resp, captcha.getImage());
  }
 @Override
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   //        ColoredEdgesWordRenderer wordRenderer = new ColoredEdgesWordRenderer(COLORS, FONTS);
   if (req.getParameter("_width") != null) {
     _width = Integer.parseInt(req.getParameter("_width"));
     if (_width > 200) _width = 150;
   }
   if (req.getParameter("_height") != null) {
     _height = Integer.parseInt(req.getParameter("_height"));
     if (_height > 150) _height = 70;
   }
   Captcha captcha =
       new Captcha.Builder(_width, _height)
           .addText(new DefaultTextProducer(), new DefaultWordRenderer(COLORS, FONTS))
           //                .gimp()
           .addNoise(new StraightLineNoiseProducer(Color.PINK, 2))
           //                .addBackground(new GradiatedBackgroundProducer())
           .build();
   req.getSession().setAttribute(NAME, captcha);
   CaptchaServletUtil.writeImage(resp, captcha.getImage());
 }
Пример #4
0
  public String login() {
    StringBuffer msg = new StringBuffer("{msg:'");

    SysConfig sysConfig = this.sysConfigService.findByKey("codeConfig");

    Captcha captcha = (Captcha) getSession().getAttribute("simpleCaptcha");
    Boolean login = Boolean.valueOf(false);

    String newPassword = null;

    if ((!"".equals(this.username)) && (this.username != null)) {
      setUser(this.userService.findByUserName(this.username));

      if (this.user != null) {
        if (org.apache.commons.lang.StringUtils.isNotEmpty(this.password)) {
          newPassword = StringUtil.encryptSha256(this.password);
          if (this.user.getPassword().equalsIgnoreCase(newPassword)) {
            if (sysConfig.getDataValue().equals("1")) {
              if (captcha == null) {
                msg.append("请刷新验证码再登录.'");
              } else if (captcha.isCorrect(this.checkCode)) {
                if (this.user.getStatus().shortValue() == 1) login = Boolean.valueOf(true);
                else msg.append("此用户已被禁用.'");
              } else msg.append("验证码不正确.'");

            } else if (this.user.getStatus().shortValue() == 1) login = Boolean.valueOf(true);
            else msg.append("此用户已被禁用.'");
          } else msg.append("密码不正确.'");
        } else {
          msg.append("密码不能为空.'");
        }
      } else msg.append("用户不存在.'");
    }
    if (login.booleanValue()) {
      UsernamePasswordAuthenticationToken authRequest =
          new UsernamePasswordAuthenticationToken(this.username, this.password);
      SecurityContext securityContext = SecurityContextHolder.getContext();
      securityContext.setAuthentication(this.authenticationManager.authenticate(authRequest));
      SecurityContextHolder.setContext(securityContext);
      getSession().setAttribute("SPRING_SECURITY_LAST_USERNAME", this.username);
      String rememberMe = getRequest().getParameter("_spring_security_remember_me");
      if ((rememberMe != null) && (rememberMe.equals("on"))) {
        long tokenValiditySeconds = 1209600L;
        long tokenExpiryTime = System.currentTimeMillis() + tokenValiditySeconds * 1000L;

        String signatureValue =
            DigestUtils.md5Hex(
                this.username
                    + ":"
                    + tokenExpiryTime
                    + ":"
                    + this.user.getPassword()
                    + ":"
                    + this.key);

        String tokenValue = this.username + ":" + tokenExpiryTime + ":" + signatureValue;
        String tokenValueBase64 = new String(Base64.encodeBase64(tokenValue.getBytes()));
        getResponse().addCookie(makeValidCookie(tokenExpiryTime, tokenValueBase64));
      }

      setJsonString("{success:true}");
    } else {
      msg.append(",failure:true}");
      setJsonString(msg.toString());
      return "login";
    }
    return "index";
  }
Пример #5
0
 /**
  * Validates a captcha answer. The same captcha can be validated multiple times.
  *
  * @param captchaAnswer the String to validate
  * @return boolean indicating if the answer is correct
  * @throws CaptchaException if no captcha found to validate
  */
 public boolean validateCaptcha(String captchaAnswer) throws CaptchaException {
   Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
   if (captcha == null) throw new CaptchaException("no captcha to validate");
   return captcha.isCorrect(captchaAnswer);
 }
Пример #6
0
  /** 因为获取图片只会有get方法 */
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    Builder builder = new Builder(_width, _height);
    // 增加边框
    builder.addBorder();
    // 是否增加干扰线条
    if (_noise == true) builder.addNoise();
    // ----------------自定义字体大小-----------
    // 自定义设置字体颜色和大小 最简单的效果 多种字体随机显示
    List<Font> fontList = new ArrayList<Font>();
    // fontList.add(new Font("Arial", Font.HANGING_BASELINE,
    // 40));//可以设置斜体之类的
    fontList.add(new Font("Courier", Font.BOLD, 60));
    DefaultWordRenderer dwr = new DefaultWordRenderer(Color.green, fontList);

    // 加入多种颜色后会随机显示 字体空心
    // List<Color> colorList=new ArrayList<Color>();
    // colorList.add(Color.green);
    // colorList.add(Color.white);
    // colorList.add(Color.blue);
    // ColoredEdgesWordRenderer cwr= new
    // ColoredEdgesWordRenderer(colorList,fontList);

    WordRenderer wr = dwr;
    // 增加文本,默认为5个随机字符.
    if (_text == null) {
      builder.addText();
    } else {
      String[] ts = _text.split(",");
      for (int i = 0; i < ts.length; i++) {
        String[] ts1 = ts[i].split(":");
        if ("chinese".equals(ts1[0])) {
          builder.addText(new ChineseTextProducer(Integer.parseInt(ts1[1])), wr);
        } else if ("number".equals(ts1[0])) {
          // 这里没有0和1是为了避免歧义 和字母I和O
          char[] numberChar = new char[] {'2', '3', '4', '5', '6', '7', '8'};
          builder.addText(new DefaultTextProducer(Integer.parseInt(ts1[1]), numberChar), wr);
        } else if ("word".equals(ts1[0])) {
          // 原理同上
          char[] numberChar =
              new char[] {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'k', 'm', 'n', 'p', 'r', 'w', 'x', 'y'
              };
          builder.addText(new DefaultTextProducer(Integer.parseInt(ts1[1]), numberChar), wr);
        } else {
          builder.addText(new DefaultTextProducer(Integer.parseInt(ts1[1])), wr);
        }
      }
    }

    // --------------添加背景-------------
    // 设置背景渐进效果 以及颜色 form为开始颜色,to为结束颜色
    GradiatedBackgroundProducer gbp = new GradiatedBackgroundProducer();
    gbp.setFromColor(Color.yellow);
    gbp.setToColor(Color.red);
    // 无渐进效果,只是填充背景颜色
    // FlatColorBackgroundProducer fbp=new
    // FlatColorBackgroundProducer(Color.red);
    // 加入网纹--一般不会用
    // SquigglesBackgroundProducer sbp=new SquigglesBackgroundProducer();
    // 没发现有什么用,可能就是默认的
    // TransparentBackgroundProducer tbp = new
    // TransparentBackgroundProducer();

    builder.addBackground(gbp);
    // ---------装饰字体---------------
    // 字体边框齿轮效果 默认是3
    builder.gimp(new BlockGimpyRenderer(1));
    // 波纹渲染 相当于加粗
    // builder.gimp(new RippleGimpyRenderer());
    // 修剪--一般不会用
    // builder.gimp(new ShearGimpyRenderer(Color.red));
    // 加网--第一个参数是横线颜色,第二个参数是竖线颜色
    // builder.gimp(new FishEyeGimpyRenderer(Color.red,Color.yellow));
    // 加入阴影效果 默认3,75
    // builder.gimp(new DropShadowGimpyRenderer());
    // 创建对象
    Captcha captcha = builder.build();

    CaptchaServletUtil.writeImage(resp, captcha.getImage());
    req.getSession().setAttribute(NAME, captcha);
  }