Search in sources :

Example 1 with MotechSecurityFilterChain

use of org.motechproject.security.chain.MotechSecurityFilterChain 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)

Aggregations

ArrayList (java.util.ArrayList)1 Filter (javax.servlet.Filter)1 ServletException (javax.servlet.ServletException)1 MotechSecurityFilterChain (org.motechproject.security.chain.MotechSecurityFilterChain)1 OpenIDAuthenticationFilter (org.springframework.security.openid.OpenIDAuthenticationFilter)1 ExceptionTranslationFilter (org.springframework.security.web.access.ExceptionTranslationFilter)1 ChannelProcessingFilter (org.springframework.security.web.access.channel.ChannelProcessingFilter)1 AnonymousAuthenticationFilter (org.springframework.security.web.authentication.AnonymousAuthenticationFilter)1 UsernamePasswordAuthenticationFilter (org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter)1 LogoutFilter (org.springframework.security.web.authentication.logout.LogoutFilter)1 BasicAuthenticationFilter (org.springframework.security.web.authentication.www.BasicAuthenticationFilter)1 SecurityContextPersistenceFilter (org.springframework.security.web.context.SecurityContextPersistenceFilter)1 RequestCacheAwareFilter (org.springframework.security.web.savedrequest.RequestCacheAwareFilter)1 SecurityContextHolderAwareRequestFilter (org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter)1 SessionManagementFilter (org.springframework.security.web.session.SessionManagementFilter)1 AntPathRequestMatcher (org.springframework.security.web.util.matcher.AntPathRequestMatcher)1 AnyRequestMatcher (org.springframework.security.web.util.matcher.AnyRequestMatcher)1 RequestMatcher (org.springframework.security.web.util.matcher.RequestMatcher)1