增加Token管理模块(仅限于redis token模式):"认证管理-token管理"
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
package com.central.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 请求的方法参数上添加该注解,则注入当前登录账号的应用id
|
||||
* 例:public void test(@LoginClient String clientId) //注入webApp
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/7/24 16:44
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface LoginClient {
|
||||
}
|
||||
+4
@@ -1,6 +1,7 @@
|
||||
package com.central.common.config;
|
||||
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.resolver.ClientArgumentResolver;
|
||||
import com.central.common.resolver.TokenArgumentResolver;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
@@ -26,6 +27,9 @@ public class LoginArgResolverConfig implements WebMvcConfigurer {
|
||||
*/
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
//注入用户信息
|
||||
argumentResolvers.add(new TokenArgumentResolver(userService));
|
||||
//注入应用信息
|
||||
argumentResolvers.add(new ClientArgumentResolver());
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -21,6 +21,11 @@ public interface SecurityConstants {
|
||||
*/
|
||||
String ROLE_HEADER = "x-role-header";
|
||||
|
||||
/**
|
||||
* 应用信息头
|
||||
*/
|
||||
String CLIENT_HEADER = "x-client-header";
|
||||
|
||||
/**
|
||||
* 基础角色
|
||||
*/
|
||||
@@ -140,4 +145,16 @@ public interface SecurityConstants {
|
||||
* 默认token过期时间(1小时)
|
||||
*/
|
||||
Integer ACCESS_TOKEN_VALIDITY_SECONDS = 60 * 60;
|
||||
/**
|
||||
* redis中授权token对应的key
|
||||
*/
|
||||
String REDIS_TOKEN_AUTH = "auth:";
|
||||
/**
|
||||
* redis中应用对应的token集合的key
|
||||
*/
|
||||
String REDIS_CLIENT_ID_TO_ACCESS = "client_id_to_access:";
|
||||
/**
|
||||
* redis中用户名对应的token集合的key
|
||||
*/
|
||||
String REDIS_UNAME_TO_ACCESS = "uname_to_access:";
|
||||
}
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.central.common.resolver;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.annotation.LoginClient;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* head中的应用参数注入clientId中
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/7/10
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
/**
|
||||
* 入参筛选
|
||||
*
|
||||
* @param methodParameter 参数集合
|
||||
* @return 格式化后的参数
|
||||
*/
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter methodParameter) {
|
||||
return methodParameter.hasParameterAnnotation(LoginClient.class) && methodParameter.getParameterType().equals(String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param methodParameter 入参集合
|
||||
* @param modelAndViewContainer model 和 view
|
||||
* @param nativeWebRequest web相关
|
||||
* @param webDataBinderFactory 入参解析
|
||||
* @return 包装对象
|
||||
*/
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter methodParameter,
|
||||
ModelAndViewContainer modelAndViewContainer,
|
||||
NativeWebRequest nativeWebRequest,
|
||||
WebDataBinderFactory webDataBinderFactory) {
|
||||
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
|
||||
String clientId = request.getHeader(SecurityConstants.CLIENT_HEADER);
|
||||
if (StrUtil.isBlank(clientId)) {
|
||||
log.warn("resolveArgument error clientId is empty");
|
||||
}
|
||||
return clientId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user