增加@RestUserid 废弃 HttpRequest.currentUser() 方法, 建议 HttpRequest.currentUserid()
This commit is contained in:
@@ -72,6 +72,8 @@ public class HttpRequest extends Request<HttpContext> {
|
|||||||
|
|
||||||
protected Annotation[] annotations;
|
protected Annotation[] annotations;
|
||||||
|
|
||||||
|
protected Serializable currentUserid;
|
||||||
|
|
||||||
protected Object currentUser;
|
protected Object currentUser;
|
||||||
|
|
||||||
private final ByteArray array = new ByteArray();
|
private final ByteArray array = new ByteArray();
|
||||||
@@ -273,6 +275,34 @@ public class HttpRequest extends Request<HttpContext> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 设置当前用户ID, 通常在HttpServlet.preExecute方法里设置currentUserid <br>
|
||||||
|
* 数据类型通常是int、long、String
|
||||||
|
*
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @param user 用户信息
|
||||||
|
*
|
||||||
|
* @return HttpRequest
|
||||||
|
*/
|
||||||
|
public <T extends Serializable> HttpRequest setCurrentUserid(T userid) {
|
||||||
|
this.currentUserid = userid;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前用户ID<br>
|
||||||
|
*
|
||||||
|
* @param <T> 通常是int、long、String类型
|
||||||
|
*
|
||||||
|
* @return 用户信息
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T extends Serializable> T currentUserid() {
|
||||||
|
return (T) this.currentUserid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Deprecated
|
||||||
|
* 建议使用 setCurrentUserid, 通过userid从Service或缓存中获取用户信息<br>
|
||||||
* 设置当前用户信息, 通常在HttpServlet.preExecute方法里设置currentUser <br>
|
* 设置当前用户信息, 通常在HttpServlet.preExecute方法里设置currentUser <br>
|
||||||
* 数据类型由@HttpUserType指定
|
* 数据类型由@HttpUserType指定
|
||||||
*
|
*
|
||||||
@@ -281,12 +311,15 @@ public class HttpRequest extends Request<HttpContext> {
|
|||||||
*
|
*
|
||||||
* @return HttpRequest
|
* @return HttpRequest
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public <T> HttpRequest setCurrentUser(T user) {
|
public <T> HttpRequest setCurrentUser(T user) {
|
||||||
this.currentUser = user;
|
this.currentUser = user;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @Deprecated
|
||||||
|
* 建议使用 currentUserid, 通过userid从Service或缓存中获取用户信息<br>
|
||||||
* 获取当前用户信息<br>
|
* 获取当前用户信息<br>
|
||||||
* 数据类型由@HttpUserType指定
|
* 数据类型由@HttpUserType指定
|
||||||
*
|
*
|
||||||
@@ -294,6 +327,7 @@ public class HttpRequest extends Request<HttpContext> {
|
|||||||
*
|
*
|
||||||
* @return 用户信息
|
* @return 用户信息
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T currentUser() {
|
public <T> T currentUser() {
|
||||||
return (T) this.currentUser;
|
return (T) this.currentUser;
|
||||||
@@ -513,6 +547,7 @@ public class HttpRequest extends Request<HttpContext> {
|
|||||||
this.moduleid = 0;
|
this.moduleid = 0;
|
||||||
this.actionid = 0;
|
this.actionid = 0;
|
||||||
this.annotations = null;
|
this.annotations = null;
|
||||||
|
this.currentUserid = null;
|
||||||
this.currentUser = null;
|
this.currentUser = null;
|
||||||
|
|
||||||
this.attachment = null;
|
this.attachment = null;
|
||||||
|
|||||||
@@ -1067,6 +1067,18 @@ public final class Rest {
|
|||||||
comment = annuri.comment();
|
comment = annuri.comment();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RestUserid userid = param.getAnnotation(RestUserid.class);
|
||||||
|
if (userid != null) {
|
||||||
|
if (annhead != null) throw new RuntimeException("@RestUserid and @RestHeader cannot on the same Parameter in " + method);
|
||||||
|
if (anncookie != null) throw new RuntimeException("@RestUserid and @RestCookie cannot on the same Parameter in " + method);
|
||||||
|
if (annsid != null) throw new RuntimeException("@RestUserid and @RestSessionid cannot on the same Parameter in " + method);
|
||||||
|
if (annaddr != null) throw new RuntimeException("@RestUserid and @RestAddress cannot on the same Parameter in " + method);
|
||||||
|
if (annbody != null) throw new RuntimeException("@RestUserid and @RestBody cannot on the same Parameter in " + method);
|
||||||
|
if (annfile != null) throw new RuntimeException("@RestUserid and @RestUploadFile cannot on the same Parameter in " + method);
|
||||||
|
if (!ptype.isPrimitive() && !java.io.Serializable.class.isAssignableFrom(ptype)) throw new RuntimeException("@RestUserid must on java.io.Serializable Parameter in " + method);
|
||||||
|
comment = "";
|
||||||
|
}
|
||||||
|
|
||||||
RestParam annpara = param.getAnnotation(RestParam.class);
|
RestParam annpara = param.getAnnotation(RestParam.class);
|
||||||
if (annpara != null) radix = annpara.radix();
|
if (annpara != null) radix = annpara.radix();
|
||||||
if (annpara != null) comment = annpara.comment();
|
if (annpara != null) comment = annpara.comment();
|
||||||
@@ -1100,7 +1112,7 @@ public final class Rest {
|
|||||||
}
|
}
|
||||||
} while ((loop = loop.getSuperclass()) != Object.class);
|
} while ((loop = loop.getSuperclass()) != Object.class);
|
||||||
}
|
}
|
||||||
paramlist.add(new Object[]{param, n, ptype, radix, comment, required, annpara, annsid, annaddr, annhead, anncookie, annbody, annfile, annuri, param.getParameterizedType()});
|
paramlist.add(new Object[]{param, n, ptype, radix, comment, required, annpara, annsid, annaddr, annhead, anncookie, annbody, annfile, annuri, userid, param.getParameterizedType()});
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Object> mappingMap = new LinkedHashMap<>();
|
Map<String, Object> mappingMap = new LinkedHashMap<>();
|
||||||
@@ -1218,7 +1230,8 @@ public final class Rest {
|
|||||||
RestBody annbody = (RestBody) ps[11];
|
RestBody annbody = (RestBody) ps[11];
|
||||||
RestUploadFile annfile = (RestUploadFile) ps[12];
|
RestUploadFile annfile = (RestUploadFile) ps[12];
|
||||||
RestURI annuri = (RestURI) ps[13];
|
RestURI annuri = (RestURI) ps[13];
|
||||||
java.lang.reflect.Type pgentype = (java.lang.reflect.Type) ps[14];
|
RestUserid userid = (RestUserid) ps[14];
|
||||||
|
java.lang.reflect.Type pgentype = (java.lang.reflect.Type) ps[15];
|
||||||
|
|
||||||
final boolean ishead = annhead != null; //是否取getHeader 而不是 getParameter
|
final boolean ishead = annhead != null; //是否取getHeader 而不是 getParameter
|
||||||
final boolean iscookie = anncookie != null; //是否取getCookie
|
final boolean iscookie = anncookie != null; //是否取getCookie
|
||||||
@@ -1283,6 +1296,36 @@ public final class Rest {
|
|||||||
mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "getRequestURI", "()Ljava/lang/String;", false);
|
mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "getRequestURI", "()Ljava/lang/String;", false);
|
||||||
mv.visitVarInsn(ASTORE, maxLocals);
|
mv.visitVarInsn(ASTORE, maxLocals);
|
||||||
varInsns.add(new int[]{ALOAD, maxLocals});
|
varInsns.add(new int[]{ALOAD, maxLocals});
|
||||||
|
} else if (userid != null) { //HttpRequest.currentUserid
|
||||||
|
mv.visitVarInsn(ALOAD, 1);
|
||||||
|
mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "currentUserid", "()Ljava/io/Serializable;", false);
|
||||||
|
if (ptype == int.class) {
|
||||||
|
mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
|
||||||
|
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false);
|
||||||
|
mv.visitVarInsn(ISTORE, maxLocals);
|
||||||
|
varInsns.add(new int[]{ILOAD, maxLocals});
|
||||||
|
} else if (ptype == float.class) {
|
||||||
|
mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
|
||||||
|
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false);
|
||||||
|
mv.visitVarInsn(FSTORE, maxLocals);
|
||||||
|
varInsns.add(new int[]{FLOAD, maxLocals});
|
||||||
|
} else if (ptype == long.class) {
|
||||||
|
mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
|
||||||
|
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false);
|
||||||
|
mv.visitVarInsn(LSTORE, maxLocals);
|
||||||
|
varInsns.add(new int[]{LLOAD, maxLocals});
|
||||||
|
maxLocals++;
|
||||||
|
} else if (ptype == double.class) {
|
||||||
|
mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
|
||||||
|
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false);
|
||||||
|
mv.visitVarInsn(DSTORE, maxLocals);
|
||||||
|
varInsns.add(new int[]{DLOAD, maxLocals});
|
||||||
|
maxLocals++;
|
||||||
|
} else {
|
||||||
|
mv.visitTypeInsn(CHECKCAST, Type.getInternalName(ptype));
|
||||||
|
mv.visitVarInsn(ASTORE, maxLocals);
|
||||||
|
varInsns.add(new int[]{ALOAD, maxLocals});
|
||||||
|
}
|
||||||
} else if ("#".equals(pname)) { //从request.getRequstURI 中取参数
|
} else if ("#".equals(pname)) { //从request.getRequstURI 中取参数
|
||||||
if (ptype == boolean.class) {
|
if (ptype == boolean.class) {
|
||||||
mv.visitVarInsn(ALOAD, 1);
|
mv.visitVarInsn(ALOAD, 1);
|
||||||
|
|||||||
27
src/org/redkale/net/http/RestUserid.java
Normal file
27
src/org/redkale/net/http/RestUserid.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.net.http;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 只能注解于Service类的方法的参数或参数内的Serializable字段
|
||||||
|
* <p>
|
||||||
|
* 用于获取HTTP请求端的用户ID HttpRequest.currentUserid
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
@Inherited
|
||||||
|
@Documented
|
||||||
|
@Target({PARAMETER, FIELD})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface RestUserid {
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user