I use reCAPTCHA in my Java application and there is an approach similar to the following code:
LoginRequest:
public class LoginRequest {
@NotEmpty
private String token;
@NotEmpty
private String username;
@NotEmpty
private String password;
}
LoginController :
public class LoginController {
private final Validator validator;
@Value("${isDevelopment}")
private boolean isDevelopment;
public LoginController(Validator validator) {
this.validator = validator;
}
public User login(LoginRequest request) {
if(isDevelopment || validator.isValid(request.token)) {
// ...
}
throw new InvalidTokenException();
}
}
In this approach, I thought to bypass validation using the application development environment variable isDevelopment
. However, I am not sure if there is a more elegant way. Because reCAPTCHA
is used widely and most probably there would be a better standards for this purpose. How can I bypass reCAPTCHA during development?
Update: Here is my Validator
class:
@Component
@RequiredArgsConstructor
public class CaptchaValidator {
private final RecaptchaEnterpriseServiceClient client;
@Value("${enabled}")
private boolean isEnabled;
public boolean isValid(String token) {
// code omitted
}
}