簡體   English   中英

具有spring-security的OAuth2 - 通過HTTP方法限制REST訪問

[英]OAuth2 with spring-security - limit REST access by HTTP method

因此,我們使用spring-securityREST服務器客戶端應用程序服務器之間實現了OAuth2 - 客戶端憑據授權 客戶端可以使用客戶端憑據身份驗證獲取的令牌訪問其資源。

我沒有得到的是我可以為客戶和資源設置的范圍

我明確地說過, 客戶端A的 角色X帶有Scope READ ,我說可以使用Role XScope READ訪問Resource U. 現在我希望資源拒絕任何DELETEPOST請求,但顯然我錯了。 如果我向資源U發送HTTP DELETE ,它將通過。

所以看起來Scopes在我的設置中毫無意義。 我該如何使用它們?

或者我仍然需要限制資源本身的訪問權限,如下所示?


我們設置的片段:

<oauth:client-details-service id="clientDetails">
    <oauth:client client-id="the_client"
        authorized-grant-types="client_credentials" authorities="ROLE_CLIENT"
        scope="read" secret="secret" />
</oauth:client-details-service>


<http pattern="/rest/**" create-session="never"
    entry-point-ref="oauthAuthenticationEntryPoint"
    access-decision-manager-ref="accessDecisionManager"
    xmlns="http://www.springframework.org/schema/security">
    <anonymous enabled="false" />

    <intercept-url pattern="/rest/persons" access="ROLE_CLIENT,SCOPE_READ" />

    <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
    <access-denied-handler ref="oauthAccessDeniedHandler" />
</http>

REST資源:

@Path("/persons")
@Named
public class PersonRest {

    @GET
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces({ MediaType.APPLICATION_JSON })
    public Response getAll(@Context UriInfo uriInfo) {
        // ...
    }

    @DELETE
    @Path("/{id}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    @Transactional
    public Response delete(@PathParam("id") String id) {
        // ...
    }

是否有任何“簡單”配置來告訴服務器端的Spring安全性,ROLE_READ應該只允許GET請求?

或者我是否需要為特定資源設置@PreAuthorize注釋或上下文設置?

那么,這樣的工作會是這樣嗎?

    @PreAuthorize("hasScope(read)")
    @DELETE
    @Path("/{id}")
    // ...
    public Response delete(@PathParam("id") String id) {

雖然如果我不得不這樣做,那將是一個可怕的消息,因為那時我必須單獨配置每個資源......

也許我可以使用一些過濾器

感謝您的幫助!

你為什么不能用

@RolesAllowed({"customRole"})

保護您的HTTP方法,並使用過濾器將Oauth范圍轉換為{customRole}

還是你試過

<security:intercept-url pattern="/rest/persons" access="hasAnyRole('ROLE_USER_READ')" method="GET"/> 

或類似的東西

暫無
暫無

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

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