我的配置如下,但是返回的json中没有验证错误的详细信息,请问什么原因?

来源:3-3 验证注解和自定义验证注解

慕UI2559437

2021-09-09

我的配置如下,但是返回的json中么有验证的详细信息,请问什么原因?

rest client:

###
# @name register
POST {{host}}/authorize/register HTTP/1.1
Content-Type: application/json

{
    "name": "aa"
}

返回的JSON:

HTTP/1.1 400
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 09 Sep 2021 06:00:45 GMT
Connection: close

{
“timestamp”: “2021-09-09T06:00:45.649+00:00”,
“status”: 400,
“error”: “Bad Request”,
“path”: “/authorize/register”
}

SecurityConfig.java

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests(req -> req
                        .antMatchers("/authorize/**").permitAll()
                        .antMatchers("/admin/**").hasRole("ADMIN")
                        .antMatchers("/api/**").hasRole("USER")
                        .anyRequest().authenticated())
                .addFilterAt(restAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .csrf(csrf -> csrf.disable());

API:

@RestController
@RequestMapping("/authorize")
public class AuthorizeController {
    @PostMapping("register")
    public UserDto register(@Valid @RequestBody UserDto userDto) {
        return userDto;
    }
}

写回答

1回答

慕UI2559437

提问者

2021-09-09

server:
  error:
    include-binding-errors: always

追加后,OK了。
参考URL,但是我也没有详细确认哈。

https://reflectoring.io/spring-boot-exception-handling/

1
0

Spring Security+OAuth2 精讲,打造企业级认证与授权

一站式掌握主流安全框架与行业解决方案,从容应对各种安全难题。

1061 学习 · 316 问题

查看课程