重庆官方网站查询系统,wordpress 菜单 宽度,电视直播网站建设,沧州建设工程信息网用户信息必须由后端获取#xff0c;不能通过前端传入的id是不可信的#xff0c;#xff0c;可能会出现越权的问题#xff0c;#xff0c;#xff0c;怎么通过后端获取当前登录用户#xff0c;#xff0c;#xff0c; 就需要将User 和 当前线程绑定在一起#xff0c;不能通过前端传入的id是不可信的可能会出现越权的问题怎么通过后端获取当前登录用户 就需要将User 和 当前线程绑定在一起因为Servlet中的每一个请求线程都是不同的 在拦截器中拦截token将token中的用户信息存入ThreadLocal中在拦截器执行过后的方法中清除ThreadLocal防止内存泄露
public class LocalUser {private static ThreadLocalMapString,Object threadLocal new ThreadLocalMapString, Object();public static void set(User user,Integer scope){HashMapString, Object map new HashMap();map.put(user,user);map.put(scope,scope);LocalUser.threadLocal.set(map);}public static User getUser(){MapString, Object map LocalUser.threadLocal.get();return (User) map.get(user);}public static Integer getScope(){MapString, Object map LocalUser.threadLocal.get();return (Integer) map.get(scope);}public static void clear(){LocalUser.threadLocal.remove();}
}