2012-11-28 211 views
2

我想在處女座3.6.0.M03上使用Spring Security 3.1.3創建一個簡單的例子。 該示例包含的3束:Spring Security和OSGi

  • 安全束配置和發佈的AuthenticationManager

    <security:authentication-manager id="authenticationManager"> 
        <security:authentication-provider> 
         <security:user-service> 
         <security:user name="Rigas" password="password" authorities="ROLE_MEMBER"/> 
         </security:user-service> 
        </security:authentication-provider> 
    </security:authentication-manager> 
    <service id="authenticationManagerOsgi" ref="authenticationManager" interface="org.springframework.security.authentication.AuthenticationManager"/> 
    
  • helloSecurity束髮佈一個安全的helloWorld方法。它引用AuthenticationManager並將其注入全局方法安全性中。

    <security:global-method-security secured-annotations="enabled" authentication-manager-ref="authenticationManager"/> 
    <reference id="authenticationManager" availability="mandatory" interface="org.springframework.security.authentication.AuthenticationManager"/> 
    <service id="helloSpringSecurityOsgi" ref="helloSpringSecurity" interface="net.ansible.examples.hellospringsecurity.HelloSpringSecurity" /> 
    
  • helloSecurityConsumer束引用helloSecurity IF和調用安全的helloWorld方法。

    <reference id="helloSpringSecurity" availability="mandatory" interface="net.ansible.examples.hellospringsecurity.HelloSpringSecurity"/> 
    

捆綁statup和線路正常工作,但一旦helloSecurityConsumer調用的安全方法如下異常被拋出:

Failed to call secure method org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'authenticationManager' must be of type [org.springframework.security.authentication.ProviderManager], but was actually of type [$Proxy94] 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:360) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
at org.springframework.security.config.method.GlobalMethodSecurityBeanDefinitionParser$AuthenticationManagerDelegator.authenticate(GlobalMethodSecurityBeanDefinitionParser.java:386) 
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.authenticateIfRequired(AbstractSecurityInterceptor.java:316) 
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:202) 
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:60) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622) 
at net.ansible.examples.hellospringsecurity.impl.HelloSpringSecurityImpl$$EnhancerByCGLIB$$99e49c75.sayHello(<generated>) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319) 
at org.eclipse.gemini.blueprint.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:56) 
at org.eclipse.gemini.blueprint.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.java:60) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131) 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.eclipse.gemini.blueprint.service.importer.support.LocalBundleContextAdvice.invoke(LocalBundleContextAdvice.java:57) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131) 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
at $Proxy95.sayHello(Unknown Source) 
at net.ansible.examples.hellospringsecurityconsumer.impl.HelloSpringSecurityConsumerImpl$MyRunnable.run(HelloSpringSecurityConsumerImpl.java:50) 
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) 
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(Unknown Source) 
at java.util.concurrent.FutureTask.runAndReset(Unknown Source) 
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(Unknown Source) 
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(Unknown Source) 
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at java.lang.Thread.run(Unknown Source) 

GlobalMethodSecurityBeanDefinitionParser試圖使用引用的AuthenticationManager和有問題轉換將OSGi參照代理轉發給ProviderManager。 調試顯示Proxy(一個jdkDynamicAopProxy)確實是一個圍繞ProviderManager類的代理,並具有所有相關的接口。 是否有工作配置來實現我想要做的事情?

回答

4

這看起來像一個錯誤給我。引發此錯誤的文件GlobalMethodSecurityBeanDefinitionParser.java的386線是

delegate = beanFactory.getBean(authMgrBean, ProviderManager.class); 

它要求bean工廠與具體實施ProviderManager的豆,但它應該只是獲取一個bean實例與接口AuthenticationManager這樣的:

delegate = beanFactory.getBean(authMgrBean, AuthenticationManager.class); 

您可能想要在Spring Security's Jira中提高門票。

順便說一句:我們還將導出authenticationManager作爲OSGI服務並在其他包中使用它,這非常有效。

+0

我只記得我剛纔打開了一個bug報告,它有類似的問題:https://jira.springsource.org/browse/SEC-1842 (http://forum.springsource.org/showthread。 PHP?116363)。我們也遇到了一個需要ProviderManager實例的問題,而不僅僅是一個AuthenticationManager實現。因此,在代碼中可能會有其他地方使用ProviderManager而不是AuthenticationManager。 – James

+0

你說得對。修補GlobalMethodSecurityBeanDefinitionParser使一切正常工作。 –