Authentication 在同一springboot应用程序上实现http basic和forms登录

Authentication 在同一springboot应用程序上实现http basic和forms登录,authentication,spring-boot,Authentication,Spring Boot,我想为路径“/api/”实现http基本身份验证,并为springboot应用程序的路径“/”和“/admin”实现表单身份验证 这是我当前的java配置代码,但它不工作,有什么想法吗?=) 此代码使所有站点都使用http basic进行安全保护,而不仅仅是“/api”。我在stackoverflow中发现了一些问题,但它们似乎无法解决我的问题: public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

我想为路径“/api/”实现http基本身份验证,并为springboot应用程序的路径“/”和“/admin”实现表单身份验证

这是我当前的java配置代码,但它不工作,有什么想法吗?=) 此代码使所有站点都使用http basic进行安全保护,而不仅仅是“/api”。我在stackoverflow中发现了一些问题,但它们似乎无法解决我的问题:

public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource datasource;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/api/**").authenticated().and()
                .httpBasic();
        http.authorizeRequests()
                .antMatchers("/**").authenticated()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .and()
                .formLogin().loginPage("/login").permitAll()
                .defaultSuccessUrl("/inicio");
        http.logout().permitAll();

        http.csrf().disable();
    }

    http.csrf().disable();
}
...

我也遇到了同样的问题,必须将基本身份验证和表单身份验证分开

@Configuration
@EnableWebSecurity
public class FormSecurityConfig extends WebSecurityConfigurerAdapter {

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

    http.authorizeRequests() //
            .antMatchers("/**").authenticated() //
            .antMatchers("/admin/**").hasRole("ADMIN") //
            .and() //
            .formLogin().loginPage("/login").defaultSuccessUrl("/inicio").permitAll() //
            .and() //
            .logout();
    }
}

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class BasicSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.antMatcher("/api/**") //
            .authorizeRequests().anyRequest().authenticated() //
            .and() //
            .httpBasic();
    }
}

我也遇到了同样的问题,必须将基本身份验证和表单身份验证分开

@Configuration
@EnableWebSecurity
public class FormSecurityConfig extends WebSecurityConfigurerAdapter {

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

    http.authorizeRequests() //
            .antMatchers("/**").authenticated() //
            .antMatchers("/admin/**").hasRole("ADMIN") //
            .and() //
            .formLogin().loginPage("/login").defaultSuccessUrl("/inicio").permitAll() //
            .and() //
            .logout();
    }
}

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class BasicSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.antMatcher("/api/**") //
            .authorizeRequests().anyRequest().authenticated() //
            .and() //
            .httpBasic();
    }
}

工作起来很有魅力。我只需要对您的代码添加两个小更改:1:在登录表单中添加了'permitAll()',因为它在登录页面上给了我一个'无限重定向'错误:.formLogin().loginPage(“/login”).permitAll().defaultSuccessUrl(“/inicio”),并在表单身份验证配置中添加了http.csrf.disable()(登录后出现“缺少CSRF令牌”错误(稍后我将对CSRF进行更多研究)。谢谢!您可以将我的答案标记为正确。我编辑了我的答案以添加permitAll().CSRF token是一种安全措施,在您的登录页面中,您必须添加一个包含服务器发送的令牌的_CSRF参数。我只需对您的代码添加两个小更改:1:在登录表单中添加了“permitAll()”,因为它在登录页面上给了我一个“无限重定向”错误:.formLogin().loginPage(“/login”).permitAll().defaultSuccessUrl(“/inicio”)并将http.csrf.disable()添加到表单身份验证配置中(登录后出现“缺少csrf令牌”错误(稍后我将对csrf进行更多研究)。谢谢!您可以将我的答案标记为正确。我编辑了我的答案以添加permitAll().CSRF令牌是一种安全措施,在您的登录页面中,您必须添加一个包含服务器发送的令牌的_CSRF参数