SpringBoot整合SpringSecurity

一、添加依赖

SpringBoot整合SpringSecurity

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>2.4.4</version>
</dependency>

 

二、创建数据库表

DROP TABLE IF EXISTS `vhr`.`user`;
CREATE TABLE  `vhr`.`user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(45) NOT NULL DEFAULT '' COMMENT '登录用户名',
  `password` varchar(255) NOT NULL DEFAULT '' COMMENT '登录密码',
  `enabled` tinyint(1) NOT NULL DEFAULT '0',
  `locked` tinyint(1) NOT NULL DEFAULT '0',
  `name` varchar(32) DEFAULT NULL COMMENT '姓名',
  `userface` varchar(255) DEFAULT NULL COMMENT '用户头像',
  `phone` varchar(11) DEFAULT NULL COMMENT '手机号码',
  `telephone` varchar(16) DEFAULT NULL COMMENT '固定电话',
  `address` varchar(255) DEFAULT NULL COMMENT '联系地址',
  `email` varchar(255) DEFAULT NULL COMMENT '电子邮箱',
  `remark` varchar(255) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `vhr`.`role`;
CREATE TABLE  `vhr`.`role` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `nameEn` varchar(45) NOT NULL DEFAULT '',
  `nameCn` varchar(45) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `vhr`.`user_role`;
CREATE TABLE  `vhr`.`user_role` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(10) unsigned NOT NULL DEFAULT '0',
  `rid` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `FK_user_role_1` (`uid`),
  KEY `FK_user_role_2` (`rid`),
  CONSTRAINT `FK_user_role_1` FOREIGN KEY (`uid`) REFERENCES `user` (`id`),
  CONSTRAINT `FK_user_role_2` FOREIGN KEY (`rid`) REFERENCES `role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `vhr`.`menu`;
CREATE TABLE  `vhr`.`menu` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `url` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '请求路径规则',
  `path` varchar(64) DEFAULT NULL COMMENT '路由path',
  `component` varchar(64) DEFAULT NULL COMMENT '组件英文名称',
  `name` varchar(64) DEFAULT NULL COMMENT '组件中文名称',
  `iconCls` varchar(64) DEFAULT NULL COMMENT '菜单图标',
  `keepAlive` tinyint(1) NOT NULL DEFAULT '0' COMMENT '菜单切换时是否保活',
  `requireAuth` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否登录后才能访问',
  `pid` int(10) unsigned DEFAULT NULL COMMENT '父菜单Id',
  `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否可用',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `vhr`.`menu_role`;
CREATE TABLE  `vhr`.`menu_role` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `mid` int(10) unsigned NOT NULL DEFAULT '0',
  `rid` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `FK_menu_role_1` (`mid`),
  KEY `FK_menu_role_2` (`rid`),
  CONSTRAINT `FK_menu_role_1` FOREIGN KEY (`mid`) REFERENCES `menu` (`id`),
  CONSTRAINT `FK_menu_role_2` FOREIGN KEY (`rid`) REFERENCES `role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 SpringBoot整合SpringSecurity

 三、启动项目测试

随便添加一个简单接口,网页url访问测试,我这里用的是之前的接口

SpringBoot整合SpringSecurity

SpringBoot整合SpringSecurity

 

 

默认用户名是user,默认密码在每次启动项目时随机生成  

Using generated security password: 0a98ecc8-779b-4dd7-86c6-1b39f6b77437

 

上一篇:性能优化之数据库优化


下一篇:MySQL - Failed to open the referenced table XXX