首页 > 其他分享 >【WEEK14】 【DAY2】Shiro第七部分【中文版】

【WEEK14】 【DAY2】Shiro第七部分【中文版】

时间:2024-06-02 17:30:58浏览次数:13  
标签:WEEK14 登录 15.8 boot DAY2 shiro org Shiro user

2024.5.28 Tuesday
接上文【WEEK14】 【DAY1】Shiro第六部分【中文版】

目录

15.8.Shiro整合Thymeleaf

15.8.1.修改pom.xml添加依赖

15.8.1.1.shiro-thymeleaf整合包导入

官网:Maven Repository: com.github.theborakompanioni » thymeleaf-extras-shiro (mvnrepository.com)
在这里插入图片描述

<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

15.8.1.2.当前完整的pom文件

<?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 https://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.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>shiro-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>shiro-springboot</name>
    <description>shiro-springboot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <!--
        Subject 用户
        SecurityManager 管理所有用户
        Realm   连接数据
        -->
        <!--shiro_thymeleaf整合-->
        <!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
        <dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <!--log4j-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--druid-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <!--mybatis-->
        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>

        <!--shiro整合spring的包-->
        <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

15.8.2.修改ShiroConfig.java

package com.P40.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //要配置什么:点击进入ShiroFilterFactoryBean源码查看
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);

        //添加shiro的内置过滤器
        /*
        anon: 无需认证就可以访问
        authc: 必须认证了才能访问
        user: 必须拥有记住我功能才能用
        perms: 拥有对某个资源的权限才能访问
        role: 拥有某个角色权限
         */
        //登录拦截
        Map<String,String> filterMap = new LinkedHashMap<>();

        //授权。正常情况下,应该跳转到未授权页面,但是此时由于只添加了以下的验证,导致直接跳转到401页面
        filterMap.put("/user/add","perms[user:add]");
        filterMap.put("/user/update","perms[user:update]");

