|
|
@ -14,6 +14,7 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.cache.CacheManager; |
|
|
|
import org.springframework.cache.CacheManager; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize; |
|
|
|
import org.springframework.security.authentication.event.LogoutSuccessEvent; |
|
|
|
import org.springframework.security.authentication.event.LogoutSuccessEvent; |
|
|
|
import org.springframework.security.oauth2.common.OAuth2AccessToken; |
|
|
|
import org.springframework.security.oauth2.common.OAuth2AccessToken; |
|
|
|
import org.springframework.security.oauth2.common.OAuth2RefreshToken; |
|
|
|
import org.springframework.security.oauth2.common.OAuth2RefreshToken; |
|
|
@ -44,7 +45,7 @@ import java.util.stream.Collectors; |
|
|
|
@RestController |
|
|
|
@RestController |
|
|
|
@RequiredArgsConstructor |
|
|
|
@RequiredArgsConstructor |
|
|
|
@RequestMapping("/token") |
|
|
|
@RequestMapping("/token") |
|
|
|
public class kiccTokenEndpoint { |
|
|
|
public class KiccTokenEndpoint { |
|
|
|
|
|
|
|
|
|
|
|
private final ClientDetailsService clientDetailsService; |
|
|
|
private final ClientDetailsService clientDetailsService; |
|
|
|
|
|
|
|
|
|
|
@ -98,11 +99,19 @@ public class kiccTokenEndpoint { |
|
|
|
@DeleteMapping("/logout") |
|
|
|
@DeleteMapping("/logout") |
|
|
|
public R<Boolean> logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader) { |
|
|
|
public R<Boolean> logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader) { |
|
|
|
if (StrUtil.isBlank(authHeader)) { |
|
|
|
if (StrUtil.isBlank(authHeader)) { |
|
|
|
return R.ok(); |
|
|
|
return R.error("退出失败,token 为空"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String tokenValue = authHeader.replace(OAuth2AccessToken.BEARER_TYPE, StrUtil.EMPTY).trim(); |
|
|
|
String tokenValue = authHeader.replace(OAuth2AccessToken.BEARER_TYPE, StrUtil.EMPTY).trim(); |
|
|
|
return removeToken(tokenValue); |
|
|
|
OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue); |
|
|
|
|
|
|
|
if (accessToken == null || StrUtil.isBlank(accessToken.getValue())) { |
|
|
|
|
|
|
|
return R.error("退出失败,token 无效"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 清空access token
|
|
|
|
|
|
|
|
tokenStore.removeAccessToken(accessToken); |
|
|
|
|
|
|
|
// 清空 refresh token
|
|
|
|
|
|
|
|
OAuth2RefreshToken refreshToken = accessToken.getRefreshToken(); |
|
|
|
|
|
|
|
tokenStore.removeRefreshToken(refreshToken); |
|
|
|
|
|
|
|
return R.ok(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -111,21 +120,22 @@ public class kiccTokenEndpoint { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Inner |
|
|
|
@Inner |
|
|
|
@DeleteMapping("/{token}") |
|
|
|
@DeleteMapping("/{token}") |
|
|
|
|
|
|
|
@PreAuthorize("@ps.hasPerm('token_del')") |
|
|
|
public R<Boolean> removeToken(@PathVariable("token") String token) { |
|
|
|
public R<Boolean> removeToken(@PathVariable("token") String token) { |
|
|
|
OAuth2AccessToken accessToken = tokenStore.readAccessToken(token); |
|
|
|
OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(token); |
|
|
|
if (accessToken == null || StrUtil.isBlank(accessToken.getValue())) { |
|
|
|
if (oAuth2AccessToken == null || StrUtil.isBlank(oAuth2AccessToken.getValue())) { |
|
|
|
return R.ok(); |
|
|
|
return R.ok(Boolean.TRUE, "操作失败,token 无效"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
OAuth2Authentication auth2Authentication = tokenStore.readAuthentication(accessToken); |
|
|
|
OAuth2Authentication auth2Authentication = tokenStore.readAuthentication(oAuth2AccessToken); |
|
|
|
// 清空用户信息
|
|
|
|
// 清空用户信息
|
|
|
|
cacheManager.getCache(CacheConstants.USER_DETAILS).evict(auth2Authentication.getName()); |
|
|
|
cacheManager.getCache(CacheConstants.USER_DETAILS).evict(auth2Authentication.getName()); |
|
|
|
|
|
|
|
|
|
|
|
// 清空access token
|
|
|
|
// 清空access token
|
|
|
|
tokenStore.removeAccessToken(accessToken); |
|
|
|
tokenStore.removeAccessToken(oAuth2AccessToken); |
|
|
|
|
|
|
|
|
|
|
|
// 清空 refresh token
|
|
|
|
// 清空 refresh token
|
|
|
|
OAuth2RefreshToken refreshToken = accessToken.getRefreshToken(); |
|
|
|
OAuth2RefreshToken refreshToken = oAuth2AccessToken.getRefreshToken(); |
|
|
|
tokenStore.removeRefreshToken(refreshToken); |
|
|
|
tokenStore.removeRefreshToken(refreshToken); |
|
|
|
|
|
|
|
|
|
|
|
// 处理自定义退出事件,保存相关日志
|
|
|
|
// 处理自定义退出事件,保存相关日志
|
|
|
@ -140,9 +150,10 @@ public class kiccTokenEndpoint { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Inner |
|
|
|
@Inner |
|
|
|
@PostMapping("/list") |
|
|
|
@PostMapping("/list") |
|
|
|
|
|
|
|
@PreAuthorize("@ps.hasPerm('token_view')") |
|
|
|
public R tokenList(@RequestBody Map<String, Object> params) { |
|
|
|
public R tokenList(@RequestBody Map<String, Object> params) { |
|
|
|
// 根据分页参数获取对应数据
|
|
|
|
// 根据分页参数获取对应数据
|
|
|
|
String key = String.format("%sauth_to_access:*", CacheConstants.OAUTH_ACCESS); |
|
|
|
String key = String.format("%south_to_access:*", CacheConstants.OAUTH_ACCESS); |
|
|
|
int current = MapUtil.getInt(params, CommonConstants.CURRENT); |
|
|
|
int current = MapUtil.getInt(params, CommonConstants.CURRENT); |
|
|
|
int size = MapUtil.getInt(params, CommonConstants.SIZE); |
|
|
|
int size = MapUtil.getInt(params, CommonConstants.SIZE); |
|
|
|
Set<String> keys = redisTemplate.keys(key); |
|
|
|
Set<String> keys = redisTemplate.keys(key); |