代码之家  ›  专栏  ›  技术社区  ›  rodalfus

针对不同端点的多个用户详细信息服务

  •  4
  • rodalfus  · 技术社区  · 8 年前

    我正在使用Spring构建REST API,目前正在使用自定义用户详细信息服务和以下配置代码验证我的所有请求:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
    

    我还在建立一个 DaoAuthenticationProvider 使用“我的用户详细信息”服务并使用该服务配置全局安全。

    现在,我想提供一个端点(虽然仍然使用HTTP基本身份验证进行保护),该端点使用不同的用户详细信息服务来检查是否允许用户访问给定的资源。

    如何为不同的端点使用两种不同的用户详细信息服务?

    2 回复  |  直到 8 年前
        1
  •  10
  •   jzheaux Andrii Pischanski    5 年前

    你能做的一件事就是有两个 WebSecurityConfigurerAdapter s:

    @EnableWebSecurity
    @Order(Ordered.HIGHEST_PRECEDENCE)
    class FirstEndpointConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) {
            http
                .requestMatchers()
                    .antMatchers("/specialendpoint")
                    .and()
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .httpBasic();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) {
            auth.userDetailsService(/* first of your userDetailsServices */);
        }
    }
    
    
    @Configuration
    class SecondEndpointConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) {
            http // all other requests handled here
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .httpBasic();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) {
            auth.userDetailsService(/* second of your userDetailsServices */);
        }
    }
    

    requestMatchers() 存在目标 springSecurityFilterChain 到特定端点。

    编辑 :Mahmoud Odeh提出了一个很好的观点,即如果用户基础相同,那么您可能不需要多个 UserDetailsService 实例。相反,您可以使用一个更改,通过用户帐户上的权限隔离特殊端点:

    http
        .authorizeRequests()
            .antMatchers("/specialendpoint").hasAuthority("SPECIAL")
            .anyRequest().authenticated()
            .and()
        .httpBasic();
    

    那么,你的单身 用户详细信息服务 将查找所有用户。它将包括 SPECIAL GrantedAuthority UserDetails 有权访问的用户的实例 /specialendpoint

        2
  •  2
  •   suraj bahl    6 年前

    我试图遵循M.Deinum给出的解决方案,但在我的情况下,无论执行哪个URL/v3/authorize/login或/v2/authorize/login,它都会转到相同的用户服务(v2userDetailsService)。这是我的代码:

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
    public class SecurityConfiguration {
    
    
      @Configuration
      @Order(2)
      public static class V2Configuration extends WebSecurityConfigurerAdapter {
    
        @Autowired
        @Qualifier("v2userDetailsService")
        private UserDetailsService v2userDetailsService;
    
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
        }
    
        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
          ShaPasswordEncoder passwordEncoder = new ShaPasswordEncoder(256);
          auth
                  .userDetailsService(v2userDetailsService)
                  .passwordEncoder(passwordEncoder);
        }
    
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
          http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).and().csrf().disable().headers()
                  .frameOptions().disable().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                  .authorizeRequests()
                  .antMatchers("/app").permitAll()
                  .antMatchers("/v2/authorize/login").permitAll()
                  .antMatchers("/v2/authorize/reLogin").permitAll()
                  .antMatchers("/v2/authorize/logout").permitAll();
        }
    
      }
    
    
    
      @Configuration
      @Order(1)
      public static class V3Configuration extends WebSecurityConfigurerAdapter {
        @Autowired
        @Qualifier("v3UserDetailsService")
        private UserDetailsService v3UserDetailsService;
    
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
        }
    
        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
          ShaPasswordEncoder passwordEncoder = new ShaPasswordEncoder(256);
          auth
                  .userDetailsService(v3UserDetailsService)
                  .passwordEncoder(passwordEncoder);
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
          http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).and().csrf().disable().headers()
                  .frameOptions().disable().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                  .authorizeRequests()
                              .antMatchers("/v3/authorize/login").permitAll()
                              .antMatchers("/v3/authorize/reLogin").permitAll()
                              .antMatchers("/v3/authorize/logout").permitAll();
        }
    
      }
    }