前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【Spring Boot】017-Shiro整合Thymeleaf

【Spring Boot】017-Shiro整合Thymeleaf

作者头像
訾博ZiBo
发布2025-01-06 15:38:48
发布2025-01-06 15:38:48
7100
代码可运行
举报
运行总次数:0
代码可运行

最新更新:2020年9月22日08:14:22

一、Shiro整合Thymeleaf

1、导入坐标

代码语言:javascript
代码运行次数:0
复制
        <!--整合Thymeleaf-->
        <dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
        </dependency>

2、修改ShiroConfig类,添加shiro整合thymeleaf

代码语言:javascript
代码运行次数:0
复制
package com.zibo.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;

@Configuration
public class ShiroConfig {

    //1、创建Realm对象,需要自定义
    @Bean
    public AccountRealm accountRealm(){
        return new AccountRealm();
    }

    //2、DefaultWebSecurityManager
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("accountRealm")AccountRealm accountRealm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        //关联accountRealm
        manager.setRealm(accountRealm);
        return manager;
    }

    //3、ShiroFilterFactoryBean
    @Bean(name = "shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager")DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(manager);

        //添加shiro内置过滤器
        /*
         * anon:无需认证就可以访问;
         * authc:必须认证了才能访问;
         * user:必须拥有记住我功能才能访问(一般不用);
         * perms:拥有对某个资源的权限才能访问;
         * role:拥有某个角色权限才能访问;
         */

        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();


        //★★★授权:只有account:add权限的账户才能访问
        //注意:注意这是一个有序map,需要卸载拦截前面,否则不生效!!!
        filterMap.put("/account/add","perms[account:add]");

        //拦截:设置认证了才能访问
        filterMap.put("/account/*","authc");

        bean.setFilterChainDefinitionMap(filterMap);

        //设置登录页面
        bean.setLoginUrl("/toLogin");

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


        return bean;
    }


    //shiro整合thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }

}

3、修改index.html

代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>首页</h1>
    <p th:text="${msg}"></p>

    <div shiro:hasPermission="account:add">
        <a th:href="@{/account/add}">add</a>
    </div>
    <a th:href="@{/account/update}">update</a>
    <a th:href="@{/logout}">注销</a>
</body>
</html>

4、测试结果

二、登录前后的显示与隐藏等

1、修改index.html页面

代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>首页</h1>
    <p th:text="${msg}"></p>
    <!-- 判断当前用户是否已经认证,未认证就可以看到标签中的内容 -->
    <shiro:notAuthenticated>
        登录成功则隐藏
    </shiro:notAuthenticated>
    <br/>
    <!-- 判断当前用户是否已经认证,已认证就可以看到标签中的内容 -->
    <shiro:authenticated>
        登录成功则显示
    </shiro:authenticated>
    <br/>
    <!-- 判断当前用户是否拥有指定的权限 -->
    <shiro:hasPermission name="account:add">
        当前用户有account:add权限则显示
    </shiro:hasPermission>

    <br>

    <div shiro:hasPermission="account:add">
        <a th:href="@{/account/add}">add</a>
    </div>
    <a th:href="@{/account/update}">update</a>
    <a th:href="@{/logout}">注销</a>
</body>
</html>

2、测试结果

三、源代码

链接:https://pan.baidu.com/s/1UcX7tWIBZxJtt4ViGYC2mg 提取码:zibo 复制这段内容后打开百度网盘手机App,操作更方便哦

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Shiro整合Thymeleaf
    • 1、导入坐标
    • 2、修改ShiroConfig类,添加shiro整合thymeleaf
    • 3、修改index.html
    • 4、测试结果
  • 二、登录前后的显示与隐藏等
    • 1、修改index.html页面
    • 2、测试结果
  • 三、源代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档