簡體   English   中英

如何在Java中使用Spring Security OAuth2在資源服務器中動態配置Httpsecurity?

[英]How to dynamically configure Httpsecurity in the Resource server using Spring security OAuth2 in Java?

我是Spring OAuth 2的新手。我想知道如何使用Spring OAuth 2.0在資源服務器中動態配置Httpsecurity 由於我具有存儲在數據庫中的antMatchersscopes列表(可能在將來的antMatchersscopes中可以添加)。

對於單個antMatchersscope我嘗試如下進行操作,並且按預期工作:

@Override
public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/adminservice/")
        .access("#oauth2.hasScope('Products')");
}

如果數據庫中存儲了一系列antMatchersscopes ,我應該如何配置它:

**antMatchers**     **scopes**
/adminservice/      products
/xxxx/              yyyy
/yyyy/              xxxx
/jkjlk/             uyuuy
/klklk/             hjkskjk

在此,春季引導本身希望在資源服務器中動態地從數據庫中為antMatchers配置相應的scope

我該如何實現?

根據您的數據庫訪問機制,只需自動連接jdbcTemplate,entityService並執行以下操作:

@Override
public void configure(HttpSecurity http) throws Exception {
    List<Matcher> matchers=matchersService.getAll();
    for(Matcher m : matchers) {
      http.authorizeRequests()
        .antMatchers(m.getMapping())
        .access("#oauth2.hasScope('"+m.getScope()+"')");
    }
}

現在,這將解決啟動期間的配置。 如果您希望能夠在運行時動態更改此設置,我只是建議您重新啟動服務器(如果可以選擇)。

暫無
暫無

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

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