shiro+thymeleaf 整合

SpringBoot中实现Shiro控制ThymeLeaf界面按钮级权限控制

 

移动开发

## 需求简述

在业绩核算系统中,我们使用了SpringBoot作为项目的整体架构,使用ThymeLeaf作为前端界面框架,使用Shiro作为我们的权限控制框架,Shiro作为轻量级的权限框架,使用起来非常方便,但是在使用的过程中我发现,Shiro作为页面级的权限控制框架非常好用,它可以注入到Controller中对页面级的访问权限做控制,但是如果我们想要实现页面中某个按钮或者列表的权限显示,单纯的在Controller中添加注解就显得捉襟见肘了。

 

## 解决方案

为了解决上述的需求,我们需要引入一个新的组件---------------Thymeleaf Extras Shiro ,这个组件的作用就是可以在thymeleaf中使用自定义标签并配合权限灵活的控制网页上的组件显示与否。

他的官方网站是:[Github][]

 

[Github]: theborakompanioni/thymeleaf-extras-shiro "thymeleaf-extras-shiro"

 

## 集成方法

首先我们需要在Pom.xml中引入这个组件的dependency



    com.github.theborakompanioni

    thymeleaf-extras-shiro

    2.0.0



 

引入完成后,直接启动应用会报错,因为thymeleaf-extras-shiro这个组件需要thymeleaf3.0支持,而Springboot 1.58版本默认的thymeleaf的版本是2.X的,所以我们还要将thymeleaf设置成3.0版本,具体代码如下,还是在pom.xml里:



    UTF-8

    UTF-8

    1.8

    3.0.0.RELEASE

    2.0.0

此处设置完毕后我们才是真正的将thymeleaf-extras-shiro引入进来了,之后我们还要在Shiro的Config文件中对设置进行相应的修改:
 

@Bean(name = "shiroDialect")

    public ShiroDialect shiroDialect(){

    return new ShiroDialect();

}

添加这段代码的目的就是为了在thymeleaf中使用shiro的自定义tag。

好了,现在基本上所有使用shiro-tag的条件都具备了,现在给出前端的代码示例:










Insert title here





index

Please login

Welcome back John! Not John? Click here to login.

Hello, , how are you today?

Update your contact information

Hello, , how are you today?

Please login in order to update your credit card information.

Administer the system

Sorry, you are not allowed to developer the system.

You are a developer and a admin.

You are a admin, vip, or developer.

添加用户

Sorry, you are not allowed to delete user accounts.

You can see or add users.

You can see or delete users.

Create a new User

 

这里注意一个细节,在html界面的顶部加一个tag标签引入:xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"

你可能感兴趣的:(java,springboot,shiro,thymeleaf)