`

162. Spring Boot 在thymeleaf使用shiro标签

阅读更多

 

 

需求缘起:

shirojsp配合使用的时候,有针对jspshiro标签,当我们使用的是thymeleaf也是可以使用shiro标签的,这就是本节要解决的问题。

本节大纲:

(1)添加依赖
(2)在ShiroConfiguration添加配置
(3)在index方法中存放userInfo
(4)调整index.html

 

       接下来看下具体的内容:

1)添加依赖

<dependency>
           <groupId>com.github.theborakompanioni</groupId>
           <artifactId>thymeleaf-extras-shiro</artifactId>
           <version>1.0.2</version>
</dependency>

 

 

2)在ShiroConfiguration添加配置

    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }

 

 

3)在index方法中存放userInfo对象

       修改HomeControllerindex方法:

    @RequestMapping(value={"/","/index"})
    public String index(Map<String,Object> map){
       System.out.println("HomeController.index()");
       //获取到用户信息;
       Subject subject  = SecurityUtils.getSubject();
       UserInfo ui = (UserInfo) subject.getPrincipal();
       map.put("userInfo",ui);
       return "/index";
    }

 

 

4)调整index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
   
    <h3>主界面--<span th:text="'登录账号:'+${userInfo.username}+'--名称:'+${userInfo.name}">user</span></h3>
    <a href="/logout">退出</a>
    <hr />
   
   
    <!-- 这些实际从数据库中进行查询,可以在 -->
    <h5>这是通过shiro标签的方式;</h5>
    <p shiro:hasRole="admin">
           <a>管理员角色</a>
    </p>
   
    <p shiro:hasPermission="userInfo:query">
           <a href="/userInfo/userList">用户查询</a>
    </p>
    <p shiro:hasPermission="userInfo:add">
           <a href="/userInfo/userAdd">用户添加</a>
    </p>
 
 
       <hr />
    <h5>从数据库加载菜单</h5>
    <div th:each="roleList:${userInfo.roleList}">
       <span th:text="${roleList.description}"></span>
       <!-- 角色下所有的所有菜单,实际开发中,只需要获取所有的权限就可以了,无所谓是哪个角色的; -->
       <p style="color:blue;margin-left:30px;" th:each="permission:${roleList.permissions}">
           <a th:href="${permission.url}" th:text="${permission.name}"></a>
       </p>
    </div> 
 
</body>
</html>

 

 

 à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

分享到:
评论
11 楼 hhht_yyaa 2018-03-14  
小弱鸡 写道
小弱鸡 写道
林祥纤 写道
小弱鸡 写道
版本号应该看谁和谁的,小白,望博主不要介意


我猜很有可能是没有下载下来jar包!你看看本地存在不

我在maven的扩展包里确实没有找到这个jar,那应该怎么弄

这个jar不存在,去哪找呢


http://blog.csdn.net/qq_31591883/article/details/76638848
关于这个抽象类找不到的问题参考这个链接,有解决办法
10 楼 小弱鸡 2017-12-21  
小弱鸡 写道
林祥纤 写道
小弱鸡 写道
版本号应该看谁和谁的,小白,望博主不要介意


我猜很有可能是没有下载下来jar包!你看看本地存在不

我在maven的扩展包里确实没有找到这个jar,那应该怎么弄

这个jar不存在,去哪找呢
9 楼 小弱鸡 2017-12-21  
林祥纤 写道
小弱鸡 写道
版本号应该看谁和谁的,小白,望博主不要介意


我猜很有可能是没有下载下来jar包!你看看本地存在不

我在maven的扩展包里确实没有找到这个jar,那应该怎么弄
8 楼 林祥纤 2017-12-19  
小弱鸡 写道
版本号应该看谁和谁的,小白,望博主不要介意


我猜很有可能是没有下载下来jar包!你看看本地存在不
7 楼 小弱鸡 2017-12-19  
版本号应该看谁和谁的,小白,望博主不要介意
6 楼 小弱鸡 2017-12-19  
没有的,那应该怎么解决
5 楼 林祥纤 2017-12-19  
小弱鸡 写道
博主啊,从小白看起,真切感觉非常棒!但是在这一节出了问题,完全按照步骤老的。
Error creating bean with name 'shiroDialect' defined in class path resource [cn/springboot/config/ShiroConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [at.pollux.thymeleaf.shiro.dialect.ShiroDialect]: Factory method 'shiroDialect' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/processor/attr/AbstractTextChildModifierAttrProcessor

望博主给看看,蟹蟹


NoClassDefFoundError: org/thymeleaf/processor/attr/AbstractTextChildModifierAttrProcessor

1、看看版本号是否一致的。
2、看看本地是否存在jar文件;
4 楼 小弱鸡 2017-12-19  
博主啊,从小白看起,真切感觉非常棒!但是在这一节出了问题,完全按照步骤老的。
Error creating bean with name 'shiroDialect' defined in class path resource [cn/springboot/config/ShiroConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [at.pollux.thymeleaf.shiro.dialect.ShiroDialect]: Factory method 'shiroDialect' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/processor/attr/AbstractTextChildModifierAttrProcessor

望博主给看看,蟹蟹
3 楼 小弱鸡 2017-12-19  
 
2 楼 林祥纤 2017-10-24  
zhmailm 写道
从头看到尾,不仅看到lZ知识的透彻,代码直接以达到目的为引,一步一步的解决问题。而且能坚持折磨长时间去更新,这跟更值得我去学习!
希望LZ继续保持!在这里给你加加油!


感谢,很感谢,看来你是看的很感悟了,不错,不错!
1 楼 zhmailm 2017-10-24  
从头看到尾,不仅看到lZ知识的透彻,代码直接以达到目的为引,一步一步的解决问题。而且能坚持折磨长时间去更新,这跟更值得我去学习!
希望LZ继续保持!在这里给你加加油!

相关推荐

Global site tag (gtag.js) - Google Analytics