Spring LocalVariableTableParameterNameDiscoverer 获取方法的参数名称
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* 参数拦截器
*/
public class ParamInterceptor extends HandlerInterceptorAdapter {
private static final LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 解决跨域
("Access-Control-Allow-Origin", "*");
HandlerMethod handlerMethod = (HandlerMethod) handler;
Auth authMethod = (handlerMethod);
Auth authClass = (handlerMethod);
boolean accountLogin = this.checkParamSign(authMethod, authClass);
//accountLogin=true;
if (accountLogin && handlerMethod != null) {
String[] methodParams = getMethodParams(().getName(), ().getName());
if (methodParams != null && > 0) {
TreeMap<String, String> map = new TreeMap<String, String>();
for (String paramName : methodParams) {
String value = (paramName);
(paramName + "=" + value);
if ((paramName) && (value)) {
(paramName, value);
}else{
("签名失败")
return false;
}
}
("sign", ("sign"));
("去签名");
boolean signFlag = (map);
if (!signFlag) {
("签名失败")
return false;
}
}
}
return super.preHandle(request, response, handler);
}
/**
* 获取方法所有参数名
* @param method
* @return
*/
public static String[] getParameterNames(Method method) {
return (method);
}
public static String[] getMethodParams(String pkgeName, String methodName) throws ClassNotFoundException {
Class<?> aClass = (pkgeName);
Method[] methods = ();
String[] params = null;
for (Method method : methods) {
if (().equals(methodName)) {
params = getParameterNames(method);
if (params == null || == 0) {
break;
}
StringBuilder sb = new StringBuilder();
("方法:" + () + "() ");
for (int i = 0; i < ; i++) {
if (i > 0) {
(" ,");
}
(params[i]);
}
(());
break;
}
}
return params;
}
/**
* 参数签名校验
*/
private boolean checkParamSign(Auth authMethod, Auth authClass) {
if (authMethod != null) {
return ();
}
if (authClass != null) {
return ();
}
return Auth.checkParamSign_default;
}
}