Spring 实现ServletConextAware后,AuthenticationProvider中的ServletContext实例不可用

Spring 实现ServletConextAware后,AuthenticationProvider中的ServletContext实例不可用,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,我有一个 CustomAuthenticationProvider通过实现AuthenticationProvider CustomUserDetailsService通过实现UserDetailsService 问题 我已经在这两个实现中实现了ServletContextAware,我只能在CustomUserDetailsService中获得ServletContextAware,而不能在CustomAuthenticationProvider中 背后的原因是什么 要在CustomUserD

我有一个

  • CustomAuthenticationProvider
    通过实现
    AuthenticationProvider
  • CustomUserDetailsService
    通过实现
    UserDetailsService
  • 问题

    我已经在这两个实现中实现了
    ServletContextAware
    ,我只能在
    CustomUserDetailsService
    中获得
    ServletContextAware
    ,而不能在
    CustomAuthenticationProvider

    背后的原因是什么

    要在
    CustomUserDetailsService
    中获取
    ServletContext
    ,最好的方法是什么

    下面举例说明

    CustomAuthenticationProvider示例

    public CustomAuthenticationProvider implements AuthenticationProvider, ServletContextAware{
    
          private ServletContext servletContext;
    
           @override
           public Authentication authenticate(Authentication authentication)
                         throws AuthenticationException{
    
                System.out.println("Context Path :: "+servletContext);  // Here the reference is NULL
           } 
    
           @Override 
           void setServletContext(ServletContext servletContext){
              this.servletContext = servletContext;
           }  
    }
    
    CustomUserDetails服务示例

    public CustomUserDetailsService implements UserDetailsService, ServletContextAware{
    
            private ServletContext servletContext;
    
            @Override
            public UserDetails loadByUserName(final String login){
    
                System.out.println("Context Path :: "+servletContext);  // Here the reference is NOT NULL
            }
    
    
           @Override 
           void setServletContext(ServletContext servletContext){
              this.servletContext = servletContext;
           }  
    
    
    }
    
    配置

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
    
      @Inject
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
                auth.authenticationProvider(customAuthenticationProvider)
                auth.userDetailsService(customUserDetailsService)
                    .passwordEncoder(passwordEncoder());
        }
    
        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring()
                .antMatchers("/scripts/**/*.{js,html}")
                .antMatchers("/bower_components/**")
                .antMatchers("/i18n/**")
                .antMatchers("/assets/**")
                .antMatchers("/swagger-ui.html")
                .antMatchers("/test/**");
        }
    
    }
    

    请给出建议。

    发布您的配置,并且这些方法应该是公共的,而不是包保护的。这些方法仅是公共的,它们是接口中被重写的方法。这里的配置是自动的。但无法理解为什么AuthenticationProvider中不提供ServletContextAware,但我确实需要此实现中的ServletContext。它们在您的类中不是公共的,至少在此处显示的代码中是受包保护的。你的类上没有任何注释,你对auto是什么意思。因此,请再次添加这两个类的配置。@Deinum抱歉,我无法理解您的观点,因为这两个类都在同一个包中,您可以看到这两个类的实现。现在我提供了spring安全配置供您参考。事实上,它们在同一个包中并没有什么区别。。。我想知道的是这些类是如何实例化的。如果这些都是源代码,那么如何创建这些自定义类的实例呢。这就是我想知道的。不是示例代码,而是显示一些实际的或代表性的代码,因为我怀疑这些代码是否完全代表了您在本地拥有的内容。