一、創建項目並導入相關依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.27</version>
</dependency>
注:maven默認不編譯src/main/java下的xml文件,需要我們手動導入

二、相關配置和代碼
1)application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/security
spring.datasource.username=root
spring.datasource.password=123
2)數據庫腳本
| /* |
|
|
|
Navicat MySQL Data Transfer |
|
|
Source Server : localhost |
|
|
Source Server Version : 50717 |
|
|
Source Host : localhost:3306 |
|
|
Source Database : security |
|
|
Target Server Type : MYSQL |
|
|
Target Server Version : 50717 |
|
|
File Encoding : 65001 |
|
|
Date: 2018-07-28 15:26:51 |
|
|
*/ |
|
|
SET FOREIGN_KEY_CHECKS=0; |
|
|
-- ---------------------------- |
|
|
-- Table structure for role |
|
|
-- ---------------------------- |
|
|
DROP TABLE IF EXISTS `role`; |
|
|
CREATE TABLE `role` ( |
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
|
|
`name` varchar(32) DEFAULT NULL, |
|
|
`nameZh` varchar(32) DEFAULT NULL, |
|
|
PRIMARY KEY (`id`) |
|
|
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; |
|
|
-- ---------------------------- |
|
|
-- Records of role |
|
|
-- ---------------------------- |
|
|
INSERT INTO `role` VALUES ('1', 'ROLE_dba', '數據庫管理員'); |
|
|
INSERT INTO `role` VALUES ('2', 'ROLE_admin', '系統管理員'); |
|
|
INSERT INTO `role` VALUES ('3', 'ROLE_user', '用戶'); |
|
|
-- ---------------------------- |
|
|
-- Table structure for user |
|
|
-- ---------------------------- |
|
|
DROP TABLE IF EXISTS `user`; |
|
|
CREATE TABLE `user` ( |
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
|
|
`username` varchar(32) DEFAULT NULL, |
|
|
`password` varchar(255) DEFAULT NULL, |
|
|
`enabled` tinyint(1) DEFAULT NULL, |
|
|
`locked` tinyint(1) DEFAULT NULL, |
|
|
PRIMARY KEY (`id`) |
|
|
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; |
|
|
-- ---------------------------- |
|
|
-- Records of user |
|
|
-- ---------------------------- |
|
|
INSERT INTO `user` VALUES ('1', 'root', '$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq', '1', '0'); |
|
|
INSERT INTO `user` VALUES ('2', 'admin', '$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq', '1', '0'); |
|
|
INSERT INTO `user` VALUES ('3', 'sang', '$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq', '1', '0'); |
|
|
-- ---------------------------- |
|
|
-- Table structure for user_role |
|
|
-- ---------------------------- |
|
|
DROP TABLE IF EXISTS `user_role`; |
|
|
CREATE TABLE `user_role` ( |
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
|
|
`uid` int(11) DEFAULT NULL, |
|
|
`rid` int(11) DEFAULT NULL, |
|
|
PRIMARY KEY (`id`) |
|
|
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; |
|
|
-- ---------------------------- |
|
|
-- Records of user_role |
|
|
-- ---------------------------- |
|
|
INSERT INTO `user_role` VALUES ('1', '1', '1'); |
|
|
INSERT INTO `user_role` VALUES ('2', '1', '2'); |
|
|
INSERT INTO `user_role` VALUES ('3', '2', '2'); |
|
|
INSERT INTO `user_role` VALUES ('4', '3', '3'); |
|
|
SET FOREIGN_KEY_CHECKS=1; |
3)數據庫結構




密碼是123 springsecurity加密算法后的密文,可以讓同樣的密碼每次生成的密文都不一樣,從而不會重復,我們案列中的密文是我偷懶復制粘貼的所以一樣

也就是該類完成加密的
4)創建實體類User,Role
4.1)創建User實現UserDetails

這里要實現UserDetails接口,這個接口好比一個規范。防止開發者定義的密碼變量名各不相同,從而導致springSecurity不知道哪個方法是你的密碼
我給的數據庫中user表沒有UserDetails的幾個方法,可以直接手動給它true

有的就是用數據庫查出來的屬性

4.2)創建Role類

5)創建UserMapper和UserMapper.xml
5.1)UserMapper.class
注:以前在SSM框架之所以不用加類似@Service和@controller的注解時因為我們在xml文件已經配置了,如今使用SpringBoot所以我們也要改變寫法
一共兩種寫法
5.1.1)在類上直接加@Mapper

5.1.2)在SpringBoot啟動類上配置全局的掃描

5.2)UserMapper.xml

6)創建UserService類
6.1)UserService同樣也要繼承UserServiceDetails接口

7)創建SercurityConfig配置類

7.1)SercurityConfig需要繼承WebSecurityConfigurerAdapter類,並在類上加@configuration

7.2)SpringSecurity5.0之后密碼必須加密

7.3)把數據庫查出的用戶信息交給SpringSecurity處理

這樣就算完成了一半了,為什么說完成一半因為這些並不是全部代碼下半部分我下篇博客寫,這樣比較容易接受,所以url的權限還是寫死的
下篇博客https://www.cnblogs.com/fernfei/p/12194847.html
7.4)配置httpSercurity
下圖的代碼可以參考我另一個博客介紹里面有這段全部代碼,並有解釋
https://www.cnblogs.com/fernfei/p/12185186.html

這里解釋一下為什么數據庫中的user表需要加ROLE_這樣前綴,可以看一下源碼

①處告訴我們如果在.antMatchers("/dba/**").hasRole("dba")的時候加前綴會拋異常
②處告訴我們為什么在.antMatchers("/dba/**").hasRole("dba")的時候不需要加ROLE_前綴
8)角色繼承
8.1)在Spring Boot2.0.8(含)版本的時候是以下寫法
用空格把ROLE_dba>ROLE_admin 和ROLE_admin>ROLE_user分開
@Bean
RoleHierarchyroleHierarchy(){
RoleHierarchyImplroleHierarchy=newRoleHierarchyImpl();
Stringhierarchy="ROLE_dba>ROLE_adminROLE_admin>ROLE_user";
roleHierarchy.setHierarchy(hierarchy);
returnroleHierarchy;
}
8.2)Spring Boot2.0.8(不含)之后得版本用 \n 來區分
@Bean
RoleHierarchyroleHierarchy(){
RoleHierarchyImplroleHierarchy=newRoleHierarchyImpl();
Stringhierarchy="ROLE_dba>ROLE_admin\nROLE_admin>ROLE_user";
roleHierarchy.setHierarchy(hierarchy);
returnroleHierarchy;
}
9)方法安全
9.1)在SecurityConfig配置上類加上
@EnableGlobalMethodSecurity(prePostEnabled=true,securedEnabled=true)

9.2)在Service層創建一個MethodService,當然並一定非要叫這個類名 可以根據你的具體業務考慮加在哪里

10)controller層

