增加Token管理模块(仅限于redis token模式):"认证管理-token管理"
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.central.oauth.controller;
|
||||
|
||||
import com.central.common.annotation.LoginClient;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.oauth.model.TokenVo;
|
||||
import com.central.oauth.service.ITokensService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* token管理接口
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Api(tags = "Token管理")
|
||||
@RestController
|
||||
@RequestMapping("/tokens")
|
||||
public class TokensController {
|
||||
@Autowired
|
||||
private ITokensService tokensService;
|
||||
|
||||
@GetMapping("/list")
|
||||
@ApiOperation(value = "token列表")
|
||||
public PageResult<TokenVo> list(@RequestParam Map<String, Object> params, @LoginClient String clientId) {
|
||||
return tokensService.listTokens(params, clientId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.central.oauth.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class TokenVo implements Serializable {
|
||||
private static final long serialVersionUID = -6656955957477645319L;
|
||||
/**
|
||||
* token的值
|
||||
*/
|
||||
private String tokenValue;
|
||||
/**
|
||||
* 到期时间
|
||||
*/
|
||||
private Date expiration;
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 所属应用
|
||||
*/
|
||||
private String clientId;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.central.oauth.service;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.oauth.model.TokenVo;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
public interface ITokensService {
|
||||
/**
|
||||
* 查询token列表
|
||||
* @param params 请求参数
|
||||
* @param clientId 应用id
|
||||
*/
|
||||
PageResult<TokenVo> listTokens(Map<String, Object> params, String clientId);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.central.oauth.service.impl;
|
||||
|
||||
import cn.hutool.core.util.PageUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.redis.template.RedisRepository;
|
||||
import com.central.oauth.model.TokenVo;
|
||||
import com.central.oauth.service.ITokensService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* token管理服务(redis token)
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/7/12
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class RedisTokensServiceImpl implements ITokensService {
|
||||
@Autowired
|
||||
private RedisRepository redisRepository;
|
||||
|
||||
@Override
|
||||
public PageResult<TokenVo> listTokens(Map<String, Object> params, String clientId) {
|
||||
Integer page = MapUtils.getInteger(params, "page");
|
||||
Integer limit = MapUtils.getInteger(params, "limit");
|
||||
int[] startEnds = PageUtil.transToStartEnd(page, limit);
|
||||
//根据请求参数生成redis的key
|
||||
String redisKey = getRedisKey(params, clientId);
|
||||
long size = redisRepository.length(redisKey);
|
||||
List<TokenVo> result = new ArrayList<>(limit);
|
||||
//查询token集合
|
||||
List<Object> tokenObjs = redisRepository.getList(redisKey, startEnds[0], startEnds[1]-1);
|
||||
if (tokenObjs != null) {
|
||||
for (Object obj : tokenObjs) {
|
||||
DefaultOAuth2AccessToken accessToken = (DefaultOAuth2AccessToken)obj;
|
||||
//获取用户信息
|
||||
Object authObj = redisRepository.get(SecurityConstants.REDIS_TOKEN_AUTH + accessToken.getValue());
|
||||
OAuth2Authentication authentication = (OAuth2Authentication)authObj;
|
||||
//构造token对象
|
||||
TokenVo tokenVo = new TokenVo();
|
||||
tokenVo.setClientId(clientId);
|
||||
tokenVo.setTokenValue(accessToken.getValue());
|
||||
tokenVo.setExpiration(accessToken.getExpiration());
|
||||
tokenVo.setUsername(authentication.getName());
|
||||
result.add(tokenVo);
|
||||
}
|
||||
}
|
||||
return PageResult.<TokenVo>builder().data(result).code(0).count(size).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据请求参数生成redis的key
|
||||
*/
|
||||
private String getRedisKey(Map<String, Object> params, String clientId) {
|
||||
String result;
|
||||
String username = MapUtils.getString(params, "username");
|
||||
if (StrUtil.isNotEmpty(username)) {
|
||||
result = SecurityConstants.REDIS_UNAME_TO_ACCESS + clientId + ":" + username;
|
||||
} else {
|
||||
result = SecurityConstants.REDIS_CLIENT_ID_TO_ACCESS + clientId;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,8 @@ zlt:
|
||||
# 忽略认证的地址
|
||||
httpUrls: >
|
||||
/validata/**,
|
||||
/clients,
|
||||
/clients/**,
|
||||
/tokens/**,
|
||||
/login.html,
|
||||
/css/**,
|
||||
/images/**,
|
||||
|
||||
Reference in New Issue
Block a user