Search in sources :

Example 1 with RequestMatcher

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

the class SecurityRuleBuilder method addFilterSecurityInterceptor.

private void addFilterSecurityInterceptor(List<Filter> filters, MotechURLSecurityRule securityRule) {
    Map<RequestMatcher, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<>();
    List<AccessDecisionVoter> voters = new ArrayList<>();
    Collection<ConfigAttribute> configAtts = new ArrayList<>();
    if (CollectionUtils.isEmpty(securityRule.getPermissionAccess()) && CollectionUtils.isEmpty(securityRule.getUserAccess())) {
        configAtts.add(new SecurityConfig("IS_AUTHENTICATED_FULLY"));
        AuthenticatedVoter authVoter = new AuthenticatedVoter();
        voters.add(authVoter);
    } else {
        if (!CollectionUtils.isEmpty(securityRule.getPermissionAccess())) {
            for (String permission : securityRule.getPermissionAccess()) {
                configAtts.add(new SecurityConfig(permission));
            }
        }
        if (!CollectionUtils.isEmpty(securityRule.getUserAccess())) {
            for (String userAccess : securityRule.getUserAccess()) {
                configAtts.add(new SecurityConfig(SecurityConfigConstants.USER_ACCESS_PREFIX + userAccess));
            }
        }
    }
    buildRequestMap(requestMap, configAtts, securityRule);
    FilterInvocationSecurityMetadataSource metadataSource = new DefaultFilterInvocationSecurityMetadataSource((LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>) requestMap);
    FilterSecurityInterceptor interceptor = new FilterSecurityInterceptor();
    interceptor.setSecurityMetadataSource(metadataSource);
    RoleVoter roleVoter = new RoleVoter();
    roleVoter.setRolePrefix(SecurityConfigConstants.ROLE_ACCESS_PREFIX);
    voters.add(roleVoter);
    voters.add(new MotechAccessVoter());
    AccessDecisionManager decisionManager = new AffirmativeBased(voters);
    interceptor.setAccessDecisionManager(decisionManager);
    interceptor.setAuthenticationManager(authenticationManager);
    filters.add(interceptor);
}
Also used : RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AnyRequestMatcher(org.springframework.security.web.util.matcher.AnyRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) AccessDecisionManager(org.springframework.security.access.AccessDecisionManager) ConfigAttribute(org.springframework.security.access.ConfigAttribute) FilterSecurityInterceptor(org.springframework.security.web.access.intercept.FilterSecurityInterceptor) ArrayList(java.util.ArrayList) RoleVoter(org.springframework.security.access.vote.RoleVoter) AccessDecisionVoter(org.springframework.security.access.AccessDecisionVoter) DefaultFilterInvocationSecurityMetadataSource(org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource) FilterInvocationSecurityMetadataSource(org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource) LinkedHashMap(java.util.LinkedHashMap) AuthenticatedVoter(org.springframework.security.access.vote.AuthenticatedVoter) SecurityConfig(org.springframework.security.access.SecurityConfig) MotechAccessVoter(org.motechproject.security.authentication.MotechAccessVoter) AffirmativeBased(org.springframework.security.access.vote.AffirmativeBased) Collection(java.util.Collection) DefaultFilterInvocationSecurityMetadataSource(org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource)

Example 2 with RequestMatcher

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

the class SecurityRuleBuilder method buildSecurityChain.

/**
 * Builds SecurityFilterChain which is capable of being
 * matched against HttpServletRequest in order to decide
 * whether it applies to that request
 *
 * @param securityRule that will be used as pattern
 * @param method to be used in filter
 * @return new filter chain with security rule, matcher and filters
 */
public synchronized SecurityFilterChain buildSecurityChain(MotechURLSecurityRule securityRule, HTTPMethod method) {
    LOGGER.info("Building security chain for rule: {} and method: {}", securityRule.getPattern(), method);
    List<Filter> filters = new ArrayList<>();
    RequestMatcher matcher;
    validateRule(securityRule);
    String pattern = securityRule.getPattern();
    if (pattern.equals(SecurityConfigConstants.ANY_PATTERN) || "/**".equals(pattern) || "**".equals(pattern)) {
        matcher = AnyRequestMatcher.INSTANCE;
    } else if (ANY == method) {
        matcher = new AntPathRequestMatcher(pattern);
    } else {
        matcher = new AntPathRequestMatcher(pattern, method.name());
    }
    if (!noSecurity(securityRule)) {
        try {
            filters = addFilters(securityRule);
        } catch (ServletException e) {
            LOGGER.error("Cannot create {} in {} security rule.", SecurityContextHolderAwareRequestFilter.class, securityRule.getPattern(), e);
        }
    }
    LOGGER.info("Built security chain for rule: {} and method: {}", securityRule.getPattern(), method);
    return new MotechSecurityFilterChain(securityRule, matcher, filters);
}
Also used : ServletException(javax.servlet.ServletException) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AnyRequestMatcher(org.springframework.security.web.util.matcher.AnyRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) OpenIDAuthenticationFilter(org.springframework.security.openid.OpenIDAuthenticationFilter) SessionManagementFilter(org.springframework.security.web.session.SessionManagementFilter) Filter(javax.servlet.Filter) ChannelProcessingFilter(org.springframework.security.web.access.channel.ChannelProcessingFilter) ExceptionTranslationFilter(org.springframework.security.web.access.ExceptionTranslationFilter) UsernamePasswordAuthenticationFilter(org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter) SecurityContextPersistenceFilter(org.springframework.security.web.context.SecurityContextPersistenceFilter) LogoutFilter(org.springframework.security.web.authentication.logout.LogoutFilter) AnonymousAuthenticationFilter(org.springframework.security.web.authentication.AnonymousAuthenticationFilter) BasicAuthenticationFilter(org.springframework.security.web.authentication.www.BasicAuthenticationFilter) SecurityContextHolderAwareRequestFilter(org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter) RequestCacheAwareFilter(org.springframework.security.web.savedrequest.RequestCacheAwareFilter) ArrayList(java.util.ArrayList) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) SecurityContextHolderAwareRequestFilter(org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter) MotechSecurityFilterChain(org.motechproject.security.chain.MotechSecurityFilterChain)

Example 3 with RequestMatcher

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

the class SecurityRuleBuilder method buildRequestMap.

private void buildRequestMap(Map<RequestMatcher, Collection<ConfigAttribute>> requestMap, Collection<ConfigAttribute> configAtts, MotechURLSecurityRule securityRule) {
    String pattern = securityRule.getPattern();
    for (HTTPMethod method : securityRule.getMethodsRequired()) {
        RequestMatcher matcher;
        if (securityRule.getMethodsRequired().contains(ANY) && (pattern.equals(SecurityConfigConstants.ANY_PATTERN) || "/**".equals(pattern))) {
            matcher = AnyRequestMatcher.INSTANCE;
        } else if (securityRule.getMethodsRequired().contains(ANY)) {
            matcher = new AntPathRequestMatcher(pattern, null);
        } else {
            matcher = new AntPathRequestMatcher(pattern, method.name());
        }
        requestMap.put(matcher, configAtts);
    }
}
Also used : RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AnyRequestMatcher(org.springframework.security.web.util.matcher.AnyRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) HTTPMethod(org.motechproject.security.constants.HTTPMethod) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher)

Example 4 with RequestMatcher

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

the class SecurityRuleBuilder method addSecureChannel.

private void addSecureChannel(List<Filter> filters, Protocol protocol) {
    ChannelProcessingFilter channelProcessingFilter = new ChannelProcessingFilter();
    channelProcessingFilter.setChannelDecisionManager(channelDecisionManager);
    RequestMatcher anyRequest = AnyRequestMatcher.INSTANCE;
    LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<>();
    Collection<ConfigAttribute> configAtts = new ArrayList<>();
    switch(protocol) {
        case HTTP:
            configAtts.add(new SecurityConfig("ANY_CHANNEL"));
            break;
        case HTTPS:
            configAtts.add(new SecurityConfig("REQUIRES_SECURE_CHANNEL"));
            break;
        default:
    }
    requestMap.put(anyRequest, configAtts);
    FilterInvocationSecurityMetadataSource securityMetadataSource = new DefaultFilterInvocationSecurityMetadataSource(requestMap);
    channelProcessingFilter.setSecurityMetadataSource(securityMetadataSource);
    filters.add(channelProcessingFilter);
}
Also used : ChannelProcessingFilter(org.springframework.security.web.access.channel.ChannelProcessingFilter) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AnyRequestMatcher(org.springframework.security.web.util.matcher.AnyRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) ConfigAttribute(org.springframework.security.access.ConfigAttribute) SecurityConfig(org.springframework.security.access.SecurityConfig) ArrayList(java.util.ArrayList) Collection(java.util.Collection) DefaultFilterInvocationSecurityMetadataSource(org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource) DefaultFilterInvocationSecurityMetadataSource(org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource) FilterInvocationSecurityMetadataSource(org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with RequestMatcher

use of org.springframework.security.web.util.matcher.RequestMatcher in project ma-core-public by infiniteautomation.

the class MangoSecurityConfiguration method createBrowserHtmlRequestMatcher.

/**
 * Internal method to create a static matcher
 * @return
 */
private static RequestMatcher createBrowserHtmlRequestMatcher() {
    ContentNegotiationStrategy contentNegotiationStrategy = contentNegotiationStrategy();
    MediaTypeRequestMatcher mediaMatcher = new MediaTypeRequestMatcher(contentNegotiationStrategy, MediaType.APPLICATION_XHTML_XML, MediaType.TEXT_HTML);
    mediaMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
    RequestMatcher notXRequestedWith = new NegatedRequestMatcher(new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"));
    return new AndRequestMatcher(Arrays.asList(notXRequestedWith, mediaMatcher));
}
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) AndRequestMatcher(org.springframework.security.web.util.matcher.AndRequestMatcher) NegatedRequestMatcher(org.springframework.security.web.util.matcher.NegatedRequestMatcher) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) ContentNegotiationStrategy(org.springframework.web.accept.ContentNegotiationStrategy) RequestHeaderRequestMatcher(org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher) AndRequestMatcher(org.springframework.security.web.util.matcher.AndRequestMatcher)

Aggregations

RequestMatcher (org.springframework.security.web.util.matcher.RequestMatcher)81 Test (org.junit.jupiter.api.Test)40 AntPathRequestMatcher (org.springframework.security.web.util.matcher.AntPathRequestMatcher)24 LinkedHashMap (java.util.LinkedHashMap)15 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)10 AuthenticationEntryPoint (org.springframework.security.web.AuthenticationEntryPoint)10 MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)9 OrRequestMatcher (org.springframework.security.web.util.matcher.OrRequestMatcher)9 Collection (java.util.Collection)8 ConfigAttribute (org.springframework.security.access.ConfigAttribute)7 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 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5