【问题标题】:Configure Spring Security AuthenticationManager without spring-boot-starter-web在没有 spring-boot-starter-web 的情况下配置 Spring Security AuthenticationManager
【发布时间】:2020-10-27 06:45:16
【问题描述】:

我正在使用io.github.lognet:grpc-spring-boot-starter:3.5.3 添加 Grpc 支持。我有一个org.springframework.boot:spring-boot-starter-security 依赖项。我不想添加 org.springframework.boot:spring-boot-starter-web 依赖项,因为我的应用程序需要使用没有 servlet 和 tomcat 服务器的 Grpc Netty 服务器。

有两个AuthentificationProvider 的实现我配置了一个AuthentificationManager:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final ClientAuthenticationProvider clientAuthenticationProvider;
    private final ServiceAuthenticationProvider serviceAuthenticationProvider;

    public WebSecurityConfig(ClientAuthenticationProvider clientAuthenticationProvider,
                             ServiceAuthenticationProvider serviceAuthenticationProvider) {
        this.clientAuthenticationProvider = clientAuthenticationProvider;
        this.serviceAuthenticationProvider = serviceAuthenticationProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .authenticationProvider(clientAuthenticationProvider)
                .authenticationProvider(serviceAuthenticationProvider);
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}

构建失败并显示消息:

错误:(18, 8) java: 无法访问 javax.servlet.Filter 类文件 对于 javax.servlet.Filter 未找到

这是因为WebSecurityConfigurerAdapter 需要javax.servlet.Filter。

我尝试添加 javax.servlet:javax.servlet-api:4.0.1 依赖,但在调用 authenticationManager.authenticate(...) 时应用程序在运行时失败:

public class MytAuthService {

    private final AuthenticationManager authenticationManager;

    public AbstractAuthService(
          @Qualifier("authenticationManagerBean") AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

   
    public TokenPair authenticate(AuthRequest request) throws AuthenticationException {
        ...

        authenticationManager.authenticate(createAuthToken(username, password));

        ...
    }

使用堆栈跟踪:

java.lang.IllegalStateException:此对象尚未在 org.springframework.security.config.annotation.AbstractSecurityBuilder.getObject(AbstractSecurityBuilder.java:55) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在 org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:506) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在 org.my.service.auth.MyAuthService.authenticate(MyAuthService.java:75) ~[类/:?]

当我添加 org.springframework.boot:spring-boot-starter-web 时一切正常,但我不想将 Tomcat 和 servlet 添加到我的应用程序中。 我可以配置AuthentificationManager 来设置自定义AuthenticationProvider 而不扩展WebSecurityConfigurerAdapter 类或任何方式吗?或者你可以展示一个很好的示例/教程,其中 Grpc 和 Spring Security 仅使用?

【问题讨论】:

    标签: java spring spring-boot grpc


    【解决方案1】:

    回答我自己,我应该删除扩展 WebSecurityConfigurerAdapter 并使用 ProviderManager 类创建一个 AuthenticationManager bean。

    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class WebSecurityConfig {
    
        private final ClientAuthenticationProvider clientAuthenticationProvider;
        private final ServiceAuthenticationProvider serviceAuthenticationProvider;
    
        public WebSecurityConfig(ClientAuthenticationProvider clientAuthenticationProvider,
                                 ServiceAuthenticationProvider serviceAuthenticationProvider) {
            this.clientAuthenticationProvider = clientAuthenticationProvider;
            this.serviceAuthenticationProvider = serviceAuthenticationProvider;
        }
    
        @Bean
        public AuthenticationManager authenticationManagerBean() {
            return new ProviderManager(Arrays.asList(clientAuthenticationProvider, serviceAuthenticationProvider));
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-13
      • 2021-01-22
      • 2014-11-18
      • 2016-06-01
      • 2016-01-29
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 2011-09-16
      相关资源
      最近更新 更多