簡體   English   中英

資源服務器如何使用oauth2中的令牌識別資源所有者?

[英]How can the resource server identify the resource owner using token in oauth2?

我正在研究的典型方案是:

  1. User1向前端的其余客戶端提供適當的憑據(授予類型:密碼),然后客戶端獲得令牌。
  2. 客戶端發送令牌並訪問User1擁有的資源。

在我的方案中,一旦客戶端具有user1的訪問令牌,我希望客戶端只能訪問User1的資源。

考慮客戶端訪問URI / users / 1 / books。 該響應將包含與User1相關聯的所有書籍。 主要的問題是,如果客戶端使用User1的令牌訪問URL / users / 2 / books,則它將獲取User2的所有書籍的列表,這是不允許的。

如何將范圍限制為使用其憑據獲取令牌的用戶?

如何將令牌映射到資源服務器中的特定用戶?

我正在使用Spring / Java。 但是任何一般理論也將有所幫助。

經過大量調試,我得到了答案。

Spring Security 1.4令牌存儲:InMemoryTokenStore()

在ResourceServerConfiguration中,配置HttpSecurity。

@Override
public void configure(final HttpSecurity http) throws Exception {
    // @formatter:off
    http.authorizeRequests().
    // antMatchers("/oauth/token").permitAll().
    antMatchers("/api/users/{userId}").access("@webSecurity.checkUserId(authentication,#userId)")
    .anyRequest().authenticated().and().sessionManagement()
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf().disable();
    // @formatter:on
}

創建一個類WebSecurity並提供實現。

public class WebSecurity {
    public boolean checkUserId(Authentication auth, int id) {
        return ((UmUser)auth.getPrincipal()).getId() == id;
    }
}

資源: http : //docs.spring.io/spring-security/site/docs/current/reference/html/el-access.html#el-access-web-path-variables

http://www.baeldung.com/get-user-in-spring-security

使用JwtTokenStore時,我不得不調試很多東西。 這將主體作為字符串返回,而不是我期望的UserDetails實例。

切換到InMemoryTokenStore后,我得到了預期的結果。 我可以選擇這對我來說不是問題,但是我仍然想知道如何使用JWT實現它。

暫無
暫無

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

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