使用具有不同AuthenticationProviders的多个WebSecurityConfigurerAdapter的Spring启动安全性

时间:2018-01-17 11:39:20

标签: java spring spring-boot spring-security

我正在尝试根据某些http请求实现不同的身份验证,但无论请求是什么,它们都会使用错误的身份验证。更具体地说,对于任何 manage 端点,我只想使用身份验证A和任何 内部 api 端点我只想使用身份验证B.

例如,对于当前代码,如果我发送无效的身份验证/用户进行管理,它将首先尝试使用身份验证A,然后失败,然后尝试使用身份验证B(不应该)。

管理:

private resolver: ComponentFactoryResolver

var factories = Array.from(this.resolver['_factories'].keys());
var factoryClass = <Type<any>>factories.find((x: any) => x.name === this.comp);
const factory = this.resolver.resolveComponentFactory(factoryClass);
const compRef = this.vcRef.createComponent(factory);

对于api和内部:

@Configuration
@Order(1)
@EnableWebSecurity
public class SecurityConfigA extends WebSecurityConfigurerAdapter { 


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/manage/**")
                .authenticated().anyRequest().permitAll().and().httpBasic();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) { 

        auth.authenticationProvider(new AuthenticationProvider() {

            @Override
            public boolean supports(Class<?> arg0) {
                return true;
            }

            @Override
            public Authentication authenticate(Authentication auth) {

                //do authentication A
            }
        });   
    }
}

0 个答案:

没有答案