Set-Cookie标头已从响应(春季启动)中移除

时间:2019-07-14 10:42:58

标签: java spring security header

我有一个简单的@RestController,我正在尝试添加cookie,但是我在响应中看不到它。添加任何其他响应标头都可以,但是如果我使用.addCookie()或.addHeader(“ Set-Cookie”,“ something”),则将其过滤掉。此外,没有实现任何自定义过滤器。

如何禁用此功能并向响应添加cookie?

Spring安全配置:

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

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

1 个答案:

答案 0 :(得分:0)

控制器代码:

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";
    }
}

启动应用程序后,自定义Cookie可以正常运行:

$ 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   

我想,您最好发布代码来解决问题