2011-07-13 10 views
22

我有一個OSGi包,它使用JAX-RS來處理一些REST服務。這個軟件包在Apache CXF上運行在Karaf上。我需要將基本http認證應用於某些路徑/方法組合。我一直在對Spring Security進行修改,看來新的3.1版本可以讓你做到這一點,但是我一直在使用OSGi時遇到了很多麻煩。在Karaf的JAX-RS包中使用Spring Security

就像一個測試,我創建了一個非常簡單的beans.xml的文件:

<beans> 
    <import resource="classpath:META-INF/cxf/cxf.xml"/> 
    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/> 
    <import resource="classpath:META-INF/cxf/cxf-extension-http.xml"/> 
    <import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml"/> 

    <jaxrs:server id="serverTest" address="/test"> 
     <jaxrs:serviceBeans> 
      <ref bean="tstSvr"/> 
     </jaxrs:serviceBeans> 
    </jaxrs:server> 

    <bean id="tstSvr" class="com.demo.Test"/> 

    <security:http auto-config="true"> 
     <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" method="PUT" /> 
     <security:intercept-url pattern="/**" access="ROLE_USER" /> 
    </security:http> 

    <security:authentication-manager> 
     <security:authentication-provider> 
      <security:user-service> 
       <security:user name="user" password="pass" authorities="ROLE_USER"/> 
       <security:user name="admin" password="pass" authorities="ROLE_ADMIN"/> 
      </security:user-service> 
     </security:authentication-provider> 
    </security:authentication-manager> 
</beans> 

現在,來這裏的有趣的部分......從一切,我已經讀的一直在做,我需要一個web.xml任何這個工作。例如樣品一個,我試圖用:

<web-app> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
</web-app> 

使用這兩個文件的組合,沒有工作。通過「沒有工作」,我的意思是什麼都沒有發生。沒有錯誤消息,沒有例外,捆綁功能像以前一樣,我嘗試添加spring安全性。我假設問題是該軟件包需要是WAR或WAB才能加載web.xml。它是否正確?

更重要的是,有沒有辦法讓spring在沒有web.xml的情況下工作?

我正在做這個假設,我需要將捆綁包作爲CXF加載它的捆綁包,所以我無法將其轉換爲WAR或WAB,但我並不完全確定是這種情況。

感謝您提供任何幫助!

UPDATE: 做一堆額外的谷歌搜索後,我發現了一個forum post,所提到加入Web-FilterMappings: springSecurityFilterChain;url-patterns:="/*"到您的清單,而不是使用一個web.xml。但是,它仍然顯示您需要使用WAB而不是普通的捆綁包。我已將該行添加到清單中,但沒有任何效果。看來我的問題正在變成:我如何在CXF中使用WAB?

更新2: 因爲這個問題是不夠長......我決定嘗試使用Spring Security的註解,而不是攔截的URL的只是爲了看看會發生什麼。當我嘗試打了一個安全的路徑,我得到這個有趣的堆棧跟蹤:

Caused by: org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext 
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:323) 
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:196) 
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:59) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[46:org.springframework.aop:3.0.5.RELEASE] 

彈簧網站上說,出現這種情況的第一次嘗試以匿名方式連接到一個安全服務,它會不會發生第二時間。那麼,每次都會發生在我身上。從例外情況看,似乎我在清單中輸入的內容爲,可能問題與我的想法不同。任何人有任何想法,爲什麼發生這種情況?我是否缺少beans.xml中的一些條目來進行基本的http驗證工作?

+0

這不會回答你的問題,但它可能會幫助給你一些想法http://stackoverflow.com/questions/1887870/decrypting-message-with-a-spring-web-service-client – Zoidberg

+0

有你嘗試了沒有osgi方面的香草CXF/Spring Security?如果我們能夠確定CXF/Spring或osgi是否存在問題,這將非常有用 –

回答

5

首先,您需要清楚瞭解spring和web.xml的作用。

  • web.xml文件中配置Servlet容器,通常是像JBoss,Glassifsh,碼頭下,Tomcat應用服務器的標準文件...
  • 你的beans.xml是Spring應用程序配置文件上下文,因爲你沒有使用任何命名空間,這意味着Karaf使用spring oob。

如果你想要做的Http使用servlet,沒有一個容器,我想你必須註冊一個HttpService的,請參閱:http://karaf.apache.org/manual/latest-2.2.x/users-guide/http.html

你的例外情況發生,因爲當它到達映射的方法,在SecurityContext中不是Authentication bean。您尚未在映射中定義任何匿名訪問,也許這就是它失敗的原因。

基本上,要麼在你的http配置中定義一個AnonymousAuthenticationFilter,或者一個帶有permitAll的URL模式。否則,當您第一次嘗試匿名連接同一個會話時,您將不斷得到該異常。

相關問題