当前位置: 首页 > 知识库问答 >
问题:

在JSR-303 Bean验证中获取HttpServletRequest

楚和悌
2023-03-14

我的应用程序中有一个验证,它使用cookies(request.getCookies)来验证验证码。

我想为这个captcha验证创建一个ConstraintValidator,以便它与其他bean的属性一起进行验证——正如JSR-303 bean验证所指定的那样。

有没有办法在ConstraintValidator中检索HttpServletRequest?

共有1个答案

施海
2023-03-14

假设(由于当前标记)您使用的是Spring,其中包含一个最新版本的(

package org.yourapp.controller.validation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class YourValidator implements ConstraintValidator<YourValidatorAnnotaion, String> {
    // here should be autowired a proxy to currently undergoing request
    @Autowired
    private HttpServletRequest request;

    @Override
    public void initialize(YourValidatorAnnotaion constraintAnnotation) {
        // this should autowire all dependencies of this class using
        // current application context
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // here goes your custom logic for validation, like matching
        // captcha challenge to captcha response, but i am not doing
        // this for you, as i don't know what it supposed to be, so
        // i simply test presence of cookies.
        return request.getCookies().length > 0;
    }
}

为了确保完整性,下面是一个示例YourValidatorAnnotation实现:

package org.yourapp.controller.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Constraint(validatedBy = YourValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface YourValidatorAnnotaion {
    String message() default "No cookies - no validation";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

就是这样。现在,如果您使用YourValidatorAnnotation对DTO字段进行注释,那么当在请求头中没有cookie的情况下调用具有此类有效@RequestBody参数的控制器时,您应该会得到一个错误。

 类似资料:
  • 我的bean中有两个字段 当字段key=“A”,“value”应该跟在其他“key”的特定Regex后面时,它可以是任何内容。 如何根据键定义此值验证。

  • 我使用Spring 3.2.2与Primeface 4.0和Hibernate 4.2。我在我的实体上有JSR303验证注释,并且我配置了Spring来在服务层验证它们——这很好。 但是我希望在调用服务之前启动JSF验证,但事实并非如此。我所做的所有研究都表明,我只需在类路径中添加一个验证程序,JSF2就会自动应用。 我已将建议的JAR添加到我的类路径中: 完整的依赖关系树可以在https://g

  • 我目前正在用Spring开发一个应用程序,使用Hibernate作为ORM。我知道,默认情况下,Hibernate在持久化或加载实体时都会使用JSR-303 Bean验证。由于此应用程序支持草稿(我希望在持久化后执行验证),因此必须将其添加到persistence.xml中: 模型实体代码: 我想知道如何传播bean验证,以便每当MyEntity实例被验证时,所有相关联的bar也被验证,而不必在验

  • 接口说明 获取验证码 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 POST /api/server/1.0.0/getValidCode 是否需要登录 否 请求字段说明 无 响应字段说明 无 响应成功示例 {} 响应失败示例 {} 响应接受类型 image/jpeg;charset=UTF-8 响应状态码 HTTP状态码 原因 204 N

  • 错误: TypeError:无法读取未定义的LoginForm c:/reactjs/hello-world/src/components/accountbox/LoginForm.js的属性“state”:23 2023 value={this.state.input.email}^24 onchange={this.handlechange}25 class=“form-control”26

  • scala支持JSR-303验证吗? 如果是的话,你能写一个例子吗? 如果没有-是否有变通方法在scala类上运行JSR-303验证?