Java 从spring安全上下文访问应用程序上下文

Java 从spring安全上下文访问应用程序上下文,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我有一个SpringMVC应用程序,它使用SpringSecurity进行授权 我已经实现了一个自定义AuthenticationProvider,它授权用户 我希望此自定义AuthenticationProvider访问在应用程序上下文中定义的bean 这可能吗?如果是,怎么做 web.xml: ... <context-param> <param-name>contextConfigLocation</param-name> <param

我有一个SpringMVC应用程序,它使用SpringSecurity进行授权

我已经实现了一个自定义AuthenticationProvider,它授权用户

我希望此自定义AuthenticationProvider访问在应用程序上下文中定义的bean

这可能吗?如果是,怎么做

web.xml:

 ...
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>WEB-INF/spring-security.xml</param-value>
 </context-param>
  ...
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
  ...
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
我得到的错误是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'customAuthenticationProvider' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAuthenticationProvider' defined in ServletContext resource [/WEB-INF/spring-security.xml]: Cannot resolve reference to bean 'loginService' while setting bean property 'loginService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'loginService' is defined

父上下文不能具有子上下文的依赖项

在本例中,CustomAuthenticationProviderbean是父上下文的一部分,它与子web上下文loginService具有依赖关系

所以你应该

  • 创建单独的services-context.xml并将loginService bean定义从dispatcher-servlet.xml移动到services-context.xml
  • 在web.xml中的
    contextConfigLocation
    values列表中添加services-context.xml

  • 父上下文不能具有子上下文的依赖项

    在本例中,CustomAuthenticationProviderbean是父上下文的一部分,它与子web上下文loginService具有依赖关系

    所以你应该

  • 创建单独的services-context.xml并将loginService bean定义从dispatcher-servlet.xml移动到services-context.xml
  • 在web.xml中的
    contextConfigLocation
    values列表中添加services-context.xml

  • 请发布代码示例OK,将代码示例添加到问题请发布代码示例OK,将代码示例添加到问题
      <bean class="com.example.davvstest.LoginService" name="loginService">
      </bean>
    
    package com.example.davvstest;
    
    import org.springframework.security.authentication.AuthenticationProvider;
    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.AuthenticationException;
    
    public class CustomAuthenticationProvider implements AuthenticationProvider {
    
        private LoginService loginService;
    
        public LoginService getLoginService() {
            return loginService;
        }
    
        public void setLoginService(LoginService loginService) {
            this.loginService = loginService;
        }
    
        @Override
        public Authentication authenticate(Authentication authentication)
                throws AuthenticationException {
            if (!loginService.checkAuth(authentication.getName())){
                throw new BadUserNameException("Bad username");
            }
            return authentication;
        }
    
        @Override
        public boolean supports(Class<?> authentication) {
            return true;
        }
    
    }
    
    package com.example.davvstest;
    
    public class LoginService {
    
        public LoginService() {
        }
    
        public boolean checkAuth(String username){
            return true;
        }
    }
    
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'customAuthenticationProvider' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAuthenticationProvider' defined in ServletContext resource [/WEB-INF/spring-security.xml]: Cannot resolve reference to bean 'loginService' while setting bean property 'loginService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'loginService' is defined