CREATE TABLE `t_right` ( `id` int(11) NOT NULL AUTO_INCREMENT, `common` bit(1) DEFAULT NULL, `right_code` bigint(20) DEFAULT NULL, `right_desc` varchar(255) DEFAULT NULL, `right_name` varchar(255) DEFAULT NULL, `right_pos` int(11) DEFAULT NULL, `right_url` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 CREATE TABLE `t_role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `role_desc` varchar(255) DEFAULT NULL, `role_name` varchar(255) DEFAULT NULL, `role_value` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 CREATE TABLE `t_role_right` ( `id` int(11) NOT NULL AUTO_INCREMENT, `role_id` int(11) DEFAULT NULL, `right_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK2A2639BE64C16BC1` (`role_id`), KEY `FK2A2639BEEDAF4F53` (`right_id`), CONSTRAINT `FK2A2639BEEDAF4F53` FOREIGN KEY (`right_id`) REFERENCES `t_right` (`id`), CONSTRAINT `FK2A2639BE64C16BC1` FOREIGN KEY (`role_id`) REFERENCES `t_role` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `tel` varchar(255) DEFAULT NULL, `sex` varchar(255) DEFAULT NULL, `description` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 CREATE TABLE `t_user_role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) DEFAULT NULL, `role_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK331DEE5F9559BAB9` (`role_id`), KEY `FK331DEE5F9EC2FA1` (`user_id`), CONSTRAINT `FK331DEE5F9EC2FA1` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`), CONSTRAINT `FK331DEE5F9559BAB9` FOREIGN KEY (`role_id`) REFERENCES `t_right` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
@Entity @Table(name = "t_right") public class Right extends BaseEntity implements Serializable{ /** * */ private static final long serialVersionUID = 1444825234975317847L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "right_name") private String rightName = ""; @Column(name = "right_url") private String rightUrl; @Column(name = "right_desc") private String rightDesc; @Column(name = "right_code") private long rightCode; @Column(name = "right_pos") private int rightPos; @Column(name = "common") private boolean common ; public int getId() { return id; } public String getRightName() { return rightName; } public String getRightUrl() { return rightUrl; } public String getRightDesc() { return rightDesc; } public long getRightCode() { return rightCode; } public int getRightPos() { return rightPos; } public boolean isCommon() { return common; } public void setId(int id) { this.id = id; } public void setRightName(String rightName) { this.rightName = rightName; } public void setRightUrl(String rightUrl) { this.rightUrl = rightUrl; } public void setRightDesc(String rightDesc) { this.rightDesc = rightDesc; } public void setRightCode(long rightCode) { this.rightCode = rightCode; } public void setRightPos(int rightPos) { this.rightPos = rightPos; } public void setCommon(boolean common) { this.common = common; } }
@Entity @Table(name = "t_role") public class Role extends BaseEntity implements Serializable{ /** * */ private static final long serialVersionUID = -3249248953909188737L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "role_name") private String roleName; @Column(name = "role_value") private String roleValue; @Column(name = "role_desc") private String roleDesc; @ManyToMany(targetEntity = Right.class, fetch = FetchType.LAZY) @JoinTable(name = "t_role_right", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "right_id")) private List<Right> rights; public int getId() { return id; } public String getRoleName() { return roleName; } public String getRoleValue() { return roleValue; } public String getRoleDesc() { return roleDesc; } public List<Right> getRights() { return rights; } public void setId(int id) { this.id = id; } public void setRoleName(String roleName) { this.roleName = roleName; } public void setRoleValue(String roleValue) { this.roleValue = roleValue; } public void setRoleDesc(String roleDesc) { this.roleDesc = roleDesc; } public void setRights(List<Right> rights) { this.rights = rights; } }
@Entity @Table(name = "t_user") public class User extends BaseEntity implements Serializable{ /** * */ private static final long serialVersionUID = -1461963356403533227L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "user_name") private String userName; @Column(name = "password") private String password; @Column(name = "tel") private String tel; @Column(name = "sex") private String sex; @Column(name = "description") private String description; @ManyToMany(targetEntity = Right.class, fetch = FetchType.LAZY) @JoinTable(name = "t_user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) private List<Role> roles; @Transient private long[] rightSum; public List<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; } public String getUserName() { return userName; } public String getPassword() { return password; } public String getTel() { return tel; } public String getSex() { return sex; } public String getDescription() { return description; } public void setUserName(String userName) { this.userName = userName; } public void setPassword(String password) { this.password = password; } public void setTel(String tel) { this.tel = tel; } public void setSex(String sex) { this.sex = sex; } public void setDescription(String description) { this.description = description; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((description == null) ? 0 : description.hashCode()); result = prime * result + id; result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((sex == null) ? 0 : sex.hashCode()); result = prime * result + ((tel == null) ? 0 : tel.hashCode()); result = prime * result + ((userName == null) ? 0 : userName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (description == null) { if (other.description != null) return false; } else if (!description.equals(other.description)) return false; if (id != other.id) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (sex == null) { if (other.sex != null) return false; } else if (!sex.equals(other.sex)) return false; if (tel == null) { if (other.tel != null) return false; } else if (!tel.equals(other.tel)) return false; if (userName == null) { if (other.userName != null) return false; } else if (!userName.equals(other.userName)) return false; return true; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", tel=" + tel + ", sex=" + sex + ", description=" + description + "]"; } public void calculateRightSum() { int pos = 0; long code = 0; for(Role role: roles){ if("-1".equals(role.getRoleValue())){ roles = null; return; } for(Right right: role.getRights()){ pos = right.getRightPos(); code = right.getRightCode(); rightSum[pos] = rightSum[pos] | code; } } roles = null; } public boolean hasRight(Right r){ int pos = r.getRightPos(); long code = r.getRightCode(); return !((rightSum[pos] & code) == 0); } }
public class RightFilterInterceptor implements HandlerInterceptor { @Resource RightService rightService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String requestUri = request.getRequestURI(); String contextPath = request.getContextPath(); String url = requestUri.substring(contextPath.length()); int hasRight = RightValidateUtil.hasRight(url, request); switch (hasRight) { case RightValidateUtil.NORIGHTS: response.sendRedirect(request.getContextPath() + "/web/user/error.action"); return false; case RightValidateUtil.NOLOGIN: response.sendRedirect(request.getContextPath() + "/web/user/login.action"); return false; case RightValidateUtil.HASRIGHTS: return true; default: return true; } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
@Component public class RightValidateUtil implements InitializingBean{ public static final int HASRIGHTS = 1; public static final int NORIGHTS = -1; public static final int NOLOGIN = 0; @Resource RightService rightService; public static Map<String,Right> map = new HashMap<String, Right>(); public static boolean isValid(String url) { return !(url == null || "".equals(url.trim())); } public static boolean isValid(Collection col) { if (col == null || col.isEmpty()) { return false; } return true; } public static boolean isValid(Object[] arr) { if (arr == null || arr.length == 0) { return false; } return true; } // -1 否 0 未登录 1 真 public static int hasRight(String url, HttpServletRequest request) { HttpSession session = request.getSession(); // Map<String, Right> map = (Map<String, Right>) session.getAttribute("all_rights_map"); Right r = map.get(url); if (r == null || r.isCommon()) { return RightValidateUtil.HASRIGHTS; } else { User user = (User) session.getAttribute(Constants.USER); if (user == null) { return RightValidateUtil.NOLOGIN; } else { if (user.hasRight(r)) { return RightValidateUtil.HASRIGHTS; } else { return RightValidateUtil.NORIGHTS; } } } } @Override public void afterPropertiesSet() throws Exception { map.clear(); List<Right> rights = rightService.findAll(); for (Right right : rights) { map.put(right.getRightUrl(), right) ; } } }
<mvc:interceptor> <mvc:mapping path="/web/**" /> <bean class="com.curiousby.baoyou.cn.interceptor.RightFilterInterceptor" /> </mvc:interceptor>
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!