Search in sources :

Example 61 with RequestMatcher

use of org.springframework.security.web.util.matcher.RequestMatcher in project CzechIdMng by bcvsolutions.

the class AuthenticationFilter method doFilter.

/**
 * Authentication flow implementation.
 */
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = HttpFilterUtils.asHttp(req);
    // 
    boolean isPublicPath = false;
    if (getPublicPathRequestMatchers().stream().anyMatch(requestMatcher -> requestMatcher.matches(request))) {
        LOG.debug("Authentication filter will be optional for public path [{}].", request.getServletPath());
        isPublicPath = true;
    }
    // 
    HttpServletResponse response = HttpFilterUtils.asHttp(res);
    try {
        filters.stream().filter(f -> enabledEvaluator.isEnabled(f)).filter(f -> !f.isDisabled()).filter(f -> isAuthenticated() || res.isCommitted() || f.authorize(request, response) || handleAuthenticationHeader(request, response, f)).findFirst();
    } catch (Exception ex) {
        if (!isPublicPath) {
            // not public => authentication is required
            throw ex;
        }
        // public path => authentication is optional
        LOG.debug("Exception is occured by authentication filters on public page, authentication will not be set.", ex);
    }
    if (!res.isCommitted()) {
        chain.doFilter(req, res);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PublicController(eu.bcvsolutions.idm.core.api.rest.PublicController) FilterChain(javax.servlet.FilterChain) AopUtils(org.springframework.aop.support.AopUtils) ServletException(javax.servlet.ServletException) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) StringUtils(org.apache.commons.lang3.StringUtils) SecurityService(eu.bcvsolutions.idm.core.security.api.service.SecurityService) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) GenericFilterBean(org.springframework.web.filter.GenericFilterBean) Method(java.lang.reflect.Method) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) IOException(java.io.IOException) EnabledEvaluator(eu.bcvsolutions.idm.core.security.api.service.EnabledEvaluator) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) IdmAuthenticationFilter(eu.bcvsolutions.idm.core.security.api.filter.IdmAuthenticationFilter) Sets(com.google.common.collect.Sets) List(java.util.List) ServletResponse(javax.servlet.ServletResponse) BaseDtoController(eu.bcvsolutions.idm.core.api.rest.BaseDtoController) Optional(java.util.Optional) Lazy(org.springframework.context.annotation.Lazy) Collections(java.util.Collections) HttpFilterUtils(eu.bcvsolutions.idm.core.api.utils.HttpFilterUtils) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 62 with RequestMatcher

use of org.springframework.security.web.util.matcher.RequestMatcher in project flow by vaadin.

the class VaadinWebSecurityConfigurerAdapter method createAccessDeniedHandler.

private AccessDeniedHandler createAccessDeniedHandler() {
    final AccessDeniedHandler defaultHandler = new AccessDeniedHandlerImpl();
    final AccessDeniedHandler http401UnauthorizedHandler = new Http401UnauthorizedAccessDeniedHandler();
    final LinkedHashMap<Class<? extends AccessDeniedException>, AccessDeniedHandler> exceptionHandlers = new LinkedHashMap<>();
    exceptionHandlers.put(CsrfException.class, http401UnauthorizedHandler);
    final LinkedHashMap<RequestMatcher, AccessDeniedHandler> matcherHandlers = new LinkedHashMap<>();
    matcherHandlers.put(requestUtil::isEndpointRequest, new DelegatingAccessDeniedHandler(exceptionHandlers, new AccessDeniedHandlerImpl()));
    return new RequestMatcherDelegatingAccessDeniedHandler(matcherHandlers, defaultHandler);
}
Also used : AccessDeniedHandlerImpl(org.springframework.security.web.access.AccessDeniedHandlerImpl) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) OrRequestMatcher(org.springframework.security.web.util.matcher.OrRequestMatcher) AnyRequestMatcher(org.springframework.security.web.util.matcher.AnyRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) AccessDeniedException(org.springframework.security.access.AccessDeniedException) DelegatingAccessDeniedHandler(org.springframework.security.web.access.DelegatingAccessDeniedHandler) AccessDeniedHandler(org.springframework.security.web.access.AccessDeniedHandler) RequestMatcherDelegatingAccessDeniedHandler(org.springframework.security.web.access.RequestMatcherDelegatingAccessDeniedHandler) DelegatingAccessDeniedHandler(org.springframework.security.web.access.DelegatingAccessDeniedHandler) RequestMatcherDelegatingAccessDeniedHandler(org.springframework.security.web.access.RequestMatcherDelegatingAccessDeniedHandler) RequestMatcherDelegatingAccessDeniedHandler(org.springframework.security.web.access.RequestMatcherDelegatingAccessDeniedHandler) LinkedHashMap(java.util.LinkedHashMap)

Example 63 with RequestMatcher

use of org.springframework.security.web.util.matcher.RequestMatcher in project BroadleafCommerce by BroadleafCommerce.

the class CsrfFilter method doFilter.

@Override
public void doFilter(ServletRequest baseRequest, ServletResponse baseResponse, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) baseRequest;
    HttpServletResponse response = (HttpServletResponse) baseResponse;
    boolean excludedRequestFound = false;
    if (excludedRequestPatterns != null && excludedRequestPatterns.size() > 0) {
        for (String pattern : excludedRequestPatterns) {
            RequestMatcher matcher = new AntPathRequestMatcher(pattern);
            if (matcher.matches(request)) {
                excludedRequestFound = true;
                break;
            }
        }
    }
    // We only validate CSRF tokens on POST
    if (request.getMethod().equals("POST") && !excludedRequestFound) {
        String requestToken = request.getParameter(exploitProtectionService.getCsrfTokenParameter());
        try {
            exploitProtectionService.compareToken(requestToken);
        } catch (ServiceException e) {
            throw new ServletException(e);
        }
    }
    chain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) ServiceException(org.broadleafcommerce.common.exception.ServiceException) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 64 with RequestMatcher

use of org.springframework.security.web.util.matcher.RequestMatcher in project BroadleafCommerce by BroadleafCommerce.

the class SecurityFilter method doFilter.

@Override
public void doFilter(ServletRequest baseRequest, ServletResponse baseResponse, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) baseRequest;
    HttpServletResponse response = (HttpServletResponse) baseResponse;
    boolean excludedRequestFound = false;
    if (excludedRequestPatterns != null && excludedRequestPatterns.size() > 0) {
        for (String pattern : excludedRequestPatterns) {
            RequestMatcher matcher = new AntPathRequestMatcher(pattern);
            if (matcher.matches(request)) {
                excludedRequestFound = true;
                break;
            }
        }
    }
    // We only validate CSRF tokens on POST
    if (request.getMethod().equals("POST") && !excludedRequestFound) {
        String requestToken = request.getParameter(exploitProtectionService.getCsrfTokenParameter());
        try {
            exploitProtectionService.compareToken(requestToken);
        } catch (ServiceException e) {
            throw new ServletException(e);
        }
    }
    if (staleStateProtectionService.isEnabled()) {
        // Catch attempts to update form data from a stale page (i.e. a important state change has taken place for this session)
        if (request.getMethod().equals("POST") && !excludedRequestFound) {
            String requestToken = request.getParameter(staleStateProtectionService.getStateVersionTokenParameter());
            try {
                staleStateProtectionService.compareToken(requestToken);
            } catch (StaleStateServiceException e) {
                throw new ServletException(e);
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) StaleStateServiceException(org.broadleafcommerce.common.security.service.StaleStateServiceException) ServiceException(org.broadleafcommerce.common.exception.ServiceException) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaleStateServiceException(org.broadleafcommerce.common.security.service.StaleStateServiceException)

Example 65 with RequestMatcher

use of org.springframework.security.web.util.matcher.RequestMatcher in project spring-security by spring-projects.

the class OAuth2LoginConfigurer method getLoginEntryPoint.

private AuthenticationEntryPoint getLoginEntryPoint(B http, String providerLoginPage) {
    RequestMatcher loginPageMatcher = new AntPathRequestMatcher(this.getLoginPage());
    RequestMatcher faviconMatcher = new AntPathRequestMatcher("/favicon.ico");
    RequestMatcher defaultEntryPointMatcher = this.getAuthenticationEntryPointMatcher(http);
    RequestMatcher defaultLoginPageMatcher = new AndRequestMatcher(new OrRequestMatcher(loginPageMatcher, faviconMatcher), defaultEntryPointMatcher);
    RequestMatcher notXRequestedWith = new NegatedRequestMatcher(new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"));
    LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints = new LinkedHashMap<>();
    entryPoints.put(new AndRequestMatcher(notXRequestedWith, new NegatedRequestMatcher(defaultLoginPageMatcher)), new LoginUrlAuthenticationEntryPoint(providerLoginPage));
    DelegatingAuthenticationEntryPoint loginEntryPoint = new DelegatingAuthenticationEntryPoint(entryPoints);
    loginEntryPoint.setDefaultEntryPoint(this.getAuthenticationEntryPoint());
    return loginEntryPoint;
}
Also used : NegatedRequestMatcher(org.springframework.security.web.util.matcher.NegatedRequestMatcher) RequestHeaderRequestMatcher(org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) OrRequestMatcher(org.springframework.security.web.util.matcher.OrRequestMatcher) AndRequestMatcher(org.springframework.security.web.util.matcher.AndRequestMatcher) NegatedRequestMatcher(org.springframework.security.web.util.matcher.NegatedRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) LoginUrlAuthenticationEntryPoint(org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint) DelegatingAuthenticationEntryPoint(org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint) AuthenticationEntryPoint(org.springframework.security.web.AuthenticationEntryPoint) RequestHeaderRequestMatcher(org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher) DelegatingAuthenticationEntryPoint(org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint) AndRequestMatcher(org.springframework.security.web.util.matcher.AndRequestMatcher) OrRequestMatcher(org.springframework.security.web.util.matcher.OrRequestMatcher) LoginUrlAuthenticationEntryPoint(org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

RequestMatcher (org.springframework.security.web.util.matcher.RequestMatcher)85 Test (org.junit.jupiter.api.Test)40 AntPathRequestMatcher (org.springframework.security.web.util.matcher.AntPathRequestMatcher)27 LinkedHashMap (java.util.LinkedHashMap)16 AndRequestMatcher (org.springframework.security.web.util.matcher.AndRequestMatcher)14 NegatedRequestMatcher (org.springframework.security.web.util.matcher.NegatedRequestMatcher)12 RequestHeaderRequestMatcher (org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher)12 ArrayList (java.util.ArrayList)11 AuthenticationEntryPoint (org.springframework.security.web.AuthenticationEntryPoint)10 OrRequestMatcher (org.springframework.security.web.util.matcher.OrRequestMatcher)10 MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)9 Collection (java.util.Collection)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 ConfigAttribute (org.springframework.security.access.ConfigAttribute)7 AnyRequestMatcher (org.springframework.security.web.util.matcher.AnyRequestMatcher)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 DelegatingAuthenticationEntryPoint (org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint)6 ContentNegotiationStrategy (org.springframework.web.accept.ContentNegotiationStrategy)6 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)6