Spring Security с фильтрами «Базовая аутентификация» и SiteMinder - PullRequest
0 голосов
/ 13 сентября 2018

У меня есть веб-приложение Spring Boot, которое обслуживает как веб-контент, так и предоставляет REST Services.Я хочу защитить службы REST с помощью «Базовой аутентификации» и веб-контент с помощью SiteMinder.Я использую Spring Security версии 4.2.3.

Моя проблема в том, что я вижу ошибку:

[13.09.18 16: 11: 20: 711 EDT] 000000bcSystemOut O 2018-09-13 16: 11: 20.710 ОШИБКА 19260 --- [ebContainer: 1] osboot.web.support.ErrorPageFilter: перенаправление на страницу ошибки из запроса [/ services / ews / meetinglocations] из-за исключения [заголовок SM_USERне найден в запросе.]

Как при попытке вызвать службу REST из PostMan, так и при попытке загрузить веб-контент из веб-браузера. Я ожидаю увидеть ошибку при загрузке содержимого из веб-браузера, поскольку у меня еще не настроен SiteMinder, но почему я не могу вызвать службу REST?

Вот класс WebSecurityConfigurerAdapter:

package eacmanager.security;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
import org.springframework.stereotype.Component;

import eacmanager.utility.ApplicationConstants;

@Configuration
@Component
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    private static final Logger LOGGER = Logger.getLogger(CustomWebSecurityConfigurerAdapter.class.getName());

    private PreAuthenticatedAuthenticationProvider preAuthenticatedProvider;


    public CustomWebSecurityConfigurerAdapter() {
        super();

        UserDetailsService userDetailsService = new CustomUserDetailsService();
        UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> wrapper = 
                new UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken>(userDetailsService);

        preAuthenticatedProvider = new PreAuthenticatedAuthenticationProvider();
        preAuthenticatedProvider.setPreAuthenticatedUserDetailsService(wrapper);
    }


    @Override
    protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.authenticationProvider(preAuthenticatedProvider);
    }


    @Override
    public void configure(WebSecurity webSecurity) throws Exception {
        webSecurity.ignoring().antMatchers("/resources/**", "/assets/**", "/eacmanager/error.html");
    }


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        LOGGER.log(Level.INFO, "Creating in memory Authentication");
        auth.inMemoryAuthentication()
          .withUser("user").password("pass")
          .roles("USER");
    }


//  Use of multiple URLs to protect:  https://stackoverflow.com/questions/33037559/spring-rest-security-secure-different-urls-differently

    @Configuration
    @Order(1)
    public static class WebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(HttpSecurity http) throws Exception {

            LOGGER.info("Creating SiteMinder filter 4");

            RequestHeaderAuthenticationFilter siteMinderFilter = new RequestHeaderAuthenticationFilter();
            siteMinderFilter.setAuthenticationManager(authenticationManager());

            http
                .addFilter(siteMinderFilter)
                .authorizeRequests()
                .antMatchers("/eacmanager/**")
                .permitAll()
                .anyRequest()
                .hasRole(ApplicationConstants.SITE_MINDER_AUTHORITY);
        }
    }


    @Configuration
    @Order(2)
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Autowired
        private EACManagerBasicAuthenticationEntryPoint authenticationEntryPoint;

        @Override
        protected void configure(HttpSecurity http) throws Exception {

            LOGGER.info("Creating REST Services filter");

            http
              .requestMatcher(new NegatedRequestMatcher(new AntPathRequestMatcher("/eacmanager/**")))
              .authorizeRequests()                      
              .antMatchers("/eacmanager/services/**")   
              .permitAll()                              
              .anyRequest()                             
              .authenticated()                          
              .and()
              .httpBasic()                              
              .realmName(ApplicationConstants.SITE_REALM_NAME)
              .authenticationEntryPoint(authenticationEntryPoint); 
        }
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}

Если я поменяю порядок двух фильтров, я могу вызвать службу REST с помощью «Базовой аутентификации», но веб-контент будет обслуживаться БЕЗ ошибка SiteMinder.Я должен получить ошибку отсутствия заголовка SiteMinder.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...