有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SetCookie标头已从响应Spring boot中删除

我有简单的@RestController,我正在尝试添加cookies,但在响应中没有看到它。添加任何其他响应头都可以,但是如果我使用。addCookie()或。addHeader(“设置Cookie”、“某物”)正在被过滤掉。此外,没有实现自定义过滤器

如何禁用此功能并将cookie添加到响应

Spring安全配置:

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

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

共 (1) 个答案

  1. # 1 楼答案

    控制器代码:

    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   
    

    我想,你最好发布你的代码来解决这个问题