Commit 1f1eaf35 by 陶光胜

init

parent ce8125f4
package com.gic.enterprise.filter;
import com.alibaba.fastjson.JSON;
import com.gic.enterprise.constants.Constants;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;
@Activate(group = {CommonConstants.PROVIDER})
public class ServiceValidateFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
String token = invocation.getAttachment(Constants.USER_TOKEN);
RpcContext.getContext();
if(StringUtils.isNotBlank(token)){
RpcContext.getContext().setAttachment(Constants.USER_TOKEN, token);
}
System.out.println("profilter attachments"+ JSON.toJSONString(invocation.getAttachments()));
System.out.println("参数:"+JSON.toJSONString(invocation.getArguments()));
Class<?>[] parameterTypes = invocation.getParameterTypes();
String methodName = invocation.getMethodName();
Method[] declaredMethods = invoker.getInterface().getDeclaredMethods();
for(Method method : declaredMethods){
if(method.getName().equals(methodName) && checkMethodParameter(parameterTypes, method.getParameterTypes())){
for(int i=0; i<parameterTypes.length; i++){
Class<?> parameterType = parameterTypes[i];
Annotation[] annotations = parameterType.getAnnotations();
}
}
}
return invoker.invoke(invocation);
}
private boolean checkMethodParameter(Class<?>[] parameterTypes, Class<?>[] parameterTypes2){
if(parameterTypes.length != parameterTypes2.length){
return false;
}
for(int i=0; i<parameterTypes.length; i++){
if(!parameterTypes[i].equals(parameterTypes2[i])){
return false;
}
}
return true;
}
}
package com.gic.enterprise.filter;
import com.alibaba.fastjson.JSON;
import com.gic.enterprise.utils.UserDetailUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import java.util.Map;
@Activate(group = {CommonConstants.PROVIDER})
public class UserDetailFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
RpcContext context = RpcContext.getContext();
Map<String, String> attachments = context.getAttachments();
System.out.println("filter attachments"+ JSON.toJSONString(attachments));
return invoker.invoke(invocation);
}
}
package com.gic.enterprise.filter;
import com.gic.enterprise.constants.Constants;
import com.gic.enterprise.utils.TokenUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.filter.ConsumerContextFilter;
@Activate(group = {CommonConstants.CONSUMER}, order = -1000)
public class UserTokenFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
String token = invocation.getAttachment(Constants.USER_TOKEN);
RpcContext rpcContext = RpcContext.getContext();
if(StringUtils.isNotBlank(token)){
TokenUtils.set(token);
RpcContext.getContext().setAttachment(Constants.USER_TOKEN, token);
}else {
RpcContext.getContext().setAttachment(Constants.USER_TOKEN, TokenUtils.get());
}
try{
return invoker.invoke(invocation);
}finally {
TokenUtils.remove();
}
}
}
package com.gic.enterprise.utils;
public class TokenUtils {
private static ThreadLocal<String> tokenThreadLocal = new ThreadLocal<>();
public static void set(String token){
tokenThreadLocal.set(token);
}
public static String get(){
String token = tokenThreadLocal.get();
return token;
}
public static void remove(){
tokenThreadLocal.remove();
}
}
......@@ -9,4 +9,8 @@ public class UserDetailUtils {
public static void setUserDetail(UserDetail userDetail){
UserContext.getContext().init(userDetail);
}
public static void destory(){
UserContext.getContext().destory();
}
}
userDetailFilter=com.gic.enterprise.filter.UserDetailFilter
\ No newline at end of file
userTokenFilter=com.gic.enterprise.filter.UserTokenFilter
serviceValidateFilter=com.gic.enterprise.filter.ServiceValidateFilter
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
package com.gic.enterprise.interceptor;
import com.alibaba.fastjson.JSON;
import com.gic.enterprise.constants.Constants;
import com.gic.enterprise.context.UserContext;
import com.gic.enterprise.utils.UserDetail;
......@@ -22,7 +23,8 @@ public class AuthInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
String token = UserContext.getContext().getToken();
RpcContext.getContext().set(Constants.USER_TOKEN, token);
RpcContext.getContext().getAttachments().put(Constants.USER_TOKEN, token);
System.out.println(JSON.toJSONString(RpcContext.getContext().getArguments()));
return true;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment