如何创建一个cookie并从我的服务层内部添加到http响应? [英] how to create a cookie and add to http response from inside my service layer?

查看:121
本文介绍了如何创建一个cookie并从我的服务层内部添加到http响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Spring MVC应用程序中创建自定义身份验证服务:

I am creating a custom authentication service in my spring mvc application:

@Service
public class AuthenticationServiceImpl implements AuthenticationService {

   @Autowired
   UserService userService;

   @Override
   public void login(String email, String password) {

      boolean isValid = userService.isValidLogin(email, password);

      if(isValid) {
          // ??? create a session cookie and add to http response
      }

   }

}

如何创建Cookie并将其添加到响应中?

How can I create and add the cookie to the response?

推荐答案

在Spring MVC中,默认情况下,您将获得HtppServletResponce对象.

In Spring MVC you get the HtppServletResponce object by default .

   @RequestMapping("/myPath.htm")
    public ModelAndView add(HttpServletRequest request,
         HttpServletResponse response) throws Exception{
            //Do service call passing the response
    return new ModelAndView("CustomerAddView");
    }

//Service code
Cookie myCookie =
  new Cookie("name", "val");
  response.addCookie(myCookie);

这篇关于如何创建一个cookie并从我的服务层内部添加到http响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