Package org.ametys.core.captcha
Class CaptchaHelper
- java.lang.Object
-
- org.ametys.core.captcha.CaptchaHelper
-
- All Implemented Interfaces:
Serviceable
public final class CaptchaHelper extends Object implements Serviceable
Helper for generating image captcha to PNG format
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
CaptchaHelper.CaptchaType
(package private) static class
CaptchaHelper.ValidableCaptcha
Bean for a captcha and a validity date
-
Field Summary
Fields Modifier and Type Field Description private static JSONUtils
_jsonUtils
private static org.slf4j.Logger
_logger
private static Map<String,CaptchaHelper.ValidableCaptcha>
_mapDynamicCaptcha
private static Map<String,List<CaptchaHelper.ValidableCaptcha>>
_mapStaticCaptcha
private static String
CAPTCHA_TYPE_KEY
private static String
DYNAMIC_PREFIX_KEY
private static String
RECAPTCHA_SECRET_KEY
private static String
STATIC_PREFIX_KEY
-
Constructor Summary
Constructors Constructor Description CaptchaHelper()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
_cleanOldDynamicCaptchas()
private static void
_cleanOldStaticCaptchas()
private static Captcha
_generateImageCaptcha(Integer color, boolean addNoise, boolean fisheye, int width, int height)
static boolean
checkAndInvalidate(String key, String value)
Check a captchastatic boolean
checkAndInvalidateJCaptcha(String key, String value)
Check a captchastatic boolean
checkAndInvalidateReCaptcha(String value)
Check a ReCaptcha valuestatic void
cleanOldCaptchas()
Clean the outdated captchasstatic BufferedImage
generateImageCaptcha(String key)
Generate an image captcha to PNG format.static BufferedImage
generateImageCaptcha(String key, boolean addNoise, boolean fisheye)
Generate an image captcha to PNG format.static BufferedImage
generateImageCaptcha(String key, Integer color)
Generate an image captcha to PNG format.static BufferedImage
generateImageCaptcha(String key, Integer color, boolean addNoise, boolean fisheye)
Generate an image captcha to PNG format.static BufferedImage
generateImageCaptcha(String key, Integer color, boolean addNoise, boolean fisheye, int width, int height)
Generate an image captcha to PNG format.static String
getCaptchaType()
Retrieve the type of captcha usedstatic void
removeCaptcha(String key)
Remove a captchavoid
service(ServiceManager manager)
-
-
-
Field Detail
-
CAPTCHA_TYPE_KEY
private static final String CAPTCHA_TYPE_KEY
- See Also:
- Constant Field Values
-
RECAPTCHA_SECRET_KEY
private static final String RECAPTCHA_SECRET_KEY
- See Also:
- Constant Field Values
-
STATIC_PREFIX_KEY
private static final String STATIC_PREFIX_KEY
- See Also:
- Constant Field Values
-
DYNAMIC_PREFIX_KEY
private static final String DYNAMIC_PREFIX_KEY
- See Also:
- Constant Field Values
-
_mapStaticCaptcha
private static Map<String,List<CaptchaHelper.ValidableCaptcha>> _mapStaticCaptcha
-
_mapDynamicCaptcha
private static Map<String,CaptchaHelper.ValidableCaptcha> _mapDynamicCaptcha
-
_jsonUtils
private static JSONUtils _jsonUtils
-
_logger
private static org.slf4j.Logger _logger
-
-
Constructor Detail
-
CaptchaHelper
public CaptchaHelper()
-
-
Method Detail
-
service
public void service(ServiceManager manager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
getCaptchaType
public static String getCaptchaType()
Retrieve the type of captcha used- Returns:
- The type of captcha.
-
checkAndInvalidate
public static boolean checkAndInvalidate(String key, String value)
Check a captcha- Parameters:
key
- The captcha key. Can be empty or null when using reCaptcha.value
- The value to check- Returns:
true
if the captcha is valid, false otherwise.
-
checkAndInvalidateJCaptcha
public static boolean checkAndInvalidateJCaptcha(String key, String value)
Check a captcha- Parameters:
key
- The keyvalue
- The value to check- Returns:
- The image captcha
-
checkAndInvalidateReCaptcha
public static boolean checkAndInvalidateReCaptcha(String value)
Check a ReCaptcha value- Parameters:
value
- The value to check- Returns:
- True if the captcha is valid.
-
removeCaptcha
public static void removeCaptcha(String key)
Remove a captcha- Parameters:
key
- the key value
-
cleanOldCaptchas
public static void cleanOldCaptchas()
Clean the outdated captchas
-
_cleanOldDynamicCaptchas
private static void _cleanOldDynamicCaptchas()
-
_cleanOldStaticCaptchas
private static void _cleanOldStaticCaptchas()
-
generateImageCaptcha
public static BufferedImage generateImageCaptcha(String key)
Generate an image captcha to PNG format. The key has to be unique. If you can not give a unique id use generateImageCaptch without the key argument : but this is less secure.- Parameters:
key
- the wanted key. Can be not null. MUST START with "STATIC-" or "DYNAMIC-". If the key starts with 'STATIC-' this key may be used several times (e.g. for a cached page with a unique id for several display), if the key starts with 'DYNAMIC-' the key will unique (removing an existing captcha with the same key).- Returns:
- The corresponding image
-
generateImageCaptcha
public static BufferedImage generateImageCaptcha(String key, boolean addNoise, boolean fisheye)
Generate an image captcha to PNG format. The key has to be unique. If you can not give a unique id use generateImageCaptch without the key argument : but this is less secure.- Parameters:
key
- the wanted key. Can be not null. MUST START with "STATIC-" or "DYNAMIC-". If the key starts with 'STATIC-' this key may be used several times (e.g. for a cached page with a unique id for several display), if the key starts with 'DYNAMIC-' the key will unique (removing an existing captcha with the same key).addNoise
- true to add noise to captcha imagefisheye
- true to add fish eye background to captcha image- Returns:
- The corresponding image
-
generateImageCaptcha
public static BufferedImage generateImageCaptcha(String key, Integer color)
Generate an image captcha to PNG format. The key has to be unique, if you cannot generate a key use the other form of the method.- Parameters:
key
- the wanted key. Can not be null. You can use RandomStringUtils.randomAlphanumeric(10) to generates onecolor
- The color for font- Returns:
- The corresponding image
-
generateImageCaptcha
public static BufferedImage generateImageCaptcha(String key, Integer color, boolean addNoise, boolean fisheye)
Generate an image captcha to PNG format. The key has to be unique, if you cannot generate a key use the other form of the method.- Parameters:
key
- the wanted key. Can not be null. You can use RandomStringUtils.randomAlphanumeric(10) to generates onecolor
- The color for fontaddNoise
- true to add noise to captcha imagefisheye
- true to add fish eye background to captcha image- Returns:
- The corresponding image
-
generateImageCaptcha
public static BufferedImage generateImageCaptcha(String key, Integer color, boolean addNoise, boolean fisheye, int width, int height)
Generate an image captcha to PNG format. The key has to be unique, if you cannot generate a key use the other form of the method.- Parameters:
key
- the wanted key. Can not be null. You can use RandomStringUtils.randomAlphanumeric(10) to generates onecolor
- The color for fontaddNoise
- true to add noise to captcha imagefisheye
- true to add fish eye background to captcha imagewidth
- The image widthheight
- The image height- Returns:
- The corresponding image
-
_generateImageCaptcha
private static Captcha _generateImageCaptcha(Integer color, boolean addNoise, boolean fisheye, int width, int height)
-
-