Springweb

Spring Boot整合jpa,Shiro进行权限管理

2019-02-25  本文已影响37人  树蜂

转:https://blog.csdn.net/liuchuanhong1/article/details/76179601

本来想写一篇spring boot整合Shiro实现权限验证的文章,发现这篇写的非常不错,就直接借鉴了!

(1). Shiro简单介绍

Shiro是Apache下的一个开源项目,我们称之为Apache Shiro。它是一个很易用与Java项目的的安全框架,提供了认证、授权、加密、会话管理,与spring Security 一样都是做一个权限的安全框架,但是与Spring Security 相比,在于 Shiro 使用了比较简单易懂易于使用的授权方式。
Apache Shiro 的三大核心组件

image

Apache Shiro 核心通过 Filter 来实现,就好像SpringMvc 通过DispachServlet 来主控制一样。
既然是使用 Filter 一般也就能猜到,是通过URL规则来进行过滤和权限校验,所以我们需要定义一系列关于URL的规则和访问权限。
另外我们可以通过Shiro 提供的会话管理来获取Session中的信息。Shiro 也提供了缓存支持,使用 CacheManager 来管理。

官方网站:http://shiro.apache.org/

完整架构图:

image

Shiro是很强大的一个安全框架,这里只是抛装引玉下,还有很多的需要大家自己去学习Shiro。

(2). 集成Shiro核心分析
集成Shiro的话,我们需要知道Shiro框架大概的一些管理对象。
第一:ShiroFilterFactory,Shiro过滤器工厂类,具体的实现类是:ShiroFilterFactoryBean,此实现类是依赖于SecurityManager安全管理器。
第二:SecurityManager,Shiro的安全管理,主要是身份认证的管理,缓存管理,cookie管理,所以在实际开发中我们主要是和SecurityManager进行打交道的,ShiroFilterFactory主要配置好了Filter就可以了。当然SecurityManager并进行身份认证缓存的实现,我们需要进行对应的编码然后进行注入到安全管理器中。
第三:Realm,用于身份信息权限信息的验证。
第四:其它的就是缓存管理,记住登录之类的,这些大部分都是需要自己进行简单的实现,然后注入到SecurityManager让Shiro的安全管理器进行管理就好了。

(3). 无Shiro的Spring Boot
我们先编写一个无Shiro的简单的框架,在这个框架中我们可以访问到index,login,userInfo,userInfoAdd。
这个步骤对于有Spring Boot基础的就应该很简单了,在这里简单的介绍下:
(a) 新建一个maven Java project,取名为spring-boot-shiro1
(b) 在pom.xml中引入基本依赖,在这里还没有引入shiro等的依赖:

<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>  
    <groupId>com.example</groupId>  
    <artifactId>spring-boot-shiro1</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <packaging>jar</packaging>  
  
  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
  
  
    <!-- Inherit defaults from Spring Boot -->  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.4.0.RELEASE</version>  
    </parent>  
  
  
    <dependencies>  
        <!-- spring boot web支持:mvc,aop... -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <!-- thmleaf模板依赖. -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-thymeleaf</artifactId>  
        </dependency>  
         
       <!-- 热部署 -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-devtools</artifactId>  
            <optional>true</optional>  
        </dependency>  
  
    </dependencies>  
</project>

(c) 编写网页文件:
index.html,login.html,userInfo.html,userInfoAdd.html
这个文件存在在src/main/resouces/templates, 这几个文件中都是简单的代码,只有登录界面中有账号和密码:

index.html

1.  <!DOCTYPE html>  
2.  <html>  
3.  <head>  
4.  <meta charset="UTF-8" />  
5.  <title>Insert title here</title>  
6.  </head>  
7.  <body>  
8.  <[h3](https://www.baidu.com/s?wd=h3&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd)>index</h3>  
9.  </body>  
10.  </html>
上一篇下一篇

猜你喜欢

热点阅读