我假設您使用Azure AD OAuth 2.0的默認配置來返回JWT編碼的標記。這種類型的令牌幾乎沒有什麼好處 - 您可以從令牌中自行提取信息,例如授予的範圍,並且您可以通過檢查令牌簽名來避免向驗證服務器發送驗證請求。
配置JWT令牌驗證
您將需要配置資源服務器(你的Web API)使用JWT令牌:
@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setVerifierKey(obtainAzureADPublicKey());
return converter;
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
return defaultTokenServices;
}
}
此代碼(帶小的修改),從優秀blog post取Eugen Paraschiv(aka Baeldung)。
獲取Azure的簽名密鑰
您將需要得到一個非對稱的公共加密密鑰在Azure AD使用簽署發佈的令牌,並從obtainAzureADPublicKey
方法返回。
基礎上documentation你將不得不(通過檢索"jwks_uri"
財產從結果值)首先獲得來自https://login.microsoftonline.com/common/.well-known/openid-configuration
智威湯遜的簽名密鑰端點的元信息。
然後,您需要從該URL獲取正確的密鑰。
請注意,Azure AD會不時更改此信息,因此您無法在應用程序啓動時只執行一次。但是,將其緩存至少24小時將是一個好主意。
Hi @ Oleg,它是否(從安全的角度來看)可以在Cookie中存儲刷新令牌和不記名令牌? –