2016-01-15 18 views
2

我正在使用Spring 4和Spring Security 4來保護Web服務。普通的網絡服務運行良好,沒有安全性。我的安全網絡服務在本地和單元測試中似乎也很有效。Spring 4 REST Web服務不能與安全性配合

我們正在使用SiteMinder示例,這意味着我們從遠程授權機構進行身份驗證。這會在我們登錄時向瀏覽器傳遞令牌。我們將驗證令牌與請求標頭一起傳遞,並從customerUserDetailsS​​ervice中提取。該類和方法從標題中獲取令牌,根據該遠程授權認證用戶,並獲取用戶名。從該用戶名中,我們對數據庫進行DAO調用以獲取用戶詳細信息及其角色,這些用戶詳細信息在Spring安全上下文中使用角色授予權限。這一切工作正常,我們得到一個認證的用戶,我們有他/她的角色/ grantedAuthorities。

因此,如前所述,我們現在只需使用spring-security.xml保護web服務,以基於用戶的角色來保護web服務。再次,這一切似乎都適用於單元測試。我們爲沒有訪問網站的用戶設置了令牌,並且我們正確地返回了403錯誤。當我們爲具有合適角色的用戶使用令牌時,能夠執行Web服務。

現在我正試圖將其部署到一個新的環境中,而且我沒有太多的運氣。

所以,我有一個彈簧security.xml文件看起來像:

<http use-expressions="true" auto-config="false" entry-point-ref="http403EntryPoint"> 

    <!-- Additional http configuration omitted --> 

     <intercept-url pattern="/records/authorizedRecords" access="hasRole('portalUser')" /> 
    <intercept-url pattern="/records/myCode" access="hasRole('portalUser')" /> 

    <intercept-url pattern="https://stackoverflow.com/users/email" access="hasRole('appAdmin')" /> 

    <custom-filter position="PRE_AUTH_FILTER" ref="openAmFilter" /> 
</http> 

<beans:bean id="openAmFilter" class="org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter"> 
    <beans:property name="principalRequestHeader" value="openam_token"/> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
</beans:bean> 

<beans:bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider"> 

    <beans:property name="preAuthenticatedUserDetailsService"> 
     <beans:bean id="userDetailsServiceWrapper" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper"> 
      <beans:property name="userDetailsService" ref="customUserDetailsService"/> 
     </beans:bean> 
    </beans:property> 

</beans:bean> 

<authentication-manager alias="authenticationManager"> 
    <authentication-provider ref="preauthAuthProvider" /> 
</authentication-manager> 

<beans:bean id="customUserDetailsService" class="com.agmednet.server.security.CustomUserDetailsService"></beans:bean> 
<beans:bean id="http403EntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"> 

當我們試圖訪問:

<intercept-url pattern="https://stackoverflow.com/users/email/*" access="hasRole('appAdmin')" /> 

這似乎並不匹配:

/rest/users/email/[email protected]

所以我把它改成:

<intercept-url pattern="/rest/users/email/*" access="hasRole('appAdmin')" /> 

我從日誌中得到這個。

DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/rest/trials/integratedtrials'; against '/rest/users/email/*' 
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Public object - authentication not attempted 
DEBUG: org.springframework.security.web.FilterChainProxy - /rest/trials/integratedTrials reached end of additional filter chain; proceeding with original chain 
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally 
DEBUG: org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed 

此時,我知道我們有一個經過身份驗證的用戶,用戶名和角色列在日誌中。我們似乎在spring-security.xml中找到了匹配的URL,URL匹配,角色匹配,現在我覺得我們應該執行Web服務背後的邏輯,但是我收到一條錯誤消息:

的Apache Tomcat/8.0.30 - 錯誤報告 /services/rest/users/email/[email protected]

所請求的資源不可用。

我絕對在這裏......無擔保的web服務工作很好。我必須失去一些東西?它是否與URL中的「休息」一詞? Web服務在不安全時一直有效。我增加了安全性並添加了單元測試來測試這一切,現在我不確定發生了什麼?

回答

0

問題是我是一個白癡!

在/ WEB-INF/web中。XML文件我已經這樣定義的:

<servlet-mapping> 
    <servlet-name>springmvc</servlet-name> 
    <url-pattern>/api/*</url-pattern> 
</servlet-mapping> 

我想這就是爲什麼當我調用任何RESTful網絡服務有:

/rest/users/email/[email protected] 

這是行不通的。

但是當我使用:

/api/users/email/[email protected] 

現在它的工作原理。

結論:我是個白癡!