//        filterMap.put("/user/add","authc");
//        filterMap.put("/user/update","authc");
        //仅在此处修改add和update页面的访问权限后,重启项目,点击add或update都将被拦截,显示404错误,希望跳转到登录页面
        filterMap.put("/user/*","authc");   //也可以使用通配符实现(替代上两行的/user/add和/user/update)
        bean.setFilterChainDefinitionMap(filterMap);

        //如果没有权限,则需要跳转到登录页
        bean.setLoginUrl("/toLogin");   //设置登录的请求

        //未授权页面
        bean.setUnauthorizedUrl("/noauth");

        return bean;
    }

    //DefaultWebSecurityManager
    @Bean(name = "securityManager") //给这个类起别名,便于ShiroFilterFactoryBean调用
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){    //获取UserRealm,但是这里好像不需要使用注解,可以直接调用
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //默认的DefaultWebSecurityManager类的类名是defaultWebSecurityManager,只是在这里修改成securityManager

        //关联UserRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建realm对象,需要自定义类
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    //创建时顺序是相反的(从real->DefaultWebSecurityManager->ShiroFilterFactoryBean)

    //整合ShiroDialect(方言):用于整合Shiro和thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}

15.8.3.修改index.html

<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>

<!--不在登录状态时才显示“Login 登录”的入口-->
<div shiro:notAuthenticated>
    <a th:href="@{/toLogin}">Login 登录</a>
</div>

<p th:text="${msg}"></p>
<hr>    <!--在 HTML 页面中创建一条水平线-->

<!--仅当用户在数据库中有对应权限时才能在页面上显示该功能入口-->
<div shiro:hasPermission="user:add">
    <a th:href="@{/user/add}">add</a>
</div>
<div shiro:hasPermission="user:update">
    <a th:href="@{/user/update}">update</a>
</div>
<!--a标签定义超链接,用于从一个页面链接到另一个页面-->

<!--在登录状态时才显示“Sign out 退出登录”的入口-->
<hr>
<div shiro:Authenticated>
    <a class="nav-link" th:href="@{/logout}" onclick="alert('已退出')">Sign out 退出登录</a><!--单词別拼错了href不是herf-->
</div>

</body>
</html>

15.8.4.给root用户开放所有权限(为了试验罢了)

在这里插入图片描述

15.8.5.修改MyController.java

只修改了退出登录的方法

//退出登录
@RequestMapping("/logout")
public String logout(){
    Subject currentUser = SecurityUtils.getSubject();
    currentUser.logout();
    return "/index";    //修改成:退出登录后跳转到首页
}

15.8.6.重启

15.8.6.1.没有用户登录时

在这里插入图片描述

15.8.6.2.点击Login 登录

只在主页上显示该登录用户有权限的功能
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

15.8.6.3.点击Sign out 退出登录

修改成返回主页面了(在请求授权时是直接跳转到登录页)
在这里插入图片描述
在这里插入图片描述

标签:WEEK14,登录,15.8,boot,DAY2,shiro,org,Shiro,user
From: https://blog.csdn.net/2401_83329143/article/details/139146808

相关文章

  • 【WEEK14】 【DAY3】Swagger Part 1【English Version】
    2024.5.29WednesdayContents16.Swagger16.1.IntroductiontoSwagger16.1.1.Front-EndandBack-EndSeparation16.1.2.TheEraofFront-EndandBack-EndSeparation16.1.3.IssuesArising16.1.4.Solution16.1.5.Swagger16.2.IntegratingSwaggerintoSpringBoot1......
  • 动手学机器学习入门之Day2-梯度下降和多元线性回归
    前言前面我们已经学习的最小二乘法属于多元线性回归的主要概念,所以在看这篇文章之前,请确保你已经了解了最小二乘法,详情请见我的博客动手学机器学习入门之Day1。在机器学习领域,梯度下降和多元线性回归是两个至关重要的概念,它们为我们理解和构建复杂模型提供了基础。梯度下降......
  • 【WEEK14】 【DAY5】Swagger第三部分【中文版】
    2024.5.31Friday接上文【WEEK14】【DAY4】Swagger第二部分【中文版】目录16.6.配置API分组16.6.1.修改SwaggerConfig.java16.6.2.重启16.7.实体配置16.7.1.新建pojo文件夹16.7.2.修改HelloController.java16.7.3.重启16.8.常用注解16.8.1.Swagger的所有注解定义在i......
  • UNR#3 Day2
    A.白鸽是否有解即判欧拉回路,判每个结点的度数是否都是偶数和有边的的结点是否与\(1\)号结点连通即可。因为最后形成的是一个封闭图形,大可把\(x\)轴正方向看做一条射线,我们每由上至下穿过这条射线会对答案产生\(1\)的贡献,反之,从下至上穿过这条射线就会对答案产生\(-1\)......
  • 【WEEK14】 【DAY2】Shiro Part 7【English Version】
    2024.5.28TuesdayContinuationfromprevious【WEEK14】【DAY1】ShiroPart6【EnglishVersion】Contents15.8.IntegrateShirowithThymeleaf15.8.1.Modifypom.xmltoAddDependencies15.8.1.1.Importingtheshiro-thymeleafIntegrationPackage15.8.1.2.......
  • [CEOI2010 day2] pin
    [CEOI2010day2]pin题目信息题目链接LuoguP6521题目描述给定\(n\)个长度为\(4\)的字符串,你需要找出有多少对字符串满足恰好\(D\)个对应位置的字符不同。输入格式输入第一行两个整数\(n,D\)。接下来的\(n\)行,每行一个长度为\(4\)的字符串。输出格式输出一行......
  • 代码随想录算法训练Day20|LeetCode654-最大二叉树、LeetCode617-合并二叉树、LeetCode
    最大二叉树题目描述力扣654-最大二叉树给定一个不重复的整数数组nums。最大二叉树可以用下面的算法从nums递归地构建:创建一个根节点,其值为nums中的最大值。递归地在最大值左边的子数组前缀上构建左子树。递归地在最大值右边的子数组后缀上构建右子树。......
  • Shiro+Jwt+Redis
    如何整合Shiro+Jwt+Redis,以及为什么要这么做我个人认为①为什么用shiro:“Shiro+Jwt+Redis”模式和“单纯的shiro”模式相比,主要用的是shiro里面的登录认证和权限控制功能②为什么用jwt:“Shiro+Jwt”模式和“Shiro+Cookie”模式相比,后者的用户登录信息是存储在服务器的......
  • 【刷题笔记Day2】数组|977.有序数组的平方、209. 长度最小的子数组、59.螺旋矩阵II
    文章目录977.有序数组的平方解题思路遇到的问题及解决方案209.长度最小的子数组解题思路遇到的问题及解决方案59.螺旋矩阵II解题思路遇到的问题及解决方案总结977.有序数组的平方题目描述:给你一个按非递减顺序排序的整数数组nums,返回每个数字的平方组成的新......
  • 卡尔的算法训练营day2,数组2
    第一题做错了,还是边界值的问题。忘记存草稿了。题号997publicstaticintfindJudge(intn,int[][]trust){int[]judgeCandidate=newint[n+1];int[]othersCandidate=newint[n+1];for(inti=0;i<trust.length;i++){//二维数组......