List of usage examples for javax.servlet.http Cookie Cookie
public Cookie(String name, String value)
From source file:testapp.CsrfTokenResponseHeaderBindingFilter.java
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, javax.servlet.FilterChain filterChain) throws ServletException, IOException { CsrfToken token = (CsrfToken) request.getAttribute(REQUEST_ATTRIBUTE_NAME); Enumeration<String> e = request.getAttributeNames(); while (e.hasMoreElements()) { String param = (String) e.nextElement(); System.out.println(param); }//ww w . j a v a 2s .co m if (token != null) { response.setHeader(RESPONSE_HEADER_NAME, token.getHeaderName()); response.setHeader(RESPONSE_PARAM_NAME, token.getParameterName()); response.setHeader(RESPONSE_TOKEN_NAME, token.getToken()); response.addCookie(new Cookie("XSRF-TOKEN", token.getToken())); } filterChain.doFilter(request, response); }
From source file:com.erudika.scoold.utils.HttpUtils.java
/** * Sets a cookie./*from w w w . j a v a 2 s . c om*/ * @param name the name * @param value the value * @param req HTTP request * @param res HTTP response * @param httpOnly HTTP only flag * @param maxAge max age */ public static void setRawCookie(String name, String value, HttpServletRequest req, HttpServletResponse res, boolean httpOnly, int maxAge) { if (StringUtils.isBlank(name) || value == null || req == null || res == null) { return; } Cookie cookie = new Cookie(name, value); cookie.setHttpOnly(httpOnly); cookie.setMaxAge(maxAge < 0 ? Config.SESSION_TIMEOUT_SEC : maxAge); cookie.setPath("/"); cookie.setSecure(req.isSecure()); res.addCookie(cookie); }
From source file:com.xidu.framework.common.util.CookieUtils.java
/** * set the name/value entry to the cookie * /* w w w . jav a 2 s. c om*/ * @Date : 2011-3-23 * @param response * - HttpServletResponse's instance * @param name * - Cookie's Entry key * @param value * - Cookie's Entry value * @param path * - Cookie's path * @param domain * - Cookie' domain * @param maxAge * - Cookie's max age */ public static void setCookie(HttpServletResponse response, String name, String value, String path, String domain, int maxAge) { logger.debug("cookie value:" + value); Cookie cookie = new Cookie(name, value); cookie.setSecure(false); if (StringUtils.isNotBlank(path)) { cookie.setPath(path); } cookie.setMaxAge(maxAge); if (StringUtils.isNotBlank(domain)) { cookie.setDomain(domain); } response.addCookie(cookie); }
From source file:org.ngrinder.infra.spring.UserHandlerMethodArgumentResolverTest.java
@Test public void testUserHandlerMethodArgument() throws Exception { // create a tmp test user "TEST2_USER" for this test User user = new User(); user.setUserId("TEST2_USER"); user.setUserName("TEST2_USER"); user.setEmail("TEST2_USER@nhn.com"); user.setPassword("123"); user.setRole(Role.USER);//from w w w. j a va 2s . c o m user = userRepository.save(user); User testUser = getTestUser(); testUser.setFollowers(Lists.newArrayList(user)); userRepository.save(testUser); resolver = new UserHandlerMethodArgumentResolver() { @Override Cookie[] getCookies(NativeWebRequest webRequest) { return new Cookie[] { new Cookie("switchUser", switchUser) }; } }; resolver.setUserService(userService); MethodParameter parameter = mock(MethodParameter.class); final Class<?> class1 = User.class; when(parameter.getParameterType()).thenAnswer(new Answer<Class<?>>() { @Override public Class<?> answer(InvocationOnMock invocation) throws Throwable { return class1; } }); assertThat(resolver.supportsParameter(parameter), is(true)); // test1 scenario: general user can not check other user's script // has parameter "ownerId", and current user is general, resolved user is "TEST_USER" ServletWebRequest webRequest1 = mock(ServletWebRequest.class); when(webRequest1.getParameter("ownerId")).thenReturn("TEST2_USER"); resolver.setUserContext(mockUserContext); Object resolveArgument1 = resolver.resolveArgument(parameter, null, webRequest1, null); assertThat(((User) resolveArgument1).getUserId(), is(getTestUser().getUserId())); // test2 scenario: admin can check other user's script // has parameter "ownerId", and current user is Admin, resolved user is "TEST2_USER" ServletWebRequest webRequest2 = mock(ServletWebRequest.class); when(webRequest2.getParameter("ownerId")).thenReturn("TEST2_USER"); User adminUser = new User("tmpAdminId", "tmpAdminId", "tmpAdminPwd", "admin@nhn.com", Role.ADMIN); MockUserContext adminUserContext = mock(MockUserContext.class); when(adminUserContext.getCurrentUser()).thenReturn(adminUser); resolver.setUserContext(adminUserContext); Object resolveArgument2 = resolver.resolveArgument(parameter, null, webRequest2, null); assertThat(((User) resolveArgument2).getUserId(), is("TEST2_USER")); // test3 scenario: general user switch to use other's permission // has parameter "switchUser", resolved user id is "TEST2_USER" ServletWebRequest webRequest3 = mock(ServletWebRequest.class); switchUser = "TEST2_USER"; resolver.setUserContext(mockUserContext); User resolveArgument3 = cast(resolver.resolveArgument(parameter, null, webRequest3, null)); assertThat(((User) resolveArgument3).getUserId(), is("TEST_USER")); // current user's owner is "TEST2_USER" // assertThat(resolveArgument3.getOwnerUser().getUserId(), is("TEST2_USER")); // test4 scenario: general user switch back to its own user permission // has parameter "switchUserId", resolved user id is "TEST_USER" ServletWebRequest webRequest4 = mock(ServletWebRequest.class); switchUser = "TEST_USER"; resolver.setUserContext(mockUserContext); Object resolveArgument4 = resolver.resolveArgument(parameter, null, webRequest4, null); assertThat(((User) resolveArgument4).getUserId(), is("TEST_USER")); // current user's owner is null }
From source file:net.sourceforge.vulcan.web.PreferencesFilterTest.java
public void testLoadsPreferencesFromCookieIntoSessionIfPresent() throws Exception { request.addCookie(new Cookie(Keys.PREFERENCES, "descending")); request.getSession();/* w w w . j av a 2 s. c o m*/ EasyMock.expect(prefStore.convertFromString("descending")).andReturn(new PreferencesDto()); filter(); assertNotNull(request.getSession().getAttribute(Keys.PREFERENCES)); }
From source file:cn.vlabs.umt.ui.servlet.LogoutServlet.java
private void removeCookie(HttpServletResponse response, String domain, String path, String name) { Cookie cookie = new Cookie(name, ""); if (!CommonUtils.isNull(domain)) { cookie.setDomain(domain);/*from w ww. ja v a 2 s . c o m*/ } if (!CommonUtils.isNull(path)) { cookie.setPath(path); } cookie.setMaxAge(0); response.addCookie(cookie); }
From source file:net.smartam.leeloo.controller.RedirectController.java
@RequestMapping(method = RequestMethod.GET) public ModelAndView handleRedirect(@ModelAttribute("oauthParams") OAuthParams oauthParams, HttpServletRequest request, HttpServletResponse response) { try {//from w w w . j av a2 s .c o m // Create the response wrapper OAuthAuthzResponse oar = null; oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request); // Get Authorization Code String code = oar.getCode(); // Get OAuth Info String clientId = Utils.findCookieValue(request, "clientId"); String clientSecret = Utils.findCookieValue(request, "clientSecret"); String authzEndpoint = Utils.findCookieValue(request, "authzEndpoint"); String tokenEndpoint = Utils.findCookieValue(request, "tokenEndpoint"); String redirectUri = Utils.findCookieValue(request, "redirectUri"); String scope = Utils.findCookieValue(request, "scope"); String app = Utils.findCookieValue(request, "app"); response.addCookie(new Cookie("app", app)); oauthParams.setAuthzCode(code); oauthParams.setClientId(clientId); oauthParams.setClientSecret(clientSecret); oauthParams.setAuthzEndpoint(authzEndpoint); oauthParams.setTokenEndpoint(tokenEndpoint); oauthParams.setRedirectUri(redirectUri); oauthParams.setScope(Utils.isIssued(scope)); oauthParams.setApplication(app); } catch (OAuthProblemException e) { StringBuffer sb = new StringBuffer(); sb.append("</br>"); sb.append("Error code: ").append(e.getError()).append("</br>"); sb.append("Error description: ").append(e.getDescription()).append("</br>"); sb.append("Error uri: ").append(e.getUri()).append("</br>"); sb.append("State: ").append(e.getState()).append("</br>"); oauthParams.setErrorMessage(sb.toString()); return new ModelAndView("main"); } return new ModelAndView("request_token"); }
From source file:com.third.rent.user.controller.LoginController.java
@RequestMapping(value = "/user/login.do", method = RequestMethod.POST) public String login_post(@RequestParam String userId, @RequestParam String userPwd, @RequestParam(required = false) String chkSaveId, HttpServletRequest request, HttpServletResponse response, Model model) { // 1//from w w w .java 2 s. c o m logger.info("? , ? userId={} userPwd={}", userId, userPwd); logger.info("? chkSaveId={}", chkSaveId); // 2 int result = userService.loginCheck(userId, userPwd); logger.info("? , ? result={}", result); String msg = "", url = "/user/login.do"; if (result == userService.LOGIN_OK) { UserVO vo = userService.selectByUserid(userId); msg = vo.getUserName() + " ? ?."; url = "/user/index.do"; // ? HttpSession session = request.getSession(); session.setAttribute("userId", userId); session.setAttribute("userName", vo.getUserName()); session.setAttribute("userLicense", vo.getUserLicense()); // ? Cookie ck = new Cookie("ck_userId", userId); ck.setPath("/"); // ? ? if (chkSaveId != null) { ck.setMaxAge(1000 * 24 * 60 * 60); // 1000? response.addCookie(ck); } else { ck.setMaxAge(0); // response.addCookie(ck); } } else if (result == userService.ID_NONE) { msg = " ? ."; } else if (result == userService.PWD_DISAGREE) { msg = " ? "; } else { msg = "? ? "; } // 3 model.addAttribute("msg", msg); model.addAttribute("url", url); return "common/message"; }
From source file:org.jasig.cas.web.support.CookieRetrievingCookieGeneratorTests.java
public void testCookieRetrieve() { final MockHttpServletRequest request = new MockHttpServletRequest(); final Cookie cookie = new Cookie("test", "test"); cookie.setDomain("cas.org"); cookie.setMaxAge(5);//w ww . jav a2 s . c o m request.setCookies(new Cookie[] { cookie }); assertEquals("test", this.g.retrieveCookieValue(request)); }
From source file:org.examproject.tweet.controller.OAuthController.java
private static void storeTokenToCookie(HttpServletResponse response, OAuthAccessorValue accessorValue, int maxAge) { Cookie cookie = new Cookie(TweetCookie.REQUEST_TOKEN.getName(), accessorValue.getRequestToken()); cookie.setMaxAge(maxAge);/*from ww w .j av a2 s . c o m*/ response.addCookie(cookie); cookie = new Cookie(TweetCookie.ACCESS_TOKEN.getName(), accessorValue.getAccessToken()); cookie.setMaxAge(maxAge); response.addCookie(cookie); cookie = new Cookie(TweetCookie.TOKEN_SECRET.getName(), accessorValue.getTokenSecret()); cookie.setMaxAge(maxAge); response.addCookie(cookie); cookie = new Cookie(TweetCookie.USER_ID.getName(), accessorValue.getId()); cookie.setMaxAge(maxAge); response.addCookie(cookie); cookie = new Cookie(TweetCookie.SCREEN_NAME.getName(), accessorValue.getScreenName()); cookie.setMaxAge(maxAge); response.addCookie(cookie); }