shiro 知识:https://www.cnblogs.com/rmxd/p/11750786.html
https://www.cnblogs.com/learnhow/p/5694876.html
本文转自:https://github.com/fishpro/spring-boot-study/tree/master/spring-boot-study-jdbc
根据个人实践情况做一些注释
感谢fishpro的教程
-----------------------------------------------------
Apache Shiro 已经大名鼎鼎,搞 Java 的没有不知道的,这类似于 .Net 中的身份验证 form 认证。跟 .net core 中的认证授权策略基本是一样的。当然都不知道也没有关系,因为所有的权限都是模拟的人或机构的社会行为。
本系列从简单的权限讲起,主要涉及到 Shiro、Spring Security、Jwt、OAuth2.0及其他自定义权限策略。
本章主要讲解 Shiro 的基本原理与如何使用,本章主要用到以下基础设施:
- jdk1.8+
- spring boot 2.1.6
- idea 2018.1
1 Spring Boot 快速集成 Shiro 示例
首先我们来一段真实的代码演示下 Spring Boot 如何集成 Shiro 。本代码示例暂时没有使用到数据库相关知识,本代码主要使用到:
- shiro
- thymeeaf
本示例演示了网站用户 admin 密码 123456 的用户使用用户名密码登录网站,经过 Shiro 认证后,获取了授权权限列表,演示了权限的使用。
1.1 新建 Spring Boot Maven 示例工程项目
- File > New > Project,如下图选择
Spring Initializr
然后点击 【Next】下一步 - 填写
GroupId
(包名)、Artifact
(项目名) 即可。点击 下一步 groupId=com.fishpro
artifactId=shiro - 选择依赖
Spring Web Starter
前面打钩。 - 项目名设置为
spring-boot-study-shiro
.
1.2 依赖引入 Pom.xml
本代码主要使用到:
- shiro
- thymeeaf
在Pom.xml中加入以下代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fishpro</groupId>
<artifactId>shiro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shiro</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--shiro 1.4.0 thymeleaf-extras-shiro 2.0.0 组合-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!--shiro for thymeleaf 生效需要加入 spring boot 2.x 请使用 2.0.0 版本 否则使用 1.2.1版本-->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-b