單點登陸的實現(xiàn)方式有多種:
1椎椰、基于cas來做
2离咐、spring cloud oauth2的spring全家桶
3绊汹、自定義jwt(不推薦)
下面我們基于spring全家桶來做一個單點登陸系統(tǒng)2019年12月23日最新springboot版本2.2.2.RELEASE腿倚;由于篇幅問題昏滴,我們分成兩篇文章。
基于上一篇http://www.reibang.com/p/80b125ea8e76
源碼:https://github.com/xcocean/spring-cloud-sso
第三篇是對springcloud-sso的額外補充
前言
關(guān)于springcloud中的異常累颂,可以在網(wǎng)關(guān)中處理滞详,也可以在當前服務(wù)中攔截處理,返回統(tǒng)一結(jié)果紊馏。推薦既在網(wǎng)關(guān)中處理料饥,也在元服務(wù)中處理。
1朱监、token自定義key
oauth2默認的token生成是UUID岸啡,使用jwt一大推又不好,所以我們自定義登陸成功生成的access_token
赌朋。自定義TokenEnhancer
即可凰狞。
CustomTokenEnhancer.java
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import java.util.HashMap;
import java.util.Map;
public class CustomTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
OAuth2Authentication authentication) {
if (accessToken instanceof DefaultOAuth2AccessToken) {
DefaultOAuth2AccessToken token = ((DefaultOAuth2AccessToken) accessToken);
//自定義自己的 access_token
token.setValue(customToken());
OAuth2RefreshToken refreshToken = token.getRefreshToken();
if (refreshToken instanceof DefaultOAuth2RefreshToken) {
//自定義生成的 refreshToken
token.setRefreshToken(new DefaultOAuth2RefreshToken(customRefreshToken()));
}
Map<String, Object> additionalInformation = new HashMap<String, Object>();
additionalInformation.put("client_id", authentication.getOAuth2Request().getClientId());
token.setAdditionalInformation(additionalInformation);
return token;
}
return accessToken;
}
private static int i = 0;
private static int j = 0;
/**
* 自定義token的值
* 集群的時候注意不要生成重復的值即可
*/
private String customToken() {
i = i + 1;
return "666" + i;
}
/**
* 自定義token的值
* 集群的時候注意不要生成重復的值即可
*/
private String customRefreshToken() {
j = j + 1;
return "888" + i;
}
}
AuthorizationServerConfiguration.java中注入
// 注入自定義令牌生成
@Bean
public TokenEnhancer tokenEnhancer() {
return new CustomTokenEnhancer();
}
// ...
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
// 用于支持密碼模式
.authenticationManager(authenticationManager)
.tokenStore(tokenStore())
// 自定義token生成規(guī)則
.tokenEnhancer(tokenEnhancer())
.userDetailsService(userDetailsService);
}
效果如下:
2、刷新令牌
刷新令牌的原理是再次調(diào)用登陸UserDetailsService
需要授權(quán)注入沛慢。在上一篇文章中講到的AuthorizationServerConfiguration.java
中注入我們自定義的UserDetailsServiceImpl
// 我們自定義的登陸UserDetailsService
@Autowired
private UserDetailsServiceImpl userDetailsService;
// .....
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
// 用于支持密碼模式
.authenticationManager(authenticationManager)
.tokenStore(tokenStore())
// 此處注入我們自定義的userDetailsService用于token的刷新
.userDetailsService(userDetailsService);
}
如果想刷新令牌赡若,還是調(diào)用獲取token的url,只需將grant_type
換成refresh_token团甲,post請求如下:
http://localhost:9000/oauth/token
參數(shù):
refresh_token:49eb4217-062f-47bc-956a-5d3f4684418a
client_id:client
client_secret:secret
grant_type:refresh_token
結(jié)果如下:
3逾冬、注銷的實現(xiàn)
注銷直接調(diào)用consumerTokenServices.revokeToken(token)
即可,該注銷將把token
和refresh_token
一并移除掉躺苦,可觀察redis的值身腻。
@Autowired
private ConsumerTokenServices consumerTokenServices;
@GetMapping("/user/logout")
public ResponseResult<Void> logout(HttpServletRequest request) {
// 獲取 token
String token = request.getParameter("access_token");
if (token == null) {
token = request.getHeader("authorization").split(" ")[1];
}
// 刪除 token 以注銷
consumerTokenServices.revokeToken(token);
return new ResponseResult<Void>(CodeStatus.OK, "用戶已注銷");
}
然后GET調(diào)用:http://localhost:8080/user/logout?access_token=7ed1b398-6db5-477b-80e7-783de1bf72d7
4、檢查令牌是否有效
直接Get訪問即可:http://localhost:9000/oauth/check_token?token=2580d87d-f162-4f19-8a8f-238a4f91ae54
無效時: