本文整理汇总了Java中nl.captcha.Captcha类的典型用法代码示例。如果您正苦于以下问题:Java Captcha类的具体用法?Java Captcha怎么用?Java Captcha使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Captcha类属于nl.captcha包,在下文中一共展示了Captcha类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCaptchaImage
import nl.captcha.Captcha; //导入依赖的package包/类
private void getCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws IOException
{
String seed = request.getParameter("seed");
if (seed == null || seed.isEmpty())
{
response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
return;
}
int seed_;
try
{
seed_ = Integer.parseInt(seed);
}
catch (NumberFormatException i)
{
response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
return;
}
PredictableCaptchaTextProducer producer = new PredictableCaptchaTextProducer(seed_);
final Captcha captcha = new Captcha.Builder(160, 50).addText(producer).addBackground(new GradiatedBackgroundProducer()).addNoise().gimp(new FishEyeGimpyRenderer()).addBorder().build();
CaptchaServletUtil.writeImage(response, captcha.getImage());
}
示例2: doGet
import nl.captcha.Captcha; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
List<java.awt.Font> textFonts = Arrays.asList(
new Font("Arial", Font.BOLD, 40),
new Font("Courier", Font.BOLD, 40));
Captcha captcha = new Captcha.Builder(250, 90).addText()
.addBackground(new FlatColorBackgroundProducer(Color.YELLOW))
.gimp(new FishEyeGimpyRenderer())
.addNoise(new CurvedLineNoiseProducer())
.addText(new DefaultTextProducer(5),
new DefaultWordRenderer(Color.RED, textFonts))
.build();
CaptchaServletUtil.writeImage(resp, captcha.getImage());
req.getSession().setAttribute(NAME, captcha);
}
示例3: testShouldExpire
import nl.captcha.Captcha; //导入依赖的package包/类
@Test
public void testShouldExpire() {
Captcha captcha = new Captcha.Builder(200, 50)
.addText()
.build();
assertFalse(StickyCaptchaServlet.shouldExpire(captcha));
StickyCaptchaServlet.setTtl(0);
assertTrue(StickyCaptchaServlet.shouldExpire(captcha));
assertEquals(0, StickyCaptchaServlet.getTtl());
StickyCaptchaServlet.setTtl(-1000);
assertEquals(0, StickyCaptchaServlet.getTtl());
StickyCaptchaServlet.setTtl(Long.MAX_VALUE);
assertFalse(StickyCaptchaServlet.shouldExpire(captcha));
}
示例4: isValid
import nl.captcha.Captcha; //导入依赖的package包/类
public boolean isValid(HttpSession session) {
Captcha captchaValue = (Captcha) session.getAttribute(Captcha.NAME);
session.removeAttribute(Captcha.NAME);
if (captchaValue == null || captcha == null ||
!captchaValue.isCorrect(captcha)) {
errors.put("captchaError", "Text did not match image");
}
if (!StringUtils.hasText(getNameFirst())) {
errors.put("nameFirstError", "Required field");
}
if (!StringUtils.hasText(getNameLast())) {
errors.put("nameLastError", "Required field");
}
if (!StringUtils.hasText(getEmail())) {
errors.put("emailError", "Required field");
}
if (!StringUtils.hasText(getDescription())) {
errors.put("descriptionError", "Required field");
}
return (!hasErrors());
}
示例5: doGet
import nl.captcha.Captcha; //导入依赖的package包/类
@Override
public void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException,
IOException {
final HttpSession session = req.getSession();
final Captcha captcha = new Captcha.Builder(_width, _height)
.addText()
.addBackground(new GradiatedBackgroundProducer())
.gimp()
.addBackground(new FlatColorBackgroundProducer(new Color(238, 238, 238)))
.addBorder()
.addNoise()
.build();
session.setAttribute(NAME, captcha);
CaptchaServletUtil.writeImage(resp, captcha.getImage());
}
示例6: updateCaptcha
import nl.captcha.Captcha; //导入依赖的package包/类
@At("/captcha/update")
@Ok("raw:image/png")
public BufferedImage updateCaptcha(HttpSession session, @Param("w") int w, @Param("h") int h) {
if (w * h == 0) { //长或宽为0?重置为默认长宽.
w = 200;
h = 60;
}
Captcha captcha = new Captcha.Builder(w, h)
.addText().addBackground(new GradiatedBackgroundProducer())
// .addNoise(new StraightLineNoiseProducer()).addBorder()
.gimp(new FishEyeGimpyRenderer())
.build();
String text = captcha.getAnswer();
session.setAttribute(Toolkit.captcha_attr, text);
return captcha.getImage();
}
示例7: doGet
import nl.captcha.Captcha; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//ColoredEdgesWordRenderer wordRenderer = new ColoredEdgesWordRenderer(COLORS, FONTS);
ColoredEdgesWordRenderer wordRenderer = new ColoredEdgesWordRenderer(COLORS, FONTS, 1f);
Captcha captcha = new Captcha.Builder(_width, _height).addText(wordRenderer)
//.gimp()
.addNoise()
//.addBackground(new GradiatedBackgroundProducer())
//.addBackground(new TransparentBackgroundProducer())
//.addBackground(new FlatColorBackgroundProducer(Color.lightGray))
.addBackground(new GradiatedBackgroundProducer(Color.lightGray, Color.white))
.build();
CaptchaServletUtil.writeImage(resp, captcha.getImage());
req.getSession().setAttribute(NAME, captcha);
}
示例8: validateSimpleCaptcha
import nl.captcha.Captcha; //导入依赖的package包/类
public static boolean validateSimpleCaptcha( HttpSession session, String answer) {
Captcha captcha = (Captcha) session.getAttribute( Captcha.NAME );
String code = captcha.getAnswer();
if( code.equals(answer) ) {
return true;
}
return false;
}
示例9: doGet
import nl.captcha.Captcha; //导入依赖的package包/类
@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();
CaptchaServletUtil.writeImage(resp, captcha.getImage());
req.getSession().setAttribute(NAME, captcha);
}
示例10: buildAndSetCaptcha
import nl.captcha.Captcha; //导入依赖的package包/类
private Captcha buildAndSetCaptcha(HttpSession session) {
Captcha captcha = new Captcha.Builder(_width, _height)
.addText()
.gimp()
.addBorder()
.addNoise()
.addBackground()
.build();
session.setAttribute(NAME, captcha);
return captcha;
}
示例11: shouldExpire
import nl.captcha.Captcha; //导入依赖的package包/类
static boolean shouldExpire(Captcha captcha) {
long ts = captcha.getTimeStamp().getTime();
long now = new Date().getTime();
long diff = now - ts;
return diff >= _ttl;
}
示例12: isValid
import nl.captcha.Captcha; //导入依赖的package包/类
/**
* Gets the valid attribute of the UserBean object
*
* @param session the browser sessions
* @return The valid value
*/
public boolean isValid(HttpSession session) {
Captcha captchaValue = (Captcha) session.getAttribute(Captcha.NAME);
session.removeAttribute(Captcha.NAME);
if (captchaValue == null || captcha == null ||
!captchaValue.isCorrect(captcha)) {
errors.put("captchaError", "Text did not match image");
}
if (!StringUtils.hasText(nameFirst)) {
errors.put("nameFirstError", "Required field");
}
if (!StringUtils.hasText(nameLast)) {
errors.put("nameLastError", "Required field");
}
if (!StringUtils.hasText(companyName)) {
errors.put("companyNameError", "Required field");
}
if (!StringUtils.hasText(email)) {
errors.put("emailError", "Required field");
}
if (!StringUtils.hasText(phone)) {
errors.put("phoneError", "Required field");
}
if (!StringUtils.hasText(addressLine1)) {
errors.put("addressLine1Error", "Required field");
}
if (!StringUtils.hasText(getCity())) {
errors.put("cityError", "Required field");
}
if (getState() == null || getState().equals("-1")) {
errors.put("stateError", "Required field");
}
if (getCountry() == null || getCountry().equals("-1")) {
errors.put("countryError", "Required field");
}
if (!agreement) {
errors.put("agreementError", "Accepting the terms and conditions is required");
}
return (!hasErrors());
}
示例13: validateCaptcha
import nl.captcha.Captcha; //导入依赖的package包/类
private String validateCaptcha(HttpServletRequest request,
String returnIncompleteState) throws Exception {
Captcha captcha = (Captcha) request.getSession().getAttribute(Captcha.NAME);
if (captcha == null) {
captcha = new Captcha.Builder(200, 50).addText().addBackground()
// .addNoise()
.gimp()
// .addBorder()
.build();
request.getSession().setAttribute(Captcha.NAME, captcha);
}
// Do this so we can capture non-Latin chars
request.setCharacterEncoding("UTF-8");
String answer = HTTPUtils.cleanXSS((String) request.getParameter("answer"));
if (answer == null || answer.length() == 0) {
throw new NoReloadException(
"Please enter the characters appearing in the image. ");
}
request.getSession().removeAttribute("reload");
if (!captcha.isCorrect(answer)) {
throw new InvalidCaptChaInputException(
"WARNING: The string you entered does not match"
+ " with what is shown in the image. Please try again.");
}
request.getSession().removeAttribute(Captcha.NAME);
return null;
}
示例14: validateCaptcha
import nl.captcha.Captcha; //导入依赖的package包/类
private String validateCaptcha(HttpServletRequest request,
String returnIncompleteState) throws Exception {
Captcha captcha = (Captcha) request.getSession().getAttribute(Captcha.NAME);
if (captcha == null) {
captcha = new Captcha.Builder(200, 50).addText().addBackground()
// .addNoise()
.gimp()
// .addBorder()
.build();
request.getSession().setAttribute(Captcha.NAME, captcha);
}
// Do this so we can capture non-Latin chars
request.setCharacterEncoding("UTF-8");
String answer = HTTPUtils.cleanXSS((String) request.getParameter("answer"));
if (answer == null || answer.length() == 0) {
throw new NoReloadException(
"Please enter the characters appearing in the image. ");
}
request.getSession().removeAttribute("reload");
if (!captcha.isCorrect(answer)) {
throw new InvalidCaptChaInputException(
"WARNING: The string you entered does not match"
+ " with what is shown in the image. Please try again.");
} else {
} request.getSession().removeAttribute(Captcha.NAME);
return null;
}
示例15: createCaptcha
import nl.captcha.Captcha; //导入依赖的package包/类
/**
* 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();
}