2012-01-17 12 views
13

tôi đang tạo ra một dịch vụ xác thực tùy chỉnh trong ứng dụng MVC mùa xuân của tôi:cách tạo cookie và thêm vào phản hồi http từ bên trong lớp dịch vụ của tôi?

@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 
     } 

    } 

} 

Làm thế nào tôi có thể tạo ra và thêm cookie để phản ứng?

Trả lời

16

Trong Spring MVC bạn nhận được đối tượng HtppServletResponce theo mặc định.

@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

Để thêm cookie mới, hãy sử dụng HttpServletResponse.addCookie(Cookie). Các Cookie là khá nhiều một cặp giá trị quan trọng tham gia một tên và giá trị như chuỗi trên xây dựng.

+1

Btw, tôi không khuyên bạn làm điều này để tạo cơ chế thẩm định của riêng bạn. –

18

Sau @ câu trả lời Aravind với biết thêm chi tiết

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

tài liệu liên quan:

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

5

Cookie là một đối tượng với cặp giá trị quan trọng để lưu trữ thông tin liên quan đến khách hàng. Mục tiêu chính là cá nhân hóa trải nghiệm của khách hàng.

Một phương pháp hữu ích có thể được tạo ra như

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; 
} 

Nếu lưu trữ thông tin quan trọng thì chúng ta nên alsways đặt setHttpOnly để các cookie không thể được truy cập/sửa đổi thông qua javascript. setSecure được áp dụng nếu bạn muốn cookie chỉ được truy cập qua giao thức https.

sử dụng trên phương pháp hữu ích bạn có thể thêm các tập tin cookie để đáp ứng như

Cookie cookie = createCookie("name","value"); 
response.addCookie(cookie); 
Các vấn đề liên quan