All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.session.security.web.authentication.SpringSessionRememberMeServicesCustom Maven / Gradle / Ivy

package org.springframework.session.security.web.authentication;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.StringUtils;

public class SpringSessionRememberMeServicesCustom extends SpringSessionRememberMeServices {
  private Map include = new LinkedCaseInsensitiveMap();

  @Override
  protected boolean rememberMeRequested(HttpServletRequest request, String parameter) {
    if (super.rememberMeRequested(request, parameter)) {
      return true;
    }
    if (CollectionUtils.isEmpty(this.include)) {
      return false;
    }
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
      for (Cookie cookie : cookies) {
        String sourceValue = this.include.get(cookie.getName());
        String targetValue = cookie.getValue();
        if (StringUtils.hasText(sourceValue) && StringUtils.hasText(targetValue) && sourceValue.equalsIgnoreCase(targetValue)) {
          return true;
        }
      }
    }
    Enumeration headerNames = request.getHeaderNames();
    if (headerNames != null) {
      while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        String sourceValue = this.include.get(headerName);
        String targetValue = request.getHeader(headerName);
        if (StringUtils.hasText(sourceValue) && StringUtils.hasText(targetValue) && sourceValue.equalsIgnoreCase(targetValue)) {
          return true;
        }
      }
    }
    Enumeration parameterNames = request.getParameterNames();
    if (parameterNames != null) {
      while (parameterNames.hasMoreElements()) {
        String parameterName = parameterNames.nextElement();
        String sourceValue = this.include.get(parameterName);
        String targetValue = request.getParameter(parameterName);
        if (StringUtils.hasText(sourceValue) && StringUtils.hasText(targetValue) && sourceValue.equalsIgnoreCase(targetValue)) {
          return true;
        }
      }
    }
    return false;
  }

  @Override
  public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    if (authentication != null) {
      super.logout(request, response, authentication);
    }
  }

  public Map getInclude() {
    return include;
  }

  public void setInclude(Map include) {
    this.include = include;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy