Skip to content

Instantly share code, notes, and snippets.

@bytestree bytestree/Roles.java
Created Apr 23, 2016

Embed
What would you like to do?
Model classes and database script for spring authentication
CREATE TABLE users
(
username character varying(100) NOT NULL,
password character varying(100) NOT NULL,
enabled boolean DEFAULT true,
locked boolean DEFAULT false,
failed_logins integer DEFAULT 0,
last_login_date timestamp without time zone,
CONSTRAINT "PK_USER" PRIMARY KEY (username)
);
CREATE TABLE roles
(
id integer NOT NULL,
name character varying(50) NOT NULL,
CONSTRAINT "PK_ROLES" PRIMARY KEY (id)
);
CREATE TABLE authorities
(
"user" character varying(50) NOT NULL,
role integer NOT NULL,
CONSTRAINT "PK_AUTHORITIES" PRIMARY KEY ("user", role),
CONSTRAINT "FK_ROLE" FOREIGN KEY (role)
REFERENCES roles (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT "FK_USER" FOREIGN KEY ("user")
REFERENCES users (username) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
INSERT INTO roles VALUES (1, 'ROLE_USER');
INSERT INTO roles VALUES (2, 'ROLE_ADMIN');
INSERT INTO users VALUES ('bytestree', '12345', true, false, 0);
INSERT INTO users VALUES ('admin', '12345', true, false, 0);
INSERT INTO authorities VALUES ('bytestree', 1);
INSERT INTO authorities VALUES ('admin', 1);
INSERT INTO authorities VALUES ('admin', 2);
package com.bytestree.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "roles")
public class Roles implements Serializable {
private static final long serialVersionUID = 8215940655966357715L;
@Id
@Column(name = "id", unique = true, nullable = false)
private int id;
@Column(name = "name", nullable = false, length = 50)
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "authorities", joinColumns = {
@JoinColumn(name = "role", nullable = false, updatable = false) }, inverseJoinColumns = {
@JoinColumn(name = "user", nullable = false, updatable = false) })
private Set<Users> users = new HashSet<Users>(0);
public Roles() {
}
public Roles(int id, String name) {
this.id = id;
this.name = name;
}
public Roles(int id, String name, Set<Users> users) {
this.id = id;
this.name = name;
this.users = users;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<Users> getUsers() {
return this.users;
}
public void setUsers(Set<Users> users) {
this.users = users;
}
}
package com.bytestree.model;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class Users implements Serializable {
private static final long serialVersionUID = 1948638898199176136L;
@Id
@Column(name = "username", unique = true, nullable = false, length = 100)
private String username;
@Column(name = "password", nullable = false, length = 100)
private String password;
@Column(name = "failed_logins")
private Integer failedLogins;
@Column(name = "enabled")
private Boolean enabled;
@Column(name = "locked")
private Boolean locked;
@Column(name = "last_login_date", length = 23)
private Date lastLoginDate;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "authorities", joinColumns = {
@JoinColumn(name = "user", nullable = false, updatable = false) }, inverseJoinColumns = {
@JoinColumn(name = "role", nullable = false, updatable = false) })
private Set<Roles> roleses = new HashSet<Roles>(0);
public Users() {
}
public Users(String username, String password) {
this.username = username;
this.password = password;
}
public Users(String username, String password, Integer failedLogins,
Boolean enabled, Boolean locked, Date lastLoginDate,
Set<Roles> roleses) {
this.username = username;
this.password = password;
this.failedLogins = failedLogins;
this.enabled = enabled;
this.locked = locked;
this.lastLoginDate = lastLoginDate;
this.roleses = roleses;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getFailedLogins() {
return this.failedLogins;
}
public void setFailedLogins(Integer failedLogins) {
this.failedLogins = failedLogins;
}
public Boolean getEnabled() {
return this.enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Boolean getLocked() {
return this.locked;
}
public void setLocked(Boolean locked) {
this.locked = locked;
}
public Date getLastLoginDate() {
return this.lastLoginDate;
}
public void setLastLoginDate(Date lastLoginDate) {
this.lastLoginDate = lastLoginDate;
}
public Set<Roles> getRoleses() {
return this.roleses;
}
public void setRoleses(Set<Roles> roleses) {
this.roleses = roleses;
}
}
@bytestree

This comment has been minimized.

Copy link
Owner Author

bytestree commented May 6, 2016

Refer Spring Security 4 with Hibernate for complete example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.