SpringSecurity(一):初步搭建集成SpringBoot

163 阅读2分钟

在当前的开发中,有许多权限控制框架,今天来介绍一款spring家族的亲儿子springsecurity,这是一款非常优秀的权限控制框架

整合springboot

    1.创建maven工程(聚合服务)
    2.创建子模块(springboot-security)
    3.添加依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    添加上这个依赖以后,也就意味着已经接入到springboot中了
  • 当我们访问localhost:8888的时候就会跳转到security默认的登录页面 image.png
    默认用户名:user 密码:Using generated security password: 417f4d19-b6e9-4cfa-b0ca-731a193cc8db(会在
    服务启动的时候自动生成一个随机的密码在控制台)

上面的步骤只是初步整合security,接下来需要更加细粒度的进行整合

  • 准备数据库脚本文件
create table t_user
(
	id bigint not null comment '用户表主键
',
	username varchar(20) null comment '用户名称',
	password varchar(100) null comment '用户密码',
	state tinyint(1) null comment '用户状态(1:启动,0:禁用)'
)
comment '用户表';

create unique index t_user_id_uindex
	on t_user (id);

alter table t_user
	add constraint t_user_pk
		primary key (id);


create table t_role
(
	id bigint not null comment '角色主键',
	role_name varchar(20) null comment '角色名称'
)
comment '角色表';

create unique index t_role_id_uindex
	on t_role (id);

alter table t_role
	add constraint t_role_pk
		primary key (id);

create table t_permission
(
	id bigint not null comment '权限id',
	permission_name varchar(20) null comment '权限名称',
	permission_url varchar(30) null comment '权限资源'
)
comment '权限表';

create unique index t_permission_id_uindex
	on t_permission (id);

alter table t_permission
	add constraint t_permission_pk
		primary key (id);

create table t_user_role
(
	id bigint not null comment '用户角色中间表主键',
	user_id bigint not null comment '用户主键',
	role_id bigint not null comment '角色主键'
)
comment '用户角色中间表';

create unique index t_user_role_id_uindex
	on t_user_role (id);

alter table t_user_role
	add constraint t_user_role_pk
		primary key (id);

create table t_role_permission
(
	id bigint not null comment '角色权限中间表主键',
	role_id bigint not null comment '角色主键',
	permission_id bigint not null comment '权限主键'
)
comment '角色权限中间表';

create unique index t_role_permission_id_uindex
	on t_role_permission (id);

alter table t_role_permission
	add constraint t_role_permission_pk
		primary key (id);