日期:2014-05-16 浏览次数:20552 次
create table sec_user( username varchar(100) primary key, password varchar(255) );
insert into sec_user values('admin','admin'); insert into sec_user values('test','test');
create table sec_role( rolename varchar(100) primary key, prompt varchar(255) );
insert into sec_role values('ROLE_USER','common user privilege'); insert into sec_role values('ROLE_ADMIN','administrator privilege');
create table sec_role_user( username varchar(100) not null, rolename varchar(100) not null, constraint ru_id primary key(username,rolename) );
alter table sec_role_user add constraint fk_user foreign key(username) references sec_user(username); alter table sec_role_user add constraint fk_role foreign key(rolename) references sec_role(rolename);
insert into sec_role_user values('test','ROLE_USER'); insert into sec_role_user values('admin','ROLE_USER'); insert into sec_role_user values('admin','ROLE_ADMIN');
@Entity @Table(name="SEC_USER") public class User implements Serializable,UserDetails { /** * */ private static final long serialVersionUID = 4654282337754230886L; @Id @Column(name="USERNAME",unique=true,nullable=false,length=100) private String username; @Column(name="PASSWORD",length=255) private String password; @ManyToMany(targetEntity=Role.class,fetch=FetchType.EAGER) @JoinTable(name="SEC_ROLE_USER",joinColumns={@JoinColumn(name="USERNAME",referencedColumnName="USERNAME")}, inverseJoinColumns={@JoinColumn(name="ROLENAME",referencedColumnName="ROLENAME")}) private Collection<Role> roles; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Collection<Role> getRoles() { return roles; } public void setRoles(Collection<Role> roles) { this.roles = roles; } public Collection<GrantedAuthority> getAuthorities() { List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); Iterator<Role> iter = this.roles.iterator(); while(iter.hasNext()){ Role role = iter.next(); GrantedAuthority authority = new GrantedAuthorityImpl(role.getRolename()); authorities.add(authority); } /*String[] roles = this.role.split(","); for(String r : roles){ if(r != null && !"".equals(r)){ GrantedAuthority authority = new GrantedAuthorityImpl(r); authorities.add(authority); } }*/ return authorities; } public boolean isAccountNonExpired() { return true; } public boolean isAccountNonLocked() { return true; } public boolean isCredentialsNonExpired() { return true; } public boolean isEnabled() { return true; } }
@Entity @Table(name="SEC_ROLE") public class Role implements Serializable { /** * */ private static final long serialVersionUID = 8216539800374390907L; @Id @Column(name="ROLENAME",unique=true,nullable=false,length=100) private String rolename; @Column(name="PROMPT",length=255) private String prompt; @ManyToMany(targetEntity=User.class,mappedBy="roles") private Collection<User> users; public String getRolename() { return rolename; } publ