下面搭建一個與authServer分離的resourceServer
ResourceServerSecurityConfigurer
- 因為與authServer分離锌介,所以這里需要使用remoteTokenService
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
RemoteTokenServices remoteTokenServices = new RemoteTokenServices();
DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
UserAuthenticationConverter userTokenConverter = new CommonUserConverter();
accessTokenConverter.setUserTokenConverter(userTokenConverter);
remoteTokenServices.setCheckTokenEndpointUrl(resourceServerProperties.getTokenInfoUri());
remoteTokenServices.setClientId(oAuth2ClientProperties.getClientId());
remoteTokenServices.setClientSecret(oAuth2ClientProperties.getClientSecret());
remoteTokenServices.setRestTemplate(restTemplate());
remoteTokenServices.setAccessTokenConverter(accessTokenConverter); resources.tokenServices(remoteTokenServices);
}
- CommonUserConverter實現(xiàn)了從遠程認(rèn)證信息的轉(zhuǎn)換工作曙博。
- tokenServices:ResourceServerTokenServices 類的實例锻全,用來實現(xiàn)令牌業(yè)務(wù)邏輯服務(wù)
- resourceId:這個資源服務(wù)的ID今瀑,這個屬性是可選的欢唾,但是推薦設(shè)置并在授權(quán)服務(wù)中進行驗證
- tokenExtractor 令牌提取器用來提取請求中的令牌
- 請求匹配器,用來設(shè)置需要進行保護的資源路徑熊锭,默認(rèn)的情況下是受保護資源服務(wù)的全部路徑
- 受保護資源的訪問規(guī)則弧轧,默認(rèn)的規(guī)則是簡單的身份驗證(plain authenticated)
- 其他的自定義權(quán)限保護規(guī)則通過 HttpSecurity 來進行配置
- 使用 DefaultTokenServices 在資源服務(wù)器本地配置令牌存儲、解碼碗殷、解析方式
- 使用 RemoteTokenServices 資源服務(wù)器通過 HTTP 請求來解碼令牌精绎,每次都請求授權(quán)服務(wù)器端點 /oauth/check_token
- 若授權(quán)服務(wù)器是 JWT 非對稱加密,則需要請求授權(quán)服務(wù)器的 /oauth/token_key 來獲取公鑰 key 進行解碼