Spring-Boot:有没有办法通过aop添加Cookie

Spring-Boot:Is there any way to add Cookies by aop

我尝试通过 aop 添加 Cookie,但是 work.I 无法在浏览器中找到 Cookie。 我想 HttpServletResponse 只能在控制器中更改。

@Aspect
@Component
public class AspectDemo
{
    @Before("execution(* com.example.demo.controller.RestfulController.rest(..))")
    void beforeLogin(JoinPoint joinPoint)
    {
        HttpServletResponse httpServletResponse= (HttpServletResponse) joinPoint.getArgs()[0];
        httpServletResponse.addCookie(new Cookie("TestAttribute","test"));
    }

}

尝试指定cookie的路径。如果它没有帮助,请尝试设置 httpOnly 标志。这是我的私人项目的简短示例:

newCookie.setPath("/");
newCookie.setHttpOnly(true);
newCookie.setMaxAge(Integer.MAX_VALUE);
response.addCookie(newCookie);

如果它解决了您的问题,请告诉我。