java - Spring boot with Spring Security and custom database -


sorry in advance bad english..

since have changed database configuration don't succeed log me on application. using spring security. before making changes worked.

i have 2 entities :

  • user.java
  • userrole.java

user.java

package betizy.models;  //imports  @entity @table(name = "use_user") public class user {   @id @generatedvalue(strategy = generationtype.auto) @column(name="use_id") private long id;  @notnull @column(name = "use_username") private string username;  @notnull @column(name = "use_password") private string password;  @notnull @column(name = "use_email") private string email;   //getters , setters } 



userrole.java

package betizy.models;  //imports  @entity @table(name = "usr_user_role") public class userrole {  @id @generatedvalue(strategy = generationtype.auto) @column(name="usr_id") private long id;  @manytoone @joincolumn(name = "usr_use_id") private user user;  @notnull @column(name = "usr_role") private string role;  //getters , setters } 



login.html

 <!doctype html>  <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">  <head>      <title>spring security example </title>      <script src="/webjars/angularjs/1.4.9/angular.js"></script>      <script src="webjars/jquery/2.0.3/jquery.min.js"></script>       <link rel="stylesheet" href="/webjars/bootstrap/3.3.6/css/bootstrap.css">       <script src="/js/index.js"></script>  </head>  <body ng-app="betizy">       <div header></div>       <div th:if="${param.error}">            invalid username , password.       </div>       <div th:if="${param.logout}">            have been logged out.       </div>       <form th:action="@{/login}" method="post">             <div><label> user name : <input type="text" name="username" required/> </label></div>             <div><label> password: <input type="password" name="password" required/> </label></div>             <div><input type="submit" value="sign in"/></div>       </form>       <div>to open new account click <a href="/register">here</a>. </div>        <div footer></div>  </body>  </html> 



securityconfig.java

package betizy.security;  //imports  @configuration @enablewebsecurity public class securityconfig extends websecurityconfigureradapter {   @autowired datasource datasource;  @autowired public void configauthentication(authenticationmanagerbuilder auth) throws exception {      auth.jdbcauthentication().datasource(datasource)             //.passwordencoder(passwordencoder())             .usersbyusernamequery(                     "select * use_user use_user.use_username=?")             .authoritiesbyusernamequery(                     "select * usr_user_role inner join use_user on use_user.use_id = usr_user_role.usr_use_id use_user.use_username=?"); }  @bean public passwordencoder passwordencoder(){     passwordencoder encoder = new bcryptpasswordencoder();     return encoder; }   @override protected void configure(httpsecurity http) throws exception {     http.authorizerequests()             //.antmatchers("/hello").access("hasrole('role_admin')")             .antmatchers("/", "/register", "/user/create", "/webjars/**", "/js/**").permitall()             .anyrequest().authenticated()             .and()             .formlogin().loginpage("/login").permitall()             .usernameparameter("username").passwordparameter("password")             .and()             .logout().permitall()             .and()             .exceptionhandling().accessdeniedpage("/403")             .and()             .csrf().disable(); } } 



think problem name of user entity fields or 2 queries in securityconfig.java, have idea how can solve problem.
must keep database configuration (name of fields).



thank in advance ! :)




edit

with 2 changes works not database. post differences between 2 databases , differences in securityconfig.java

first base (it works)
user table
user role table

with securityconfig.java

@autowired public void configauthentication(authenticationmanagerbuilder auth) throws exception {      auth.jdbcauthentication().datasource(datasource)             .usersbyusernamequery(                     "select username,password, enabled users username=?")             .authoritiesbyusernamequery(                     "select username, role user_roles username=?"); } 



second doesn't work. can't post links have perfect description above in user.java , userrole.java

with securityconfig.java

@autowired public void configauthentication(authenticationmanagerbuilder auth) throws exception {      auth.jdbcauthentication().datasource(datasource)             .usersbyusernamequery(                     "select use_username, use_password, use_email use_user use_username=?")             .authoritiesbyusernamequery(                     "select use_username, usr_role usr_user_role, use_user use_id = usr_use_id , use_username=?"); } 

please change sql return column names in order instead of *;

<!-- change own column name, return columns in order--> select <username,password,enabled> use_user use_user.use_username=? 

and authorities sql

<!-- change own column name, authority must second column  --> select <username,authority> usr_user_role inner join use_user on use_user.use_id = usr_user_role.usr_use_id use_user.use_username=? 

more detail see here, ok, here code, think it's more easy understand why should return columns in order.

protected list<userdetails> loadusersbyusername(string username) {         return getjdbctemplate().query(this.usersbyusernamequery,                 new string[] { username }, new rowmapper<userdetails>() {                     @override                     public userdetails maprow(resultset rs, int rownum)                             throws sqlexception {                         // user info                         string username = rs.getstring(1);                         string password = rs.getstring(2);                         boolean enabled = rs.getboolean(3);                         return new user(username, password, enabled, true, true, true,                                 authorityutils.no_authorities);                     }                  });     }  protected list<grantedauthority> loaduserauthorities(string username) {         return getjdbctemplate().query(this.authoritiesbyusernamequery,                 new string[] { username }, new rowmapper<grantedauthority>() {                     @override                     public grantedauthority maprow(resultset rs, int rownum)                             throws sqlexception {                         //get grantedauthority                         string rolename = jdbcdaoimpl.this.roleprefix + rs.getstring(2);                          return new simplegrantedauthority(rolename);                     }                 });     } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -