how to create a cookie and add to http response from inside my service layer?

前端 未结 4 814
天涯浪人
天涯浪人 2021-01-03 21:25

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

@Service
public class AuthenticationServiceImpl implements AuthenticationService          


        
相关标签:
4条回答
  • 2021-01-03 22:04

    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);
    
    0 讨论(0)
  • A cookie is a object with key value pair to store information related to the customer. Main objective is to personalize the customer's experience.

    An utility method can be created like

    private Cookie createCookie(String cookieName, String cookieValue) {
        Cookie cookie = new Cookie(cookieName, cookieValue);
        cookie.setPath("/");
        cookie.setMaxAge(MAX_AGE_SECONDS);
        cookie.setHttpOnly(true);
        cookie.setSecure(true);
        return cookie;
    }
    

    If storing important information then we should alsways put setHttpOnly so that the cookie cannot be accessed/modified via javascript. setSecure is applicable if you are want cookies to be accessed only over https protocol.

    using above utility method you can add cookies to response as

    Cookie cookie = createCookie("name","value");
    response.addCookie(cookie);
    
    0 讨论(0)
  • 2021-01-03 22:23

    Following @Aravind's answer with more details

    @RequestMapping("/myPath.htm")
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception{
        myServiceMethodSettingCookie(request, response);        //Do service call passing the response
        return new ModelAndView("CustomerAddView");
    }
    
    // service method
    void myServiceMethodSettingCookie(HttpServletRequest request, HttpServletResponse response){
        final String cookieName = "my_cool_cookie";
        final String cookieValue = "my cool value here !";  // you could assign it some encoded value
        final Boolean useSecureCookie = false;
        final int expiryTime = 60 * 60 * 24;  // 24h in seconds
        final String cookiePath = "/";
    
        Cookie cookie = new Cookie(cookieName, cookieValue);
    
        cookie.setSecure(useSecureCookie);  // determines whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL
    
        cookie.setMaxAge(expiryTime);  // A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
    
        cookie.setPath(cookiePath);  // The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories
    
        response.addCookie(cookie);
    }
    

    Related docs:

    http://docs.oracle.com/javaee/7/api/javax/servlet/http/Cookie.html

    http://docs.spring.io/spring-security/site/docs/3.0.x/reference/springsecurity.html

    0 讨论(0)
  • 2021-01-03 22:27

    To add a new cookie, use HttpServletResponse.addCookie(Cookie). The Cookie is pretty much a key value pair taking a name and value as strings on construction.

    0 讨论(0)
提交回复
热议问题