本文整理汇总了Java中net.tanesha.recaptcha.ReCaptcha类的典型用法代码示例。如果您正苦于以下问题:Java ReCaptcha类的具体用法?Java ReCaptcha怎么用?Java ReCaptcha使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ReCaptcha类属于net.tanesha.recaptcha包,在下文中一共展示了ReCaptcha类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createHtml
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Override
public String createHtml(HttpServletRequest request) {
if (captchaEnabled) {
if (StringUtils.isBlank(privateKey) || StringUtils.isBlank(publicKey)) {
return invalidConfigurationMessage;
}
boolean secure = request.isSecure();
ReCaptcha captcha;
if (secure) {
captcha = ReCaptchaFactory.newSecureReCaptcha(publicKey, privateKey, createNoScript);
} else {
captcha = ReCaptchaFactory.newReCaptcha(publicKey, privateKey, createNoScript);
}
return captcha.createRecaptchaHtml(null, null);
}
return "";
}
示例2: register
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Override
public String register(ModelMap model, HttpSession session) {
model.addAttribute("openSourceLinks",
new OpenSourceLink[]{
new OpenSourceLink("View this page's jsp code.", "https://github.com/KevinWorkman/StaticVoidGames/blob/master/StaticVoidGames/src/main/webapp/WEB-INF/jsp/register.jsp"),
new OpenSourceLink("View this page's server code.", "https://github.com/KevinWorkman/StaticVoidGames/blob/master/StaticVoidGames/src/main/java/com/StaticVoidGames/spring/controller/StaticVoidGamesController.java")
}
);
String recaptchaPublicKey = env.getProperty("recaptcha.publicKey");
String recaptchaPrivateKey = env.getProperty("recaptcha.privateKey");
ReCaptcha c = ReCaptchaFactory.newReCaptcha(recaptchaPublicKey, recaptchaPrivateKey, false);
model.addAttribute("recaptchaHtml", c.createRecaptchaHtml(null, null));
return "register";
}
示例3: validateTransientFieldsChecksPasswordsConfirmation
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Test
public void validateTransientFieldsChecksPasswordsConfirmation() throws Exception {
// Arrange
ReCaptcha mockCaptcha = mock(ReCaptcha.class);
ServletRequest mockRequest = mock(ServletRequest.class);
ReCaptchaResponse mockResponse = mock(ReCaptchaResponse.class);
when(mockCaptcha.checkAnswer(anyString(), anyString(), anyString())).thenReturn(mockResponse);
when(mockResponse.isValid()).thenReturn(true);
when(mockRequest.getRemoteAddr()).thenReturn("");
ExpertValidationRulesChecker checker = mock(ExpertValidationRulesChecker.class);
RegistrationControllerValidator target =
new RegistrationControllerValidator(checker, mockCaptcha);
JsonExpertBasic expert = mockJsonExpertBasic();
when(expert.getPassword()).thenReturn("abc123q");
when(expert.getPasswordConfirmation()).thenReturn("abc123Q");
// Act
target.validateTransientFields(expert, mockRequest);
// Assert
verify(checker).checkPasswordConfirmation(eq("abc123q"), eq("abc123Q"), anyListOf(String.class));
}
示例4: validateTransientFieldsVerifiesCaptchaCorrectly
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Test
public void validateTransientFieldsVerifiesCaptchaCorrectly() throws Exception {
// Arrange
ReCaptcha mockCaptcha = mock(ReCaptcha.class);
ServletRequest mockRequest = mock(ServletRequest.class);
ReCaptchaResponse mockResponse = mock(ReCaptchaResponse.class);
when(mockCaptcha.checkAnswer(anyString(), anyString(), anyString())).thenReturn(mockResponse);
when(mockResponse.isValid()).thenReturn(false);
when(mockRequest.getRemoteAddr()).thenReturn("expected address");
RegistrationControllerValidator target =
new RegistrationControllerValidator(mock(ExpertValidationRulesChecker.class), mockCaptcha);
JsonExpertBasic expert = mockJsonExpertBasic();
// Act
List<String> result = target.validateTransientFields(expert, mockRequest);
// Assert
assertThat(result).contains("Captcha incorrect.");
verify(mockCaptcha)
.checkAnswer("expected address", expert.getCaptchaChallenge(), expert.getCaptchaResponse());
}
示例5: isCaptchaValid
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
private boolean isCaptchaValid(SignUpCredentials creds, SignUpValidationResult result_out, String remoteAddress)
{
String captchaChallenge = creds.getCaptchaChallenge();
String captchaResponse = creds.get(E_SignUpCredentialType.CAPTCHA_RESPONSE);
ReCaptcha captcha = ReCaptchaFactory.newReCaptcha(m_publicRecaptchaKey, m_privateRecaptchaKey, false);
ReCaptchaResponse recaptchaResponse = captcha.checkAnswer(remoteAddress, captchaChallenge, captchaResponse);
if ( !recaptchaResponse.isValid())
{
result_out.setNoErrors(); // just make sure all errors are filled in for json writing...kinda hacky.
result_out.setError(E_SignUpCredentialType.CAPTCHA_RESPONSE, E_SignUpValidationError.CAPTCHA_INCORRECT);
return false;
}
return true;
}
示例6: recover
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
public ModelAndView recover(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
String usernameOrEmail = StringUtils.trimToNull(request.getParameter("usernameOrEmail"));
ReCaptcha captcha = ReCaptchaFactory.newSecureReCaptcha("6LcZ3OMSAAAAANkKMdFdaNopWu9iS03V-nLOuoiH",
"6LcZ3OMSAAAAAPaFg89mEzs-Ft0fIu7wxfKtkwmQ", false);
boolean showCaptcha = true;
if (usernameOrEmail != null) {
map.put("usernameOrEmail", usernameOrEmail);
User user = getUserByUsernameOrEmail(usernameOrEmail);
String challenge = request.getParameter("recaptcha_challenge_field");
String uresponse = request.getParameter("recaptcha_response_field");
ReCaptchaResponse captchaResponse = captcha.checkAnswer(request.getRemoteAddr(), challenge, uresponse);
if (!captchaResponse.isValid()) {
map.put("error", "recover.error.invalidcaptcha");
} else if (user == null) {
map.put("error", "recover.error.usernotfound");
} else if (user.getEmail() == null) {
map.put("error", "recover.error.noemail");
} else {
String password = RandomStringUtils.randomAlphanumeric(8);
if (emailPassword(password, user.getUsername(), user.getEmail())) {
map.put("sentTo", user.getEmail());
user.setLdapAuthenticated(false);
user.setPassword(password);
securityService.updateUser(user);
showCaptcha = false;
} else {
map.put("error", "recover.error.sendfailed");
}
}
}
if (showCaptcha) {
map.put("captcha", captcha.createRecaptchaHtml(null, null));
}
return new ModelAndView("recover", "model", map);
}
示例7: doGet
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String cacheKey = "page-contact";
String output = null;
Cache cache = CacheSingleton.getInstance().getCache();
if (cache.containsKey(cacheKey)) {
output = (String) cache.get(cacheKey);
} else {
ConfigManager configManager = ConfigManager.getInstance();
ReCaptcha captcha = ReCaptchaFactory
.newReCaptcha(
configManager
.getValue(ConfigManager.RECAPTCHA_PUBLIC_KEY),
configManager
.getValue(ConfigManager.RECAPTCHA_PRIVATE_KEY),
false);
String captchaScript = captcha.createRecaptchaHtml(
req.getParameter("error"), null);
VelocityContext context = prepareContext(req);
context.put(
"pageTitle",
"Contact form and information"
+ configManager
.getValue(ConfigManager.WEBSITE_TITLE_SUFFIX));
context.put("htmlCaptcha", captchaScript);
output = generateTemplate("contact.vm", context);
cache.put(cacheKey, output);
}
writeResponse(output, resp);
}
示例8: isValid
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Override
public boolean isValid(HttpServletRequest request) {
log.debug("ReCaptcha enabled: {}", captchaEnabled);
if (!captchaEnabled) {
return true;
}
if (StringUtils.isBlank(privateKey) || StringUtils.isBlank(publicKey)) {
log.error("ReCaptcha service is enabled, however, private or public keys are not defined.");
return true;
}
boolean secure = request.isSecure();
ReCaptcha captcha;
if (secure) {
captcha = ReCaptchaFactory.newSecureReCaptcha(publicKey, privateKey, createNoScript);
} else {
captcha = ReCaptchaFactory.newReCaptcha(publicKey, privateKey, createNoScript);
}
String response = request.getParameter(PARAM_CAPTCHA_RESPONSE);
String challenge = request.getParameter(PARAM_CAPTCHA_CHALLENGE);
String remoteAddress = request.getRemoteAddr();
// validate:
ReCaptchaResponse captchaResponse = captcha.checkAnswer(remoteAddress, challenge, response);
boolean valid = captchaResponse.isValid();
if (valid) {
return true;
}
log.warn("Invalid captcha response: {}", captchaResponse.getErrorMessage());
return false;
}
示例9: RegistrationControllerValidator
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Autowired
public RegistrationControllerValidator(
ExpertValidationRulesChecker expertValidationRulesChecker,
ReCaptcha reCaptchaService) {
this.rules = expertValidationRulesChecker;
this.reCaptchaService = reCaptchaService;
}
示例10: createValidationCaptchaCallsReCaptchaMethodWithCorrectTheme
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Test
public void createValidationCaptchaCallsReCaptchaMethodWithCorrectTheme() throws Exception {
// Arrange
ReCaptcha mockCaptcha = mock(ReCaptcha.class);
when(mockCaptcha.createRecaptchaHtml(anyString(), anyString(), anyInt())).thenReturn("expected");
RegistrationControllerValidator target =
new RegistrationControllerValidator(mock(ExpertValidationRulesChecker.class), mockCaptcha);
// Act
String result = target.createValidationCaptcha();
// Assert
assertThat(result).isEqualTo("expected");
verify(mockCaptcha).createRecaptchaHtml(eq((String) null), eq("clean"), eq((Integer) null));
}
示例11: validateBasicFieldsChecksEmail
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Test
public void validateBasicFieldsChecksEmail() throws Exception {
// Arrange
ExpertValidationRulesChecker checker = mock(ExpertValidationRulesChecker.class);
RegistrationControllerValidator target = new RegistrationControllerValidator(checker, mock(ReCaptcha.class));
Expert expert = mockExpertBasic();
when(expert.getEmail()).thenReturn("email");
// Act
target.validateBasicFields(expert);
// Assert
verify(checker).checkEmail(eq(expert.getEmail()), anyListOf(String.class));
}
示例12: validateBasicFieldsChecksPassword
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Test
public void validateBasicFieldsChecksPassword() throws Exception {
// Arrange
ExpertValidationRulesChecker checker = mock(ExpertValidationRulesChecker.class);
RegistrationControllerValidator target = new RegistrationControllerValidator(checker, mock(ReCaptcha.class));
Expert expert = mockExpertBasic();
when(expert.getPassword()).thenReturn("password");
// Act
target.validateBasicFields(expert);
// Assert
verify(checker).checkPassword(eq(expert.getPassword()), anyListOf(String.class));
}
示例13: validateDetailsFieldsChecksName
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Test
public void validateDetailsFieldsChecksName() throws Exception {
// Arrange
ExpertValidationRulesChecker checker = mock(ExpertValidationRulesChecker.class);
RegistrationControllerValidator target = new RegistrationControllerValidator(checker, mock(ReCaptcha.class));
Expert expert = mockExpertDetails();
when(expert.getName()).thenReturn("name");
// Act
target.validateDetailsFields(expert);
// Assert
verify(checker).checkName(eq(expert.getName()), anyListOf(String.class));
}
示例14: validateDetailsFieldsChecksJobTitle
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Test
public void validateDetailsFieldsChecksJobTitle() throws Exception {
// Arrange
ExpertValidationRulesChecker checker = mock(ExpertValidationRulesChecker.class);
RegistrationControllerValidator target =
new RegistrationControllerValidator(checker, mock(ReCaptcha.class));
Expert expert = mockExpertDetails();
when(expert.getJobTitle()).thenReturn("job");
// Act
target.validateDetailsFields(expert);
// Assert
verify(checker).checkJobTitle(eq(expert.getJobTitle()), anyListOf(String.class));
}
示例15: validateDetailsFieldsChecksInstitution
import net.tanesha.recaptcha.ReCaptcha; //导入依赖的package包/类
@Test
public void validateDetailsFieldsChecksInstitution() throws Exception {
// Arrange
ExpertValidationRulesChecker checker = mock(ExpertValidationRulesChecker.class);
RegistrationControllerValidator target =
new RegistrationControllerValidator(checker, mock(ReCaptcha.class));
Expert expert = mockExpertDetails();
when(expert.getInstitution()).thenReturn("institute");
// Act
target.validateDetailsFields(expert);
// Assert
verify(checker).checkInstitution(eq(expert.getInstitution()), anyListOf(String.class));
}