Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Security + JPA user schema
    primarykey
    data
    text
    <p>As per Spring documentation if you need to manage spring security via database you should have some standard schema of tables. for example.</p> <pre><code>create table users( username varchar(256) not null primary key, password varchar(256) not null, enabled boolean not null ); create table authorities ( username varchar(256) not null, authority varchar(256) not null, constraint fk_authorities_users foreign key(username) references users(username) ); create unique index ix_auth_username on authorities (username,authority); </code></pre> <p>The problem I am facing is following. 1) Not able to understand how could I achieve such schema of tables using JPA?</p> <p>I have tried something as follows.</p> <pre><code>@Entity @Table(name="USERS") public class UsersPersistence extends Users implements Serializable{ private static final long serialVersionUID = 1009548075747154488L; public UsersPersistence() { super(); } public UsersPersistence(long id, String userName, String password, boolean enabled) { super(id, userName,password,enabled); } @Id @GeneratedValue @Column(name="id") @Override public long getId() { return super.getId(); } @Column(name="username", nullable=false) @Override public String getUserName() { return super.getUserName(); } @Column(name="password", nullable=false) @Override public String getPassword() { return super.getPassword(); } @Column(name="enabled", nullable=false) @Override public boolean isEnabled() { return super.isEnabled(); } } </code></pre> <p>This table created as per requirement stated in Spring documentation schema. Problem in understanding is when i am trying to assign a foreign key on username in authorities table.Since JPA assign the foreign key's via the id of parent table (primary key Table) Or may be i do not know how to assign it.</p> <p>Following is the JPA class which create problem :-</p> <pre><code>@Entity @Table(name="AUTHORITIES") public class AuthoritiesPersistence extends Authorities implements Serializable{ private static final long serialVersionUID = 1L; public AuthoritiesPersistence() { super(); } public AuthoritiesPersistence(long id, UsersPersistence userName, String authority) { super(id,userName,authority); } @Id @GeneratedValue @Column(name="id") @Override public long getId() { return super.getId(); } @Override @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="username", nullable=false) public UsersPersistence getUserName() { return (UsersPersistence) super.getUserName(); } @Column(name="authority", nullable=false) @Override public String getAuthority() { return super.getAuthority(); } } </code></pre> <p>This table is created successfully but Spring security authentication is not able to recognize the username because JPA uses the foreign key id than the actual user name.</p> <p>Any help would be appreciable. I am really stuck in creating a foreign key which will be based on the username rather than the id. Thanks </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload