繁体   English   中英

实施Spring Security后,Spring Boot ResponseBody未序列化为JSON

[英]Spring Boot ResponseBody is not Serializing to JSON after implementing Spring Security

在将Spring Security(实际上没有对其进行任何配置)添加到我的Spring Boot项目中之后,集成测试失败并显示以下错误:

java.lang.AssertionError: Response body was not valid JSON: <CustomResponse><content><id>dde6fd40-0ca3-482b-9a8e-b05fdab1b7b6</id><domain>somedomain.com</domain></content></CustomResponse>

在将Spring Security添加到项目之前,我的响应主体已适当地序列化为JSON。

我一直在搜寻Spring Security文档,以查看任何响应主体和客户端之间发生了什么,但我不知道为什么Spring Security可能会干扰RestController对其进行序列化的方式。

这是我的RestController的示例:

@RestController
@RequestMapping("example-mapping")
@AllArgsConstructor
public class ExampleController {

  private final ExampleService exampleService;

  @GetMapping("/example")
  public CustomResponse<ExampleDTO> checkOut(@RequestParam("domain") String domain) {

    ExampleEntity result = exampleService.checkOut(domain);

    return new CustomResponse<>(new ExampleDTO(result));
  }
}

这是我的准系统,没有安全配置:

@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  private AuthenticationFilter authenticationFilter;

  protected void configure(HttpSecurity http) throws Exception {
    http
        .addFilterBefore(authenticationFilter, BasicAuthenticationFilter.class)
        .httpBasic().disable();
  }
}

这是我的准系统,没有认证过滤器:

@Component
@AllArgsConstructor
public class AuthenticationFilter extends OncePerRequestFilter {

  private ObjectMapper objectMapper; // injecting a global object mapper to read headers

  @Override
  protected void doFilterInternal(
      HttpServletRequest request,
      HttpServletResponse response,
      FilterChain filterChain) throws ServletException, IOException {

    // Read some headers, but currently not doing anything

    filterChain.doFilter(request, response);
  }
}

编辑:查看响应的Content-Type后,其application/xml (DUH)。 所以问题是,auth过滤器将响应设置为XML在哪里?

编辑:将@GetMapping("/example")更改为@GetMapping(value = "/example", produces = "application/json")可解决序列化问题。 有没有办法让它们默认都产生JSON?

使用spring-web设置项目时,JSON已经是默认的内容类型。 在类路径中是否也有spring-security没关系。 我创建了一个最少依赖的示例项目:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

我的TestController默认返回JSON:

@RestController
public class TestController {

    @GetMapping("/hello")
    public Map<String, String> getGreeting() {
        final HashMap<String, String> result = new HashMap<>();
        result.put("value", "hello");
        return result;
    }

}
{
  "value": "hello"
}

因此,您的依赖项可能存在问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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