简体   繁体   中英

Spring Security & JSR-303: Entity validation on Filter level

I'm working on a REST-service with Spring 4 . I want to validate an entity inside a filter chain. I can't use standard @Validate annotation inside @Controller , because required URI for a request is specified just on the filter level (I extended UsernamePasswordAuthenticationFilter for a further customization).

Please give me advice how to validate such entity inside a filter:

public class UserEntity {

    @NotNull
    @Email
    private String username;

    @NotNull
    private String password;

    //getters and setters
}

as a result I want to be able to retrieve errors from a binder.

Thanks

Well I solved the issue via declaration of LocalValidatorFactoryBean in a @Configuration class, and then used it at filter level.

...
@Autowired
private Validator validator;
...
DataBinder binder = new DataBinder(entityWhichNeedToBeValidated);
binder.setValidator(validator);
binder.validate();
BindingResult result = binder.getBindingResult();
...

After this I have a full information about the validated fields and complete program flow as I want.

I think this give you a some hint for implement rest call with spring validation. http://spring.io/blog/2012/08/29/integrating-spring-mvc-with-jquery-for-validation-rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM