`

2010.04.08(3)———hibernate annotation 多对多和简单权限管理

阅读更多
2010.04.08(3)———hibernate annotation 多对多和简单权限管理

参考:http://www.iteye.com/topic/314635

user---多对多---group----多对多----action
用户------------角色---------------权限


数据库


//用户表
create table cp_user(
id varchar2(32) primary key,
name varchar2(50) unique not null,
password varchar2(50) not null
)

insert into cp_user values(1,'admin','admin');
insert into cp_user values(2,'cody','cody');
insert into cp_user values(3,'xiaodu','xiaodu');


//角色表
create table cp_group(
id varchar2(32) primary key,
name varchar2(50) unique not null

)

insert into cp_group values(1,'区领导');
insert into cp_group values(2,'科室领导');
insert into cp_group values(3,'科员');

//权限表
create table cp_action(
id varchar2(32) primary key,
name varchar2(200) unique not null,
p_id varchar2(32) not null,
url varchar2(100) not null unique
)

insert into cp_action values(1,'地图主页面',0,'map.jsp');
insert into cp_action values(2,'根据项目的图表展示',0,'chart1.jsp');
insert into cp_action values(3,'根据开工状态的图表展示',0,'chart2.jsp');
insert into cp_action values(4,'地图主页面数据来源',0,'map_value.do');
insert into cp_action values(5,'根据项目的图表展示数据来源',0,'map_chart1.do');
insert into cp_action values(6,'根据开工状态的图表展示数据来源',0,'map_chart2.do');
insert into cp_action values(7,'删除点标注',0,'map_control.do');

//用户角色表
create table cp_user_group(
user_id varchar2(32) references cp_user(id),
group_id varchar2(32) references cp_group(id),
primary key(user_id,group_id)
)

insert into cp_user_group values('1','1');
insert into cp_user_group values('1','2');
insert into cp_user_group values('2','2');
insert into cp_user_group values('2','3');
insert into cp_user_group values('3','3');


//角色权限表
create table cp_group_action(
group_id varchar2(32) references cp_group(id),
action_id varchar2(32) references cp_action(id),
state number(1) not null ,
check(state in (0,1)),
primary key(action_id,group_id)
)


insert into cp_group_action values('1','1',1);
insert into cp_group_action values('1','2',1);
insert into cp_group_action values('1','3',1);
insert into cp_group_action values('1','4',1);
insert into cp_group_action values('1','5',1);
insert into cp_group_action values('1','6',1);
insert into cp_group_action values('1','7',1);

insert into cp_group_action values('2','1',1);
insert into cp_group_action values('2','2',1);
insert into cp_group_action values('2','3',1);
insert into cp_group_action values('2','4',1);
insert into cp_group_action values('2','5',1);
insert into cp_group_action values('2','6',1);

insert into cp_group_action values('3','1',1);
insert into cp_group_action values('3','2',1);
insert into cp_group_action values('3','4',1);
insert into cp_group_action values('3','5',1);


pojo类

用户:

package pojo;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="cp_user")
public class User {
	@Id
	@GeneratedValue(generator = "system-uuid")    
	@GenericGenerator(name = "system-uuid", strategy = "uuid.hex") 
	private String id;
	private String name;
	private String password;
	@ManyToMany(cascade=CascadeType.ALL ,fetch = FetchType.EAGER)
	@JoinTable(name="cp_user_group",
			joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
			inverseJoinColumns={@JoinColumn(name="group_id",referencedColumnName="id")}
	)
	private Set<Group> groups = new HashSet<Group>(); 
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public Set<Group> getGroups() {
		return groups;
	}
	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}
	
	
}

角色:

package pojo;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="cp_group")
public class Group {
	@Id
	@GeneratedValue(generator = "system-uuid")    
	@GenericGenerator(name = "system-uuid", strategy = "uuid.hex")
	private String id;
	private String name;
	@ManyToMany(cascade=CascadeType.ALL ,fetch = FetchType.EAGER,mappedBy="groups")
	private Set<User> users = new HashSet<User>();
	@ManyToMany(cascade=CascadeType.ALL ,fetch = FetchType.EAGER,mappedBy="groups")
	private Set<Action> actions = new HashSet<Action>();
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	public Set<Action> getActions() {
		return actions;
	}
	public void setActions(Set<Action> actions) {
		this.actions = actions;
	}
	
	
}

权限:

package pojo;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="cp_action")
public class Action {
	@Id
	@GeneratedValue(generator = "system-uuid")    
	@GenericGenerator(name = "system-uuid", strategy = "uuid.hex")
	private String id;
	private String name;
	private String p_id;
	private String url;
	@ManyToMany(cascade=CascadeType.ALL ,fetch = FetchType.EAGER)
	@JoinTable(name="cp_group_action",
			joinColumns={@JoinColumn(name="action_id", referencedColumnName="id")},
			inverseJoinColumns={@JoinColumn(name="group_id",referencedColumnName="id")}
	)
	private Set<Group> groups = new HashSet<Group>();
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getP_id() {
		return p_id;
	}
	public void setP_id(String p_id) {
		this.p_id = p_id;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public Set<Group> getGroups() {
		return groups;
	}
	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}
	
	
}

过滤器 filter

package util;

import java.io.IOException;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.Action;
import pojo.Group;
import pojo.User;

public class UserFilter extends HttpServlet implements Filter{

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest)request;      
		//System.out.println(req.getServletPath());      

		HttpServletResponse res = (HttpServletResponse)response;      
		HttpSession ses = req.getSession();      
		User user =(User) ses.getAttribute("user");   
		if(user ==null){      
			res.sendRedirect("../login.jsp");      
		}else{      
			Boolean allow= false ;
			Set<Group> groups = user.getGroups();
			for(Group g : groups){
				Set<Action> actions =  g.getActions();
				for(Action a : actions){
					if(a.getUrl().equals(req.getServletPath()))
						allow = true;
				}
			}
			if(allow){
				chain.doFilter(request, response);
			}else{
				res.sendRedirect("../error_quanxin.jsp");
			}
		} 
	}
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}


web.xml

<filter>
  <filter-name>userFilter</filter-name>
  <filter-class>util.UserFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>userFilter</filter-name>
  <url-pattern>/admin/*</url-pattern>
</filter-mapping>















分享到:
评论

相关推荐

    Hibernate Annotation jar

    这里面包涵了需要用Hibernate Annotation时,所需要的所有jar包! 现在我们公司在做web项目的时候,已经不用*.hbm.xml这种映射文件了,都是用Annotation(注解)方式来完成实体与表之间的映射关系,这样看起来比用...

    org.springframework.orm.hibernate3.LocalSessionFactoryBean

    ### 关于 "org.springframework.orm.hibernate3.LocalSessionFactoryBean" 未找到问题的知识点解析 #### 一、问题背景 在开发基于Spring与Hibernate整合的应用时,可能会遇到“`org.springframework.orm.hibernate...

    hibernate-Annotation.jar

    在Hibernate 3.x版本中,引入了Annotation注解,这是一种元数据的方式,可以替代XML配置文件来描述对象与数据库表之间的映射关系。 **Hibernate Annotation注解** 在Hibernate 3.x之前,对象到数据库的映射通常...

    hibernate annotation hibernate3

    这些注解分别表示一对多、多对一、一对一和多对多的关系。例如,表示用户与角色的一对多关系: ```java @ManyToOne @JoinColumn(name="ROLE_ID") private Role role; ``` 四、继承策略注解 1. `@Inheritance`...

    Hibernate-Annotation中文教程.pdf

    借助新的 Hibernate Annotation 库,即可一次性地分配所有旧映射文件,一切都会按照您的想法来定义——注释直接嵌入到您的 Java 类中,并提供一种强大及灵活的方法来声明持久性映射。 Hibernate Annotation 的优点...

    javax.annotation.jar

    javax.annotation.jar

    hibernate annotations3.4.0 GA.rar

    现在已经不用*.hbm.xml这种映射文件了,都是用Annotation(注解)方式来完成实体与表之间的映射关系,这样看起来比用xml文件来映射更具有可读性,自我感觉以后Hibernate Annotation的映射方式将代替hibernate 的*....

    hibernate annotation 中文文档

    - `@OneToOne`, `@OneToMany`, `@ManyToOne`, `@ManyToMany`: 用于处理各种关联关系,如一对一、一对多、多对一、多对多。 3. **继承与多态** - `@Inheritance`: 定义继承策略,如SINGLE_TABLE(单表)、JOINED...

    jakarta.annotation-api-1.3.5-API文档-中文版.zip

    赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...

    javax.annotation-api-1.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.2.jar; 赠送原API文档:javax.annotation-api-1.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-1.2.pom;...

    annotation-1.1.0.jar

    @androidx.annotation.NonNull 缺失的兼容、androidx.annotation兼容包

    Hibernate-Annotation中文教程.docx

    借助新的 Hibernate Annotation 库,即可一次性地分配所有旧映射文件——一切都会按照您的想法来定义——注释直接嵌入到您的 Java 类中,并提供一种强大及灵活的方法来声明持久性映射。 使用 Hibernate 注解的优点...

    annotation的jar包

    javax.annotation-3.0.jar javax.annotation-3.0.jar javax.annotation-3.0.jar

    javax.annotation.jar文件

    spring 注解初始化,初始化开始@postconstruct 对象销毁@predestroy javax.annotation.jar文件

    Hibernate Annotation Reference .htm

    Hibernate Annotation Reference .htm

    hibernate 中文文档 and _annotation.chm

    3. 注解(Annotation)在Hibernate中的应用: - @Entity:标记一个Java类为数据库中的一个表,表示该类是实体类。 - @Table:指定实体类对应的数据库表名。 - @Id:标识类中的主键字段,可以配合@GeneratedValue...

    Hibernate Annotation 中文文档

    以上是对Hibernate Annotation的简要介绍,深入理解和熟练应用这些注解,将有助于提升你在Java持久化领域的专业技能。对于更详细的用法和高级特性,建议查阅《Hibernate Annotations参考文档-3.20》这份资料,以获取...

    javax.annotation-api-1.3.2 jar包.rar

    3. **javax.annotation-api-1.3.2版本** 这个版本的API引入了一些新的特性,可能包括对Java语言新特性的支持以及对旧有注解的改进。例如,可能增加了对Java 8或9新功能的支持,或者优化了注解处理器的性能。具体...

    完整的android.support.annotation

    完整的android.support.annotation.jar包含Nullable、Keep、XmlRes、TransitionRes、WorkerThread等共49个类

Global site tag (gtag.js) - Google Analytics