Search in sources :

Example 1 with SecurityContextHolderFilter

use of org.springframework.security.web.context.SecurityContextHolderFilter in project spring-security by spring-projects.

the class SecurityContextConfigurer method configure.

@Override
@SuppressWarnings("unchecked")
public void configure(H http) {
    SecurityContextRepository securityContextRepository = getSecurityContextRepository();
    if (this.requireExplicitSave) {
        SecurityContextHolderFilter securityContextHolderFilter = postProcess(new SecurityContextHolderFilter(securityContextRepository));
        http.addFilter(securityContextHolderFilter);
    } else {
        SecurityContextPersistenceFilter securityContextFilter = new SecurityContextPersistenceFilter(securityContextRepository);
        SessionManagementConfigurer<?> sessionManagement = http.getConfigurer(SessionManagementConfigurer.class);
        SessionCreationPolicy sessionCreationPolicy = (sessionManagement != null) ? sessionManagement.getSessionCreationPolicy() : null;
        if (SessionCreationPolicy.ALWAYS == sessionCreationPolicy) {
            securityContextFilter.setForceEagerSessionCreation(true);
            http.addFilter(postProcess(new ForceEagerSessionCreationFilter()));
        }
        securityContextFilter = postProcess(securityContextFilter);
        http.addFilter(securityContextFilter);
    }
}
Also used : SessionCreationPolicy(org.springframework.security.config.http.SessionCreationPolicy) ForceEagerSessionCreationFilter(org.springframework.security.web.session.ForceEagerSessionCreationFilter) SecurityContextRepository(org.springframework.security.web.context.SecurityContextRepository) HttpSessionSecurityContextRepository(org.springframework.security.web.context.HttpSessionSecurityContextRepository) SecurityContextHolderFilter(org.springframework.security.web.context.SecurityContextHolderFilter) SecurityContextPersistenceFilter(org.springframework.security.web.context.SecurityContextPersistenceFilter)

Example 2 with SecurityContextHolderFilter

use of org.springframework.security.web.context.SecurityContextHolderFilter in project spring-security by spring-projects.

the class WebTestUtils method setSecurityContextRepository.

/**
 * Sets the {@link SecurityContextRepository} for the specified
 * {@link HttpServletRequest}.
 * @param request the {@link HttpServletRequest} to obtain the
 * {@link SecurityContextRepository}
 * @param securityContextRepository the {@link SecurityContextRepository} to set
 */
public static void setSecurityContextRepository(HttpServletRequest request, SecurityContextRepository securityContextRepository) {
    SecurityContextPersistenceFilter filter = findFilter(request, SecurityContextPersistenceFilter.class);
    if (filter != null) {
        ReflectionTestUtils.setField(filter, "repo", securityContextRepository);
    }
    SecurityContextHolderFilter holderFilter = findFilter(request, SecurityContextHolderFilter.class);
    if (holderFilter != null) {
        ReflectionTestUtils.setField(holderFilter, "securityContextRepository", securityContextRepository);
    }
}
Also used : SecurityContextHolderFilter(org.springframework.security.web.context.SecurityContextHolderFilter) SecurityContextPersistenceFilter(org.springframework.security.web.context.SecurityContextPersistenceFilter)

Example 3 with SecurityContextHolderFilter

use of org.springframework.security.web.context.SecurityContextHolderFilter in project spring-security by spring-projects.

the class WebTestUtils method getSecurityContextRepository.

/**
 * Gets the {@link SecurityContextRepository} for the specified
 * {@link HttpServletRequest}. If one is not found, a default
 * {@link HttpSessionSecurityContextRepository} is used.
 * @param request the {@link HttpServletRequest} to obtain the
 * {@link SecurityContextRepository}
 * @return the {@link SecurityContextRepository} for the specified
 * {@link HttpServletRequest}
 */
public static SecurityContextRepository getSecurityContextRepository(HttpServletRequest request) {
    SecurityContextPersistenceFilter filter = findFilter(request, SecurityContextPersistenceFilter.class);
    if (filter != null) {
        return (SecurityContextRepository) ReflectionTestUtils.getField(filter, "repo");
    }
    SecurityContextHolderFilter holderFilter = findFilter(request, SecurityContextHolderFilter.class);
    if (holderFilter != null) {
        return (SecurityContextRepository) ReflectionTestUtils.getField(holderFilter, "securityContextRepository");
    }
    return DEFAULT_CONTEXT_REPO;
}
Also used : HttpSessionSecurityContextRepository(org.springframework.security.web.context.HttpSessionSecurityContextRepository) SecurityContextRepository(org.springframework.security.web.context.SecurityContextRepository) SecurityContextHolderFilter(org.springframework.security.web.context.SecurityContextHolderFilter) SecurityContextPersistenceFilter(org.springframework.security.web.context.SecurityContextPersistenceFilter)

Aggregations

SecurityContextHolderFilter (org.springframework.security.web.context.SecurityContextHolderFilter)3 SecurityContextPersistenceFilter (org.springframework.security.web.context.SecurityContextPersistenceFilter)3 HttpSessionSecurityContextRepository (org.springframework.security.web.context.HttpSessionSecurityContextRepository)2 SecurityContextRepository (org.springframework.security.web.context.SecurityContextRepository)2 SessionCreationPolicy (org.springframework.security.config.http.SessionCreationPolicy)1 ForceEagerSessionCreationFilter (org.springframework.security.web.session.ForceEagerSessionCreationFilter)1