Example usage for javax.servlet.http Cookie setPath

List of usage examples for javax.servlet.http Cookie setPath

Introduction

In this page you can find the example usage for javax.servlet.http Cookie setPath.

Prototype

public void setPath(String uri) 

Source Link

Document

Specifies a path for the cookie to which the client should return the cookie.

Usage

From source file:de.hska.ld.core.controller.HomeController.java

@RequestMapping("/logout")
public String logout(HttpServletRequest request, HttpServletResponse response, Principal p)
        throws ServletException {
    request.logout();/*w  w  w. j av a 2s  .c o m*/
    javax.servlet.http.Cookie cookie = new Cookie("sessionID", "");
    cookie.setPath("/");
    if (!"localhost".equals(env.getProperty("module.core.oidc.server.endpoint.main.domain"))) {
        cookie.setDomain(env.getProperty("module.core.oidc.server.endpoint.main.domain"));
    }
    cookie.setMaxAge(0);
    response.addCookie(cookie);
    return "redirect:";
}

From source file:org.unidle.social.SignInAdapterImpl.java

@Override
public String signIn(final String userId, final Connection<?> connection, final NativeWebRequest request) {

    final Authentication authentication = new UsernamePasswordAuthenticationToken(userId, null);

    SecurityContextHolder.getContext().setAuthentication(authentication);

    final Cookie cookie = new Cookie(LAST_LOGIN_SOURCE.getName(), connection.createData().getProviderId());
    cookie.setMaxAge(LAST_LOGIN_SOURCE.getMaxAgeAs(SECONDS));
    cookie.setPath("/");

    request.getNativeResponse(HttpServletResponse.class).addCookie(cookie);

    return null;/*from ww  w.  j av  a  2  s  . c om*/
}

From source file:org.apache.oodt.security.sso.OpenSSOImpl.java

private void clearCookie(String name) {
    Cookie userCookie = new Cookie(name, "blank");
    userCookie.setPath("/");
    userCookie.setMaxAge(0);/*from   ww w .j  a v a2  s  .c o  m*/
    this.res.addCookie(userCookie);
}

From source file:au.gov.dto.springframework.security.web.context.CookieSecurityContextRepository.java

private Cookie createExpireAuthenticationCookie(HttpServletRequest request) {
    Cookie removeSessionCookie = new Cookie(authenticationCookieName, "");
    removeSessionCookie.setPath(authenticationCookiePath);
    removeSessionCookie.setMaxAge(0);/*from   ww  w  . jav a 2  s. c  o  m*/
    removeSessionCookie.setHttpOnly(true);
    removeSessionCookie.setSecure(request.isSecure());
    return removeSessionCookie;
}

From source file:org.sharetask.security.StoreUserInformationAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    if (authentication instanceof ClientAuthenticationToken) {
        log.debug("Token is pac4j token.");

        String language = Language.EN.getCode();
        UsernamePasswordAuthenticationToken authentToken;
        final CommonProfile profile = (CommonProfile) ((ClientAuthenticationToken) authentication)
                .getUserProfile();/*  www .ja  va 2s . c o  m*/
        if (userRepository.findByUsername(profile.getEmail()) == null) {
            log.debug("User with name: {} doesne exist's. Will be created", profile.getEmail());
            final UserInformation userInformation = new UserInformation(profile.getEmail());
            userInformation.setName(profile.getFirstName());
            userInformation.setSurName(profile.getFamilyName());
            userInformation.setLanguage(language);
            final ArrayList<Role> list = new ArrayList<Role>();
            list.add(Role.ROLE_USER);
            userInformation.setRoles(list);
            userRepository.save(userInformation);
            final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.add(new SimpleGrantedAuthority(Role.ROLE_USER.name()));
            authentToken = new UsernamePasswordAuthenticationToken(profile.getEmail(), "", authorities);
        } else {
            final UserInformation user = userRepository.read(profile.getEmail());
            language = user.getLanguage();
            final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            authentToken = new UsernamePasswordAuthenticationToken(profile.getEmail(), "", authorities);
        }
        // language cookie
        final Cookie locale = new Cookie(RequestUltil.LOCALE, language);
        locale.setMaxAge(-1);
        locale.setPath("/");
        response.addCookie(locale);

        SecurityContextHolder.getContext().setAuthentication(authentToken);
    }

    super.onAuthenticationSuccess(request, response, authentication);
}

From source file:io.mapzone.controller.vm.http.LoginProvision.java

protected void registerUser(String userId, @SuppressWarnings("hiding") HttpServletResponse response) {
    // cookie token
    byte[] bytes = new byte[8];
    rand.nextBytes(bytes);//w  w w .  j  av a  2 s . c om
    String token = Base64.encodeBase64URLSafeString(bytes);

    // FIXME Leak: entries are never removed (allow just one cookie/session per user?)
    if (loggedIn.putIfAbsent(token, userId) != null) {
        throw new IllegalStateException("Token already exists: " + token);
    }

    // set cookie
    Cookie newCookie = new Cookie(COOKIE_NAME, token);
    newCookie.setHttpOnly(true);
    newCookie.setPath(COOKIE_PATH);
    newCookie.setSecure(false); // XXX
    newCookie.setMaxAge(COOKIE_MAX_AGE);
    response.addCookie(newCookie);
}

From source file:com.glaf.core.util.RequestUtils.java

public static void setLoginUser(HttpServletRequest request, HttpServletResponse response, String systemName,
        String actorId) {//from  ww  w .j ava 2  s .  c  om
    String ip = getIPAddress(request);
    ip = DigestUtils.md5Hex(ip);
    String value = encodeValues(ip, systemName, actorId);
    HttpSession session = request.getSession(false);
    if (session != null) {
        session.setAttribute(Constants.LOGIN_INFO, value);
    }
    Cookie cookie = new Cookie(Constants.COOKIE_NAME, value);
    cookie.setPath("/");
    cookie.setMaxAge(-1);
    response.addCookie(cookie);
}

From source file:cec.easyshop.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testServerSideCookieDefaultPath() {
    cookieGenerator.setCookieName("guid");
    cookieGenerator.setHttpOnly(true);//server side
    BDDMockito.given(request.getContextPath()).willReturn("/");
    cookieGenerator.addCookie(response, "cookie_monster");
    cookieGenerator.setUseDefaultPath(false);

    final Cookie expectedCookie = new Cookie("guid", "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);/*from  www  .  j a  v  a2  s.  c o  m*/
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addHeader(EnhancedCookieGenerator.HEADER_COOKIE,
            "guid=cookie_monster; Version=1; Domain=\"what a domain\"; Path=/; HttpOnly");
}

From source file:cec.easyshop.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDefaultPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);//client side
    cookieGenerator.addCookie(response, "cookie_monster");
    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);//from  ww  w  .  ja  v a 2  s. com
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addCookie(Mockito.argThat(new CookieArgumentMatcher(expectedCookie)));
    assertNoHeaderAdjustments();
}

From source file:org.apache.falcon.resource.admin.AdminResource.java

@GET
@Path("clearuser")
@Produces(MediaType.TEXT_PLAIN)/*from  w ww .j  a  va2 s.c  o  m*/
public String clearUser(@Context HttpServletResponse response) {
    if (!SecurityUtil.isSecurityEnabled()) {
        Cookie cookie = new Cookie("hadoop.auth", null);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        cookie.setSecure(false);
        response.addCookie(cookie);
    } // Else,  Do not checkin User, security is handled via Kerberos.
    return "ok";
}