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

Viewed 207

I try to add a Cookie by aop,but it dosent work.I cant find the Cookie in Browser. I guess the HttpServletResponse only Could be changed in a Controller.

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

}
1 Answers

Try to specify cookie's path. If it does not help try to set httpOnly flag as well. Here is short example from my private project:

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

Let me know if it solved your problem.

Related