mkk 關於資源的解釋 : https://andaily.com/blog/?cat=19
resource用於將系統提供的各類資源進行分組管理,
每一個resource對應一個resource-id, 而一個client details至少要有一個resource-id (對應OauthClientDetails.resourceIds oauth_client_details.resource_ids).
1、配置resourceId
在spring-oauth-server的security.xml配置了兩個資源id:
這就是配置resource的地方, 看見里面的兩個resource-id值.
2、使用resourceId
在spring-oauth-server的security.xml配置了兩個<http>配置, 一個的pattern為/m/**, 另一個為/unity/**,
這就是資源與Spring Security整合后的配置,
注意每個<http>配置里的 ROLE_UNITY與ROLE_MOBILE, 這對應Spring Security的權限,應數據庫中的user_.previllage:
同理 scope_read字段對應數據庫中的oauth_client_details.scope.
接着我們添加一個自己的resource, 假設resource-id = myResource, url pattern為/my/api/**, 權限為ROLE_MY_RESOURCE, scope為 read; 其配置如下:
1.添加<oauth2:resource-server> , resource-id=myResource
如下:
<oauth2:resource-server id="myResourceServer" resource-id="myResource" token-services-ref="tokenServices"/>
注意, id值需要唯一
2.添加<http>配置,
如下:
<http pattern="/my/api/**" create-session="never" entry-point-ref="oauth2AuthenticationEntryPoint" access-decision-manager-ref="oauth2AccessDecisionManager" use-expressions="false"> <anonymous enabled="false"/> <intercept-url pattern="/my/api/**" access="ROLE_MY_RESOURCE,SCOPE_READ"/> <custom-filter ref="myResourceServer" before="PRE_AUTH_FILTER"/> <access-denied-handler ref="oauth2AccessDeniedHandler"/> <csrf disabled="true"/> </http>
說明: 一個<resource-server>可以配置多個<http>, 使用不同的url pattern
OK, 配置完成, 新增resource: myResource;
這時候, 所有以/my/api/** 開頭的URL請求都將受到 OAUTH2的保護,
接下來在業務中創建client details, 記得把resourceIds設置為myResource,且要有ROLE_MY_RESOURCE的權限.