Spring Boot多重身份验证

时间:2019-10-07 10:33:53

标签: spring spring-boot authentication

我已经完成了具有Spring Security集成和自定义身份验证提供程序的Spring Boot应用程序。现在,我希望某些链接将使用自定义Authent提供程序,而另一些将使用内存身份验证。我该怎么办?

2 个答案:

答案 0 :(得分:1)

您是对的。这是我的SecurityConfig类。现在,我想通过基本身份验证来保护“ / api / **”之类的路径。

@Autowired
private CustomAuthenticationProvider authProvider;

@Autowired
DataSource dataSource;

@Autowired
CustomLogoutSuccessHandler  customLogoutSuccessHandler;

@Autowired
CustomAuthenticationSuccessHandler customAutheincationSuccessHandler;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(authProvider);
}

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

    http.authorizeRequests()
        .antMatchers("/list/**").hasAnyAuthority("Administrator","Operator")
        .antMatchers("/api/**").permitAll()
        .and()
        .formLogin()
        .loginPage("/login")
        .loginProcessingUrl("/authenticateTheUser")
        .successHandler(customAutheincationSuccessHandler)
        .and().logout()
        .logoutUrl("/logout")
        .logoutSuccessHandler(customLogoutSuccessHandler)
        .logoutSuccessUrl("/login").and().exceptionHandling().accessDeniedPage("/accessDenied")
        .and()
        .csrf().disable();
}

答案 1 :(得分:0)

您可以创建两个具有不同路径的spring安全配置。在第一种配置中,您可以排除/ api / **。但是第二种配置将可以对其进行检查。

相关问题