文章目錄
token處理
基本的token參數
使用jwt替換默認的token
擴展和解析jwt的信息
基本的token配置參數 AuthorizationServerConfigurerAdapter
認證服務器繼承AuthorizationServerConfigurerAdapter 重寫 configure(ClientDetailsServiceConfigurer clients)配置token
@Configuration
@EnableAuthorizationServer
public class WhaleAuthenticationServiceConfig extends AuthorizationServerConfigurerAdapter {
// 如果不繼承AuthorizationServerConfigurerAdapter類 它會自動在bean容器中找這兩個bean
@Autowired
private AuthenticationManager authenticationManager;
@Qualifier("demoUserDetailsService")
@Autowired
private UserDetailsService userDetailsService;
/**
*
* @param endpoints oauth/token 的入口點
* @throws Exception
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
// super.configure(endpoints);
endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService);
}
/**
* 客戶端配置 我們的認證服務器會給哪些第三方發令牌
* 此時 demo application 中
* security.oauth2.client.client-id=whale
* security.oauth2.client.client-secret=123456
* 將失效
*
* 相關配置從此方法讀取
*
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// super.configure(clients);
// clients.inMemory() token 存入內存
// clients.jdbc() token 存入數據庫
clients.inMemory()
.withClient("whale")
.secret("123456")
.accessTokenValiditySeconds(7200) // token有效時間 單位秒
.authorizedGrantTypes("refresh_token","password") //支持哪些授權模式 token刷新和密碼模式
.scopes("all","read","write"); //相當於權限,配置了這個參數,請求裏面可以不帶scope參,如果帶了參數,必須在配置的這個scope範圍之內
}
}
測試
多個client配置
代碼
重構
client的配置從application中獲取,達到複用性
OAuth2ClientProperties
spring中已經有了 OAuth2ClientProperties這個實體,它有三個屬性
clientId、clientSecret 、defaultSecret
org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties
@ConfigurationProperties(
prefix = "security.oauth2.client"
)
public class OAuth2ClientProperties {
private String clientId;
private String clientSecret = UUID.randomUUID().toString();
private boolean defaultSecret = true;
······················
但我們還需要一個超時時間的屬性
重新寫一個類
public class OAuth2ClientProperties implements Serializable {
private String clientId;
private String clientSecret;
//令牌過期時間單位爲秒 0 是 永不過期
private int accessTokenValiditySeconds = 7200;
ok
支持數組配置 OAuth2Properties
public class OAuth2Properties implements Serializable {
private OAuth2ClientProperties[] clients = {};
SecurityProperties
//client 配置
private OAuth2Properties oauth2 = new OAuth2Properties();
AuthenticationServiceConfig
WhaleAuthenticationServiceConfig
/**
* 客戶端配置 我們的認證服務器會給哪些第三方發令牌
* 此時 demo application 中
* security.oauth2.client.client-id=whale
* security.oauth2.client.client-secret=123456
* 將失效
*
* 相關配置從此方法讀取
*
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
InMemoryClientDetailsServiceBuilder builder = clients.inMemory();
OAuth2ClientProperties[] clientProperties = securityProperties.getOauth2().getClients();
if(ArrayUtils.isNotEmpty(clientProperties)){
for (OAuth2ClientProperties client : clientProperties) {
builder.withClient(client.getClientId())
.secret(client.getClientSecret())
.accessTokenValiditySeconds(client.getAccessTokenValiditySeconds())
.authorizedGrantTypes("refresh_token","password") //支持哪些授權模式 token刷新和密碼模式
.scopes("all","read","write") ; //相當於權限,配置了這個參數,請求裏面可以不帶scope參,如果帶了參數,必須在配置的這個scope範圍之內
}
}
}
demo application 配置屬性
whale.security.oauth2.clients[0].clientId = whale
whale.security.oauth2.clients[0].clientSecret = 123456
whale.security.oauth2.clients[0].accessTokenValiditySeconds = 3600
測試
令牌的存儲
我們的token令牌生成後,服務如果重啓,令牌就會失效,因爲我們的令牌是存儲到內存裏面的
解決 用redis存儲令牌
redis好處就是我們不用維護數據表,且性能比較好
TokenStoreConfig
@Configuration
public class TokenStoreConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public TokenStore tokenStore(){
return new RedisTokenStore(redisConnectionFactory);
}
}
WhaleAuthenticationServiceConfig
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
// super.configure(endpoints);
// endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService);
endpoints.tokenStore(tokenStore)
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
}
測試
報錯
org.springframework.data.redis.connection.RedisConnection.set([B[B)V
解決
https://blog.csdn.net/smollsnail/article/details/78954225
-
spring-data-redis 2.0版本中set(String,String)被棄用了,要使用RedisConnection.stringCommands().set(…),所有我自定義一個RedisTokenStore,代碼和RedisTokenStore一樣,只是把所有conn.set(…)都換成conn…stringCommands().set(…)
-
最簡單的方法就是直接在pom裏面使用新版本如spring-security-oauth2–2.3.3.RELEASE,這樣就能覆蓋老的引用,新版本已經fix這個問題
我是直接在core 的pom中引入spring-security-oauth2–2.3.3.RELEASE的依賴
然而依賴中的版本號還是原有的版本號,沒有變化
又重新回看了所有pom文件
發現我們的頂級pom中配置了dependencyManagement,它是優先級最高的依賴
並且如果我們的子pom文件中沒有寫依賴版本號,他會向上尋找在父pom 中的dependencyManagement中確定版本號
所有我把spring-security-oauth2–2.3.3.RELEASE的依賴直接寫在dependencyManagement的第一行,優先級最高了
父pom
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
`````````````````
最後回到測試 成功請求
redis裏面也有了數據