java - How to configure spring-boot with security for custom SQL schema user-role? -


i try configure spring-boot-security integrated sql schema:

create table if not exists users (   id       serial primary key,   username varchar(20) unique not null,   password varchar(20) unique not null,   role     integer            not null,   foreign key (role) references user_role (id) );  create table if not exists user_role (   id   serial primary key,   role varchar(10) ); 

i find many examples of integration database spring-boot-security , examples this:

@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=?"); } 

i don't understand how work methods usersbyusernamequery , authoritiesbyusernamequery. why select username? password? password not need authentication?

please, explain me how work. how connect db security configauthentication()?

according approach should have make changes in order run querys in usersbyusernamequery , authoritiesbyusernamequery.

for example: usersbyusernamequery according table definition should select username,password,true users username=? because don't have enable filed on users table.

and authoritiesbyusernamequery should select u.username, r.role users u inner join user_roles r on (r.id=u.role) u.username=? because 1 table has user information , other role reason why should use join in query.

spring authentication let configure many ways implement authetication method, in case put on question jdbc authentication approach.

when use jdbc authentication approach necessary check following:

  1. make sure pom.xml has jdbc dependency , database dependency, example h2 database

      <dependency>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-starter-jdbc</artifactid>         </dependency>   <dependency>         <groupid>com.h2database</groupid>         <artifactid>h2</artifactid>     </dependency> 
  2. define datasource bean in order let spring boot knows how connect database

datasource bean

@bean(name = "datasource")  public drivermanagerdatasource datasource() {      drivermanagerdatasource drivermanagerdatasource = new drivermanagerdatasource();      drivermanagerdatasource.setdriverclassname("org.h2.driver");      drivermanagerdatasource.seturl("jdbc:h2:~/test2");      return drivermanagerdatasource;   } 
  1. then need create class extends websecurityconfigureradapter in order inject datasource bean authenticationmanager builder, here spring authentication bind database in order execute querys retrieve user , role data create on database.

for example:

public class mysecurityconfiguration extends websecurityconfigureradapter {   @autowired  datasource datasource;   @autowired  public void configauthentication(authenticationmanagerbuilder auth) throws exception { 

and auth.jdbcauthentication().datasource(datasource) set datasource in order query user , roles.

front end connection /login servlet authentication

first create view consume /login servlet

  <!doctype html>     <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"           xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">     <head>         <title>spring security example </title>     </head>     <body>     <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"/> </label></div>         <div><label> password: <input type="password" name="password"/> </label></div>         <div><input type="submit" value="sign in"/></div>     </form>     </body>     </html> 

register login view

@configuration     public class mvcconfig extends webmvcconfigureradapter {         @override         public void addviewcontrollers(viewcontrollerregistry registry) {             registry.addviewcontroller("/login").setviewname("login");         }     } 

hope help.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -