简体   繁体   中英

Set-Cookie header is removed from response, Spring boot

I have simple @RestController and im trying to add cookies but I don't see it in the response. Adding any other response header works just fine, but if I use .addCookie() or .addHeader("Set-Cookie", "something") its being filtered out. Further no custom filters are implemented.

How can I disable this and add cookie to response?

Spring security configuration:

 @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity
                .authorizeRequests()
                .anyRequest()
                .permitAll();

Controller code:

import  org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;

@RestController
public class MyRestController {

    @GetMapping(value="/some")
    public Object getUser(HttpServletResponse resp) {

        resp.addCookie(new Cookie("yes","it_is_possible"));
        return "Some test string";
    }
}

After launch the application, custom cookie works OK:

$ curl -v http://127.0.0.1:8080/some
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /some HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/1.2.3
> Accept: */*
> 
< HTTP/1.1 200 
< Set-Cookie: yes=it_is_possible
< 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: text/plain;charset=UTF-8
< Content-Length: 16
< Date: Mon, 15 Jul 2019 06:01:39 GMT
< 
* Curl_http_done: called premature == 0
* Connection #0 to host 127.0.0.1 left intact
Some test string   

I guess, you should better post your code to solve the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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