相关疑难解决方法(0)

将多个WebSecurityConfigurerAdapter与不同的AuthenticationProviders一起使用(API的基本身份验证和Web应用程序的LDAP)

根据Spring Security Reference 5.7节,应该可以定义多个安全适配器.

我尝试做同样但没有成功.在服务器重新启动之后,API的前x次使用基本身份验证工作正常,但经过几次我被重定向到登录(表单)页面,这应该只针对我们的Web应用程序,而不是API调用.

我的代码:

@EnableWebSecurity
public class MultiHttpSecurityConfig  {

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Autowired
        private Environment env;

        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().
                withUser("admin").password("pw_test").roles(API_ROLE);
        }

        protected void configure(HttpSecurity http) throws Exception {
            http
              .antMatcher("/services/**")
              .authorizeRequests()
              .anyRequest().hasRole(API_ROLE)
              .and()
              .httpBasic()
              .and()
              .csrf()
              .disable();
        }
    }

    @Configuration
    @Order(2)
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Autowired
        private Environment env;

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

        @Override …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security basic-authentication spring-boot

13
推荐指数
1
解决办法
1万
查看次数

Spring REST安全性 - 以不同方式保护不同的URL

我在Spring 4下使用基本身份验证工作REST API.这些REST服务位于/ api/v1/**URL下.但是,我想在不同的url/api/v2/**下添加另一组REST端点,但使用基于令牌的身份验证进行保护.

是否可以使用一个servlet执行此操作?如何配置Spring Security以对不同的URL使用不同形式的身份验证?

谢谢.

java rest spring spring-mvc spring-security

6
推荐指数
1
解决办法
8219
查看次数