简体   繁体   English

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

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

After adding Spring Security (without really configuring it to do anything) to my Spring Boot project, the integration tests have been failing with the following error: 在将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>

Before adding Spring Security to the project, my response bodies have been appropriately serialized to JSON. 在将Spring Security添加到项目之前,我的响应主体已适当地序列化为JSON。

I've been scouring the Spring Security docs to see what is going on between any response body and the client, but I couldn't figure out why Spring Security might be interfering with how the RestController serializes it. 我一直在搜寻Spring Security文档,以查看任何响应主体和客户端之间发生了什么,但我不知道为什么Spring Security可能会干扰RestController对其进行序列化的方式。

Here is a sample of my 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));
  }
}

Here is my barebones, nothing Security Config: 这是我的准系统,没有安全配置:

@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();
  }
}

Here is my barebones, nothing auth filter: 这是我的准系统,没有认证过滤器:

@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);
  }
}

EDIT: After looking at the Content-Type of the response, its application/xml (DUH). 编辑:查看响应的Content-Type后,其application/xml (DUH)。 So the question is, where is the the auth filter setting the response as XML? 所以问题是,auth过滤器将响应设置为XML在哪里?

EDIT: Changing @GetMapping("/example") to @GetMapping(value = "/example", produces = "application/json") fixes the serialization problem. 编辑:将@GetMapping("/example")更改为@GetMapping(value = "/example", produces = "application/json")可解决序列化问题。 Is there a way to have them all produce JSON by default? 有没有办法让它们默认都产生JSON?

JSON is already the default content-type when you set up a project with spring-web . 使用spring-web设置项目时,JSON已经是默认的内容类型。 It shouldn't matter if there is also spring-security in the classpath. 在类路径中是否也有spring-security没关系。 I created an example project with a minimum of dependencies: 我创建了一个最少依赖的示例项目:

<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>

My TestController returns by default JSON: 我的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"
}

So there might be a problem with your dependencies. 因此,您的依赖项可能存在问题。

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

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