跳到主要内容
版本:7.0.2

JDBC 认证

DeepSeek V3 中英对照 JDBC JDBC Authentication

Spring Security 的 JdbcDaoImpl 实现了 UserDetailsService,以提供对基于用户名和密码的身份验证的支持,该验证通过使用 JDBC 检索。JdbcUserDetailsManager 扩展了 JdbcDaoImpl,通过 UserDetailsManager 接口提供对 UserDetails 的管理。当 Spring Security 配置为接受用户名/密码进行身份验证时,会使用基于 UserDetails 的身份验证。

在接下来的章节中,我们将讨论:

默认模式

Spring Security 为基于 JDBC 的身份验证提供了默认查询。本节提供了与这些默认查询相对应的默认模式。您需要根据查询的自定义设置以及所使用的数据库方言来调整此模式。

用户模式

JdbcDaoImpl 需要依赖数据库表来加载用户的密码、账户状态(启用或禁用)以及权限(角色)列表。

备注

默认的 schema 也以类路径资源的形式提供,其名称为 org/springframework/security/core/userdetails/jdbc/users.ddl

create table users(
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);

Oracle 是一款广受欢迎的数据库选择,但其所需的模式略有不同:

CREATE TABLE USERS (
USERNAME NVARCHAR2(128) PRIMARY KEY,
PASSWORD NVARCHAR2(128) NOT NULL,
ENABLED CHAR(1) CHECK (ENABLED IN ('Y','N') ) NOT NULL
);

CREATE TABLE AUTHORITIES (
USERNAME NVARCHAR2(128) NOT NULL,
AUTHORITY NVARCHAR2(128) 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)
);

设置数据源

在配置 JdbcUserDetailsManager 之前,我们必须先创建一个 DataSource。在我们的示例中,我们设置了一个嵌入式 DataSource,该数据源使用默认用户模式进行初始化。

@Bean
DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(H2)
.addScript(JdbcDaoImpl.DEFAULT_USER_SCHEMA_DDL_LOCATION)
.build();
}

在生产环境中,您需要确保已建立与外部数据库的连接。

JdbcUserDetailsManager Bean

在本示例中,我们使用 Spring Boot CLI 对密码值 password 进行编码,得到编码后的密码 {bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW。有关如何存储密码的更多详细信息,请参阅 PasswordEncoder 部分。

@Bean
UserDetailsManager users(DataSource dataSource) {
UserDetails user = User.builder()
.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;
}