Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache-control headers within the controller are ignored with spring security #8949

Closed
czubin opened this issue Apr 20, 2017 · 7 comments
Closed
Labels
status: duplicate A duplicate of another issue

Comments

@czubin
Copy link

czubin commented Apr 20, 2017

Bug report:

Since spring boot 1.5 the 'Cache-control' headers can no longer be set within a controller.
Using ResponseEntity.cacheControl().

The test and code below works under spring boot 1.4.5.

Security configuration:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    }
}

Controller:

@Controller
public class ResourceEndpoint {
    @GetMapping("/users/{name}")
    public ResponseEntity<UserDto> getUser(@PathVariable String name) {
        return ResponseEntity
          .ok()
          .cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS))
          .body(new UserDto(name));
    }
}

Test:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AppRunner.class)
public class ResourceEndpointIntegrationTest {
    @Test
    public void givenServiceEndpoint_whenGetRequestForUser_shouldResponseWithCacheControlMaxAge() {
        given().when().get(getBaseUrl() + "/users/Michael").then().contentType(ContentType.JSON).and().statusCode(200).and().header("Cache-Control", "max-age=60");
    }
    private String getBaseUrl() {
        return String.format("http://localhost:%d", serverPort);
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 20, 2017
@mbhave
Copy link
Contributor

mbhave commented Apr 20, 2017

@czubin This seems to be happening because of this change in Spring Security. spring-projects/spring-security@57d7ad0. Previously, as can be seen in this commit, spring-projects/spring-security@242b831, Spring Security would only write the Cache-Control header if it was not set.

@philwebb This doesn't seem like a Boot issue, but more like something for Spring Security or Spring MVC. WDYT?

@wilkinsona
Copy link
Member

I think this should have been fixed by spring-projects/spring-security@168f4b8 which is in Spring Security 4.2.2 (the default version in Boot 1.5.2).

@czubin What version of Spring Security are you using?

@czubin
Copy link
Author

czubin commented Apr 21, 2017

@wilkinsona We are currently using the latest(1.5.2).

It seems to me that previously the headers were written after completion of the filterchain. Which caused problems with users who flushed the response.

I'm going to use a workaround as specified in spring-projects/spring-security#2953

@philwebb
Copy link
Member

philwebb commented Apr 21, 2017

We think this duplicates spring-projects/spring-security#4199 so I'll close the issue for now. If you can create a sample that reproduces it with Spring Security 4.2.2 and Boot 1.5.3 please attach it and we'll re-open.

(edited with correct issue link)

@philwebb philwebb added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 21, 2017
@czubin
Copy link
Author

czubin commented Apr 21, 2017

@philwebb Okay, I'll make an issue with spring-security project. 1.5.3 has the same regression.

@philwebb
Copy link
Member

@czubin Thanks, can you paste a link to the issue that you create here so that we have a future breadcrumb trail.

@czubin
Copy link
Author

czubin commented Apr 22, 2017

Made a new ticket spring-projects/spring-security#4307

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants