2015-12-02 84 views
0

这可能与某些加载顺序问题有关,但我不确定/正在寻找确认。我正在开发一个遗留项目,我们无法切换到SpringBoot配置,但我们已将所有核心弹簧罐更新为4.2.x.Spring'AuthenticationManager'是必需的'带xml配置的Java注释配置

每当我启动启用安全配置的服务器(运行在Jetty容器中)时,我会在[1]下面收到以下错误。

我的XML配置看起来像这样:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
     http://www.springframework.org/schema/util 
     http://www.springframework.org/schema/util/spring-util-4.2.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.2.xsd 
     http://www.springframework.org/schema/task 
     http://www.springframework.org/schema/task/spring-task-4.2.xsd 
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security-4.0.xsd"> 

    <context:annotation-config /> 

    <bean class="com.company.project.config.SecurityConfiguration"/> 

我有我第一次写的SecurityConfiguration样本春天启动的项目,我没有收到异常出现。

我的安全配置是这样的:

@Configuration 
@EnableWebSecurity 
@EnableCustomSSOSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Value("${sso.host}") 
    private String host; 

    @Value("${sso.appId}") 
    private String appId; 

    @Value("${sso.targetService}") 
    private String target; 

    @Autowired 
    private CustomUserDetailsService userDetailsService; 

    @Override 
    public void configure(WebSecurity web) throws Exception { 

     web.ignoring() 
      .antMatchers("/scripts/**/*.{js,html}") 
      .antMatchers("/bower_components/**") 
      .antMatchers("/i18n/**") 
      .antMatchers("/assets/**") 
      .antMatchers("/swagger-ui/index.html") 
      .antMatchers("/test/**"); 
    } 

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

     http 
     .csrf() 
      .disable() 
     .apply(CustomSSOConfigurer.configure(host)) 
      .appId(appId) 
      .targetService(target) 
      .authenticatedUserDetailsService(customUserDetailsService) 
     .and() 
      .logout() 
      .deleteCookies("JSESSIONID") 
      .logoutSuccessUrl("/logout") 
      .invalidateHttpSession(true) 
     .and() 
      .authorizeRequests() 
      .antMatchers("/*").permitAll(); 
    } 

} 

产生的问题是,因为没有配置authenticationProviders。所以在AuthenticationManagerBuilder此代码返回null:

@Override 
protected ProviderManager performBuild() throws Exception { 
    if (!isConfigured()) { 
     logger.debug("No authenticationProviders and no parentAuthenticationManager defined. Returning null."); 
     return null; 
    } 

当我从春天开机启动,我看到下面的AuthenticationProvider:

org.spring[email protected]4fcf4925 

这导致ProviderManager的创建,并最终认证经理。

这个DaoAuthenticationProvider来自哪里?我应该如何配置它或缺少什么xml配置?这与加载顺序有关吗?

任何帮助,非常感谢。

[1]从服务器错误

2015-12-02 09:58:05,230 [WARN ] org.springframework.context.support.ClassPathXmlApplicationContext [main] - Exception encountered during context initialization - cancelling refresh attempt 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodSecurityInterceptor' defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: An AuthenticationManager is required 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) 
    at com.company.core.common.configuration.Config.initContext(Config.java:753) 
    at com.company.core.common.configuration.Config.initContext(Config.java:893) 
    at com.company.core.common.configuration.Config.getApplicationContext(Config.java:1188) 
    at com.company.core.common.ApplicationBase.initContext(ApplicationBase.java:59) 
    at com.company.core.common.ApplicationBase.run(ApplicationBase.java:157) 
    at com.company.core.web.ApplicationBaseInitListener.contextInitialized(ApplicationBaseInitListener.java:58) 
    at com.company.project.core.web.ICFAplicationBaseListener.contextInitialized(ICFAplicationBaseListener.java:50) 
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:778) 
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:425) 
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:770) 
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:275) 
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1312) 
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:722) 
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:490) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:108) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:90) 
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:108) 
    at org.eclipse.jetty.server.Server.start(Server.java:346) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:90) 
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58) 
    at org.eclipse.jetty.server.Server.doStart(Server.java:294) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69) 
    at com.company.project.jetty.StartICFServer.startServer(StartICFServer.java:93) 
    at com.company.project.jetty.StartICFServer.main(StartICFServer.java:37) 
Caused by: java.lang.IllegalArgumentException: An AuthenticationManager is required 
    at org.springframework.util.Assert.notNull(Assert.java:115) 
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.afterPropertiesSet(AbstractSecurityInterceptor.java:131) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 
    ... 36 more 

回答

0

尝试在XML文件中添加以下代码:

<security:authentication-manager/> 

<bean class="com.company.project.config.SecurityConfiguration"/>