小编Kur*_*urt的帖子

仅当不为空或零时,如何java bean 验证范围

我想使用 Java Bean 验证来验证一个整数。它是多重验证的验证。

我目前正在使用 Spring Boot 和验证,并且系统正在使用@RestController我正在接收呼叫后的位置。

public Person addPerson(@RequestBody @Validated Person Person) {/*the code*/}
Run Code Online (Sandbox Code Playgroud)

我希望验证年龄,这些值是有效的:

age == null or age == 0 or (age >= 15 and age <= 80)


public class Person {
    private Integer age;
}
Run Code Online (Sandbox Code Playgroud)

我希望能够使用 java 的当前验证约束。我需要实现我自己的约束注释吗?

这会很好,但这不起作用:

public class Person {
    @Null
    @Range(min=0, max=0)
    @Range(min=15, max = 80)
    private Integer age;
}
Run Code Online (Sandbox Code Playgroud)

java validation validationrules bean-validation

2
推荐指数
1
解决办法
4639
查看次数

Spring Boot:禁用状态异常代码的安全性

我有一个安全的 Spring Boot 应用程序。我已经删除了这个“/login”网址的身份验证。

我的安全配置

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    private final JwtFilter jwtFilter;

    @Autowired
    public SecurityConfiguration(JwtFilter jwtFilter) {
        this.jwtFilter = jwtFilter;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .anonymous().and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .anyRequest().authenticated().and()
                .apply(new JwtConfigurerAdapter(jwtFilter)).and()
                .exceptionHandling().authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/v2/api-docs");
        web.ignoring().antMatchers("/login");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 NotFound 异常

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class NotFound extends RuntimeException {
    public NotFound(String message) {
        super(message);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的带有登录 url 和异常返回值的休息控制器

@RestController
public class …
Run Code Online (Sandbox Code Playgroud)

java exception-handling spring-security spring-boot spring-security-rest

1
推荐指数
1
解决办法
826
查看次数