簡體   English   中英

403訪問被禁止錯誤-URL具有動態路徑參數時的Spring安全性

[英]403 access forbidden error - spring security when the url has dynamic path param

我的休息服務有一個名為USER的資源。 以下是該資源中的一些API。

/ api / users / {userId} / order / {orderId}
/ api / users / {userId} / favoriteStores

我希望第一個URL受保護,而第二個URL不需要安全。(舉一個示例api url)。

我將安全配置配置如下

  <security:http pattern="/rest/users/**" entry-point-ref="restAuthenticationEntryPoint"
        use-expressions="true" auto-config="false" create-session="stateless">
        <security:custom-filter ref="authenticationTokenProcessingFilter"
            position="PRE_AUTH_FILTER" />
        <security:intercept-url pattern="/rest/users/{userId}/order/**"
            access="hasRole('ROLE_CUSTOMER')" />
        <security:logout />
    </security:http>

但這也會攔截第二個URL(哪個是/ api / users / {userId} / favoriteStores)

所以,我將配置更改為

<security:http pattern="/rest/users/{userId}/order/**" entry-point-ref="restAuthenticationEntryPoint"
            use-expressions="true" auto-config="false" create-session="stateless">
            <security:custom-filter ref="authenticationTokenProcessingFilter"
                position="PRE_AUTH_FILTER" />
            <security:intercept-url pattern="/rest/users/{userId}/order/**"
                access="hasRole('ROLE_CUSTOMER')" />
            <security:logout />
        </security:http>

現在顯示禁止錯誤。

我應該如何配置安全截獲網址?

在Spring Security中,如果您不想截獲URL模式,則必須將訪問值提供為allowAll。 URL模式也必須完全符合您的要求。 如您的示例所示,如果您想允許url模式rest/users/{userId}/favoriteStores那么您必須添加一個類似的攔截模式:

<security:intercept-url pattern="/rest/users/*/favoriteStores/**"
                access="permitAll" />

有關更多信息,您可以通過spring鏈接進行基於spring基於表達式的訪問控制

如果您根本不想截取網址,則只需執行

<http pattern="/rest/users/*/favoriteStores/**" security="none"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM