• SpringBoot项目Shiro的实现(一)


    一、Shiro的简单介绍

      Shiro是Apache下的一个开源项目,我们称之谓Apache Shiro,它是一个易用与Java项目的安全框架,提供了认证、授权、加密、会话管理,与Spring Security一样都是做一个权限的安全框架,但是与Spring Security

    相比,Shiro使用了更简单易懂易于使用的授权方式。

      Shiro的三大核心组件:

      1、Subject------------------当前用户

      2、SecurityManage--------管理所有的Subject

      3、Reamls------------------权限信息的验证

         我们需要实现Reamls的Authentication与Authorization,其中Authentication用于验证用户身份,Authorization用于授权访问控制。

      Shiro核心是通过Filter来实现,就好像SpringMVC用DispatchServlet来做控制一样。既然使用Filter,那么我们也可以猜到,Shiro是通过URL规则来进行过滤和权限校验,所以我们需要定义一系列的URL规则和访问权限。

      另外通过Shiro提供的会话管理可以获取Session中的信息,Shiro也提供缓存功能,使用CacheManage来管理。

    二、SpringBoot集成Shiro核心分析

      集成Shiro我们需要知道Shiro框架大概的一些管理对象。

         1、ShiroFilterFactory:Shiro过滤工厂类,具体的实现类是ShiroFilterFactoryBean,该实现类依赖与SecurityManage安全管理器。

       2、SecurityManage:Shiro的安全管理,主要是安全认证管理、缓存管理、cookie管理。所以在实际的开发中我们主要和SecurityManage打交道,ShiroFilterFactory主要配置好了Filter就可以了。

       3、Reamls:身份信息、权限信息的验证。

       4、缓存管理、记住密码等功能,这些大部分只要进行简单的实现,然后注入到SecurityManage让Shiro的安全管理器进行管理就好了。

    三、搭建无Shiro的Springboot项目

      我们先编写一个无Shiro的简单的框架,在这个框架中我们可以访问到index,login,userInfo,userInfoAdd。这个步骤对于有Spring Boot基础的就应该很简单了,在这里简单的介绍下:

      1、新建一个maven java project,取名为spring-boot-shiro

      2、在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.kfit</groupId>
      <artifactId>spring-boot-shiro1</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
     
      <name>spring-boot-shiro1</name>
      <url>http://maven.apache.org</url>
     
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
      </properties>
       
        <!--
           spring boot 父节点依赖,
           引入这个之后相关的引入就不需要添加version配置,
           spring boot会自动选择最合适的版本进行添加。
         -->
        <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>1.3.3.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>
       
      </dependencies>
    </project>

       3、编写网页文件:index.html,login.html,userInfo.html,userInfoAdd.html

        这些文件存在在src/main/resouces/templates, 这几个文件中都是简单的代码,只有登录界面中有账号和密码:

        inde.html:  

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>Insert title here</title>
    </head>
    <body>
        <h3>index</h3>
    </body>
    </html>

      login.html:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>Insert title here</title>
    </head>
    <body>
                错误信息:<h4 th:text="${msg}"></h4>
            <form action="" method="post">
                <p>账号:<input type="text" name="username" value="admin"/></p>
                <p>密码:<input type="text" name="password" value="123456"/></p>
                <P><input type="checkbox" name="rememberMe" />记住我</P>
                <p><input type="submit" value="登录"/></p>
            </form>
    </body>
    </html>

        其他页面都简单的一个标签而已,可自行编写。<h3>用户查询界面</h3>、<h3>用户添加界面</h3>

      4、Controller控制类

    @Controller
    public class HomeController {
       
        @RequestMapping({"/","/index"})
        public String index(){
           return "index";
        }
        
        @RequestMapping(value="/login",method=RequestMethod.GET)
        public String login(){
            return "login";
        }
    }

      5、编写启动类

    @SpringBootApplication
    public class App {
       
        public static void main(String[] args) {
           SpringApplication.run(App.class, args);
        }
       
    }

      这时候我们在运行我们的程序就应该可以访问index,login页面了。

     

    到此这个小节就结束了,现在我们的程序还有问题,就是index页面在没有登录的时候,就可以进行访问了,我们希望是如果直接访问index页面,如果没有登录的话,直接跳转到login进行登录。

    那么下一小节我们将会介绍如何在当前代码中集成shiro。

  • 相关阅读:
    (数据科学学习手札09)系统聚类算法Python与R的比较
    写完代码就去吃饺子|The 10th Henan Polytechnic University Programming Contest
    《四月物语》
    PAT(甲级)2017年春季考试
    PAT(甲级)2017年秋季考试
    2019年全国高校计算机能力挑战赛 C语言程序设计决赛
    CF#603 Div2
    redhat7 上安装dummynet
    cassandra 如何写数据以及放置副本
    Eclipse中设置VM参数
  • 原文地址:https://www.cnblogs.com/conswin/p/7478557.html
Copyright © 2020-2023  润新知