我在我的Web应用程序中使用Spring 4.0和最新的Spring Security。我想使用Hibernate验证5.1.1 Final。但它不起作用。相同的代码在我的JUNIT中工作,但在Web应用程序中不起作用。
有效的代码:
@Test
public void testUserAuthenticationRequestValidation(){
try{
UserAuthenticationRequest req=new UserAuthenticationRequest().addUserName("bla").addPasswd("passw");
Validator validation=Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<UserAuthenticationRequest>> constraintViolations= validation.validate(req);
Assert.assertTrue("There are some errors", constraintViolations.size()>0);
System.out.println(constraintViolations);
}catch(Exception ex){
ex.printStackTrace();
Assert.fail();
}
但是,如果我尝试在我的网络应用程序中使用相同的代码,它不起作用,在下面的代码中,我期待contraintViolation设置不为空,因为我故意留下用户名和&amp;密码为空,但我总觉得它是空的。
@Override
public UserDetails retrieveUser(String username,
UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
LOGGER.debug("About to check authentication for username: {}", username);
final UserAuthenticationRequest userData = new UserAuthenticationRequest()
.addUserName(username).addPasswd(
authentication.getCredentials().toString());
Validator validation = Validation.buildDefaultValidatorFactory()
.getValidator();
Set<ConstraintViolation<UserAuthenticationRequest>> constraintViolations = validation
.validate(userData);
LOGGER.info("Constraints Violations: {}", constraintViolations);
LOGGER.info("Obejct: username: {} password: {}",
userData.getUsername(), userData.getPasswd());
UserDetailVO userDetailVO = new UserDetailVO(
userInfoService.authenticate(userData));
return userDetailVO;
}
到目前为止,我还尝试了如下所示的初始化验证bean并注入我的类,如下所示:
@Configuration
@ComponentScan({ "com.sell.mystuff.web.security",
"com.sell.mystuff.web.service" })
@ImportResource({ "classpath*:spring/common-context.xml" })
public class CommonAppContext {
@Bean(name = "javaxValidator")
public Validator getValidator() {
return Validation.buildDefaultValidatorFactory().getValidator();
}
}
我甚至尝试在验证bean中显式初始化,如上所示,但似乎没有任何工作。 下面是我与验证api相关的部分POM.xml文件:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>5.1.1.Final</version>
</dependency>
答案 0 :(得分:0)
您是否配置了验证器bean?
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
这将引导Bean Validation 1.0或1.1,具体取决于您在类路径上使用的Hibernate Validator版本。另请参阅http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
即使您计划手动调用验证,也应该通过检索配置的工厂bean来完成,因为这样可以保证工厂的正确缓存。
答案 1 :(得分:0)
我通过添加更高版本的javax.el
来实现这一目标e.g。
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
BTW我不需要在我的spring mvc config中定义LocalValidatorFactoryBean。