Пример #1
0
  @RequestMapping(value = "login", method = RequestMethod.POST)
  ResponseEntity<PageDto> login(
      @Validated @RequestBody LoginDto loginDto,
      HttpServletRequest request,
      HttpServletResponse response) {

    PageDto pageDto = userService.login(loginDto);

    if (pageDto.getHeaderDto().isAuth()) {
      CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
      if (csrf != null) {
        Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
        String token = csrf.getToken();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if ((cookie == null || token != null && !token.equals(cookie.getValue()))
            && (authentication != null && authentication.isAuthenticated())) {
          cookie = new Cookie("XSRF-TOKEN", token);
          cookie.setPath("/");
          response.addCookie(cookie);
        }
      }
      return new ResponseEntity<>(pageDto, null, HttpStatus.OK);
    } else {
      return new ResponseEntity<>(pageDto, null, HttpStatus.UNAUTHORIZED);
    }
  }
 private Integer getMillisOffset(HttpServletRequest request) {
   Cookie cookie = WebUtils.getCookie(request, "Greenhouse.timeZoneOffset");
   if (cookie != null) {
     return Integer.valueOf(cookie.getValue());
   } else {
     return null;
   }
 }
Пример #3
0
 private boolean supportWebp(HttpServletRequest request, String name) {
   Cookie cookie = WebUtils.getCookie(request, WEBP_SUPPORT_COOKIE);
   if (cookie != null && "true".equalsIgnoreCase(cookie.getValue())) {
     String ext = Files.getFileExtension(name);
     return "jpg".equalsIgnoreCase(ext)
         || "jpeg".equalsIgnoreCase(ext)
         || "png".equalsIgnoreCase(ext);
   }
   return false;
 }
Пример #4
0
 @Override
 protected void doFilterInternal(
     HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
     throws ServletException, IOException {
   CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
   if (csrf != null) {
     Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
     String token = csrf.getToken();
     if (cookie == null || token != null && !token.equals(cookie.getValue())) {
       cookie = new Cookie("XSRF-TOKEN", token);
       cookie.setPath("/");
       response.addCookie(cookie);
     }
   }
   filterChain.doFilter(request, response);
 }
  private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
      // Retrieve and parse cookie value.
      Cookie cookie = WebUtils.getCookie(request, getCookieName());
      Locale locale = null;
      TimeZone timeZone = null;
      if (cookie != null) {
        String value = cookie.getValue();

        // Remove the double quote
        value = StringUtils.replace(value, "%22", "");

        String localePart = value;
        String timeZonePart = null;
        int spaceIndex = localePart.indexOf(' ');
        if (spaceIndex != -1) {
          localePart = value.substring(0, spaceIndex);
          timeZonePart = value.substring(spaceIndex + 1);
        }
        locale =
            (!"-".equals(localePart)
                ? StringUtils.parseLocaleString(localePart.replace('-', '_'))
                : null);
        if (timeZonePart != null) {
          timeZone = StringUtils.parseTimeZoneString(timeZonePart);
        }
        if (logger.isTraceEnabled()) {
          logger.trace(
              "Parsed cookie value ["
                  + cookie.getValue()
                  + "] into locale '"
                  + locale
                  + "'"
                  + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
        }
      }
      request.setAttribute(
          LOCALE_REQUEST_ATTRIBUTE_NAME,
          (locale != null ? locale : determineDefaultLocale(request)));

      request.setAttribute(
          TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
          (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
  }
Пример #6
0
  @Override
  protected void doFilterInternal(
      HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {

    final CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());

    if (csrf != null) {

      Cookie cookie = WebUtils.getCookie(request, CookieBlog.CSRF_NAME);

      final String token = csrf.getToken();
      if ((cookie == null) || ((token != null) && (token.equals(cookie.getValue()) == false))) {

        cookie = new Cookie(CookieBlog.CSRF_NAME, token);
        cookie.setPath(COOKIE_PATH);
        response.addCookie(cookie);
      }
    }

    filterChain.doFilter(request, response);
  }
Пример #7
0
  public Locale resolveLocale(HttpServletRequest request) {
    // Check request for pre-parsed or preset locale.
    Locale locale = (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
    if (locale != null) {
      return locale;
    }

    // Retrieve and parse cookie value.
    Cookie cookie = WebUtils.getCookie(request, getCookieName());
    if (cookie != null) {
      locale = StringUtils.parseLocaleString(cookie.getValue());
      if (logger.isDebugEnabled()) {
        logger.debug(
            "Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale + "'");
      }
      if (locale != null) {
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale);
        return locale;
      }
    }

    return determineDefaultLocale(request);
  }
Пример #8
0
 /**
  * Gets the cookie associated to this request given the cookie's name.
  *
  * @param cookieName name of cookie which wish be retrieved
  * @return Cookie belonging this request
  */
 public Cookie getCookie(String cookieName) {
   return WebUtils.getCookie(getHttpServletRequest(), cookieName);
 }