JDBC Authentication -- Spring Security()

  本篇文章为你整理了JDBC Authentication :: Spring Security()的详细内容,包含有 JDBC Authentication :: Spring Security,希望能帮助你了解 JDBC Authentication :: Spring Security。

   JDBC AuthenticationSpring Security s JdbcDaoImpl implements UserDetailsService to provide support for username-and-password-based authentication that is retrieved by using JDBC.

  JdbcUserDetailsManager extends JdbcDaoImpl to provide management of UserDetails through the UserDetailsManager interface.

  UserDetails-based authentication is used by Spring Security when it is configured to accept a username/password for authentication.

  
The Default Schema used by Spring Security JDBC Authentication

  
Spring Security provides default queries for JDBC-based authentication.

  This section provides the corresponding default schemas used with the default queries.

  You need to adjust the schema to match any customizations to the queries and the database dialect you use.

  
JdbcDaoImpl requires tables to load the password, account status (enabled or disabled) and a list of authorities (roles) for the user.

  
The default schema is also exposed as a classpath resource named org/springframework/security/core/userdetails/jdbc/users.ddl.

  
username varchar_ignorecase(50) not null primary key,

   password varchar_ignorecase(500) not null,

   enabled boolean not null

  create table authorities (

   username varchar_ignorecase(50) not null,

   authority varchar_ignorecase(50) not null,

   constraint fk_authorities_users foreign key(username) references users(username)

  create unique index ix_auth_username on authorities (username,authority);

 

 

  
PASSWORD NVARCHAR2(128) NOT NULL,

   ENABLED CHAR(1) CHECK (ENABLED IN (Y,N) ) NOT NULL

  
ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_UNIQUE UNIQUE (USERNAME, AUTHORITY);

  ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_FK1 FOREIGN KEY (USERNAME) REFERENCES USERS (USERNAME) ENABLE;

 

 

  


create table groups (

 

   id bigint generated by default as identity(start with 0) primary key,

   group_name varchar_ignorecase(50) not null

  create table group_authorities (

   group_id bigint not null,

   authority varchar(50) not null,

   constraint fk_group_authorities_group foreign key(group_id) references groups(id)

  create table group_members (

   id bigint generated by default as identity(start with 0) primary key,

   username varchar(50) not null,

   group_id bigint not null,

   constraint fk_group_members_group foreign key(group_id) references groups(id)

  );

 

  


 jdbc:embedded-database 

 

   jdbc:script location="classpath:org/springframework/security/core/userdetails/jdbc/users.ddl"/

   /jdbc:embedded-database

 

  
In a production environment, you want to ensure that you set up a connection to an external database.

  
In this sample, we use Spring Boot CLI to encode a password value of password and get the encoded password of {bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW.

  See the PasswordEncoder section for more details about how to store passwords.

  
.username("user")

   .password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")

   .roles("USER")

   .build();

   UserDetails admin = User.builder()

   .username("admin")

   .password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")

   .roles("USER", "ADMIN")

   .build();

   JdbcUserDetailsManager users = new JdbcUserDetailsManager(dataSource);

   users.createUser(user);

   users.createUser(admin);

   return users;

  }

 

 

  
user name="user"

   password="{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW"

   authorities="ROLE_USER" /

   user name="admin"

   password="{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW"

   authorities="ROLE_USER,ROLE_ADMIN" /

   /jdbc-user-service

 

 

  
.username("user")

   .password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")

   .roles("USER")

   .build();

   val admin = User.builder()

   .username("admin")

   .password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")

   .roles("USER", "ADMIN")

   .build();

   val users = JdbcUserDetailsManager(dataSource)

   users.createUser(user)

   users.createUser(admin)

   return users

  }

 

 

  以上就是JDBC Authentication :: Spring Security()的详细内容,想要了解更多 JDBC Authentication :: Spring Security的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: