启用多个身份验证提供程序以确保春季启动安全性

时间:2019-03-04 03:44:36

标签: spring spring-boot spring-security

我具有以下配置,用于在Spring Boot中启用多个身份验证提供程序。但是,仅在运行期间,仅执行活动目录身份验证。我希望针对本地数据库对请求进行身份验证,如果失败,则继续使用AD进行身份验证。有人可以建议我下面的配置有什么问题吗?

package sg.nextmove.emenu.api.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
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.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.Arrays;

/**
 * Created by Admin on 7/11/2018.
 */
@Configuration
@EnableWebSecurity
@ConditionalOnExpression("'${butterfly.app.env}'=='ADC'")
public class WebSecurityConfigurationAD extends WebSecurityConfigurerAdapter {

    @Value("${ad.domain}")
    private String AD_DOMAIN;

    @Value("${ad.url}")
    private String AD_URL;

    @Autowired
    @Qualifier("userDetailsService")
    UserDetailsService userDetailsService;

//    @Autowired
//    @Qualifier("userDetailsService")
//    UserDetailsService userDetailsService;

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

//       http
//         .authorizeRequests()
//             .anyRequest().authenticated();
//
//
//       http
//         .formLogin().failureUrl("/userLogin?error")
//         .defaultSuccessUrl("/")
//         .loginPage("/userLogin")
//         .permitAll()
//         .and()
//         .logout().logoutRequestMatcher(new AntPathRequestMatcher("/userLogout")).logoutSuccessUrl("/userLogin")
//         .permitAll();

//

//       http.authorizeRequests().anyRequest().authenticated().and()
//         .formLogin()
//         .loginPage("/userLogin")
//         .failureUrl("/userLogin?error")
//         .usernameParameter("username")
//         .permitAll()
//         .and()
//         .logout()
//         .logoutUrl("/userLogout")
//         .logoutSuccessUrl("/")
//         .permitAll();


        http.authorizeRequests()
                .antMatchers(HttpMethod.POST, "/api/user/**").anonymous()
                .antMatchers(HttpMethod.POST, "/api/role/**").anonymous()

                .antMatchers(HttpMethod.POST, "/web/login/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('ROLE_EMENU_USER') or hasRole('ROLE_KDS_USER') or hasRole('BUTTERFLY_RAPTOR_USER')")
                .antMatchers(HttpMethod.GET, "/web/login/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('ROLE_EMENU_USER') or hasRole('ROLE_KDS_USER') or hasRole('BUTTERFLY_RAPTOR_USER')")

                .antMatchers(HttpMethod.POST, "/api/login/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('ROLE_EMENU_USER') or hasRole('ROLE_KDS_USER') or hasRole('BUTTERFLY_RAPTOR_USER')")
                .antMatchers(HttpMethod.GET, "/api/login/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('ROLE_EMENU_USER') or hasRole('ROLE_KDS_USER') or hasRole('BUTTERFLY_RAPTOR_USER')")


                .antMatchers(HttpMethod.GET, "/api/posUsers/**").permitAll()

                .antMatchers(HttpMethod.GET, "/web/menus/images/**").permitAll()
                .antMatchers(HttpMethod.GET, "/web/items/images/**").permitAll()

                .antMatchers(HttpMethod.GET, "/web/cmsInfo").permitAll()


//                .antMatchers(HttpMethod.POST, "/web/**").permitAll()
//                .antMatchers(HttpMethod.GET, "/web/**").permitAll()

//                .antMatchers(HttpMethod.POST, "/api/user/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_EMENU_USER')")
//                .antMatchers(HttpMethod.POST, "/api/role/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_EMENU_USER')")

                .antMatchers("/api/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('ROLE_EMENU_USER') or hasRole('ROLE_KDS_USER') or hasRole('BUTTERFLY_RAPTOR_USER')")
                .antMatchers(HttpMethod.POST, "/web/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('BUTTERFLY_RAPTOR_USER')")
                .antMatchers(HttpMethod.GET, "/web/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('BUTTERFLY_RAPTOR_USER')")
                .antMatchers(HttpMethod.PUT, "/web/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('BUTTERFLY_RAPTOR_USER')")
                .antMatchers(HttpMethod.DELETE, "/web/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('BUTTERFLY_RAPTOR_USER')")
                .antMatchers(HttpMethod.PATCH, "/web/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGERS') or hasRole('BUTTERFLY_RAPTOR_USER')")

                .and().logout().logoutSuccessUrl("/")
                .and().httpBasic()
                .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/api/login");

//            formLogin().loginPage("/userLogin");



        http.csrf().disable();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        //authManagerBuilder.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailsService);
        //authManagerBuilder.eraseCredentials(false);
        authManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
        authManagerBuilder.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailsService());
    }

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
    }
    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        CustomActiveDirectoryLdapAuthenticationProvider provider = new CustomActiveDirectoryLdapAuthenticationProvider(AD_DOMAIN, AD_URL);
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);

        return provider;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();

        return encoder;
    }

}

0 个答案:

没有答案
相关问题