一、引入依赖(已解决版本冲突)

<!-- shiro-freemarker-tags start -->
<dependency>
<groupId>net.mingsoft</groupId>
<artifactId>shiro-freemarker-tags</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
</exclusion>
<exclusion>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- shiro-freemarker-tags end --> <!-- freemarker start -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<!-- freemarker end --> <!-- shiro begin -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>1.4.0</version>
</dependency>
<!-- shiro end -->

二、配置

Java代码:

public class FreeMarkerConfigExtend extends FreeMarkerConfigurer {
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
super.afterPropertiesSet();
Configuration cfg = this.getConfiguration();
// 添加shiro标签
cfg.setSharedVariable("shiro", new ShiroTags());
}
}
<!-- freemarker环境配置 -->
<bean id="freemarkerConfig" class="com.demo.shiro.common.freemarker.FreeMarkerConfigExtend">
<!-- 模版位置,这里配置了下面就不用配了 -->
<property name="templateLoaderPath" value="/WEB-INF/views" />
<property name="freemarkerSettings"><!-- 一些设置 -->
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.##########</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
<prop key="auto_import">
<!-- 自动装载,引入Freemarker,用于Freemarker Macro引入 -->
/common/_meta.ftl as _meta,
/common/_footer.ftl as _footer
<!--/common/menu.ftl as _menu-->
</prop>
</props>
</property>
</bean>

三、shiro标签详解

1. guest(游客)

<@shiro.guest>
您当前是游客,<a href="javascript:void(0);">登录</a>
</@shiro.guest>

2. user(已经登录,或者记住我登录)

<@shiro.user>
欢迎[<@shiro.principal/>]登录,<a href="/logout.shtml">退出</a>
</@shiro.user>

3. authenticated(已经认证,排除记住我登录的)

<@shiro.authenticated>
用户[<@shiro.principal/>]已身份验证通过
</@shiro.authenticated>

4. notAuthenticated(和authenticated相反)

<@shiro.notAuthenticated>
当前身份未认证(包括记住我登录的)
</@shiro.notAuthenticated>

该功能主要用途:识别是不是本次操作登录过的,比如支付系统,进入系统可以用记住我的登录信息,但是当要关键操作的时候,需要进行认证识别。

5. principal标签

principal标签,取值取的是你登录的时候。在Realm实现类中的如下代码:

...
return new SimpleAuthenticationInfo(user,user.getPswd(), getName());

在 new SimpleAuthenticationInfo(第一个参数,....) 的第一个参数放的如果是一个username,那么就可以直接用。

<!--取到username-->
<@shiro. principal/>

如果第一个参数放的是对象,比如放User对象。那么如果要取username字段。

<!--需要指定property-->
<@shiro.principal property="username"/>

和Java如下Java代码一致

User user = (User) SecurityUtils.getSubject().getPrincipals();
String username = user.getUsername();

6. hasRole标签(判断是否拥有这个角色)

<@shiro.hasRole name="admin">
用户[<@shiro.principal/>]拥有角色admin<br/>
</@shiro.hasRole>

7. hasAnyRoles标签(判断是否拥有这些角色的其中一个)

<@shiro.hasAnyRoles name="admin,user,member">
用户[<@shiro.principal/>]拥有角色admin或user或member<br/>
</@shiro.hasAnyRoles>

8. lacksRole标签(判断是否不拥有这个角色)

<@shiro.lacksRole name="admin">
用户[<@shiro.principal/>]不拥有admin角色
</@shiro.lacksRole>

9. hasPermission标签(判断是否有拥有这个权限)

<@shiro.hasPermission name="user/add">
用户[<@shiro.principal/>]拥有user/add权限
</@shiro.hasPermission>

10. lacksPermission标签(判断是否没有这个权限)

<@shiro.lacksPermission name="user/add">
用户[<@shiro.principal/>]不拥有user/add权限
</@shiro.lacksPermission>

原文地址:https://www.sojson.com/blog/143.html

Freemarker 的 Shiro 标签使用详解的更多相关文章

  1. Shiro 安全框架详解二(概念+权限案例实现)

    Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...

  2. Shiro 安全框架详解一(概念+登录案例实现)

    shiro 安全框架详细教程 总结内容 一.RBAC 的概念 二.两种常用的权限管理框架 1. Apache Shiro 2. Spring Security 3. Shiro 和 Spring Se ...

  3. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  4. HTML标签----图文详解(二)

    HTML标签超详细的图文演示再来一波~~~ 如果还没有看过昨天的福利的,那可要抓紧喽,传送门:HTML标签----图文详解 本文主要内容 列表标签 表格标签 框架标签及内嵌框架<iframe&g ...

  5. HTML标签----图文详解

    国庆节快乐,还在加班的童鞋,良辰必有重谢! 本文主要内容 头标签 排版标签:<p>     <br>     <hr>     <center>     ...

  6. Struts标签库详解【3】

    struts2标签库详解 要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部加入以下的代码: <%@taglib prefix="s" uri= ...

  7. Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  8. 【jsp】JSTL标签大全详解

    一.JSTL标签介绍 1.什么是JSTL? JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便,它与JSP动作标签一样,只不过它不是 ...

  9. (转) shiro权限框架详解06-shiro与web项目整合(上)

    http://blog.csdn.net/facekbook/article/details/54947730 shiro和web项目整合,实现类似真实项目的应用 本文中使用的项目架构是springM ...

随机推荐

  1. AES地址栏传参加密

    在实际开发项目中,有些数据在前后端的传输过程中需要进行加密,那就需要保证前端和后端的加解密需要统一.这里给大家简单演示AES在JavaScript前端和Java后端是如何实现加密和解密的. 直接上代码 ...

  2. [Linux]文件浏览

    1.使用file命令查看文件中数据的类型 [oracle@linuxforlijiaman Desktop]$ ls linux oracle.txt test.png [oracle@linuxfo ...

  3. DB数据源配置之抽象(〇)

    DB数据源配置之抽象(〇) liuyuhang原创,未经允许禁止转载 DB数据源之SpringBoot+Mybatis踏坑过程实录(一) DB数据源之SpringBoot+MyBatis踏坑过程(二) ...

  4. MySQL的数据类型(一)

    每一个常量.变量和参数都有数据类型.它用来指定一定的存储格式.约束和有效范围.MySQL提供了多种数据类型.主要有数值型.字符串类型.日期和时间类型.不同的MySQL版本支持的数据类型可能会稍有不同. ...

  5. 小程序swiper不显示图片

    按照文档上的代码运行后,发现图片不显示 解决办法: app.wxss文件 align-items: center;这句话删除了,运行 OK!

  6. Firewalld共享上网及本地yum仓库搭建

    1.firewalld共享上网 1.服务端操作(有外网的服务器) 1.开启防火墙并加入开机自启动 [root@zeq ~]# systemctl start firewalld [root@zeq ~ ...

  7. 简单Maven Dos命令语句

    简单Maven Dos命令语句: 打包:mvn package 编译:mvn compile 编译测试程序:mvn test-compile 清空:mvn clean 运行测试:mvn test 生成 ...

  8. linux总结及常用命令

    一.操作系统的作用: 1.是现代计算机系统中最基本和最重要的系统软件  2.承上启下的作用  3.向下对硬件操作进行封装  4.向上对用户和应用程序提供方便访问硬件的接口 二.不同领域的操作系统: 1 ...

  9. 微信小程序终于审核过了

    终于,我做的微信小程序审核结束了,虽然被退回来两次,但是第三次还是审核通过了! 加油骚年,相信自己!! 有什么问题可以评论告诉我!!

  10. 04 mysql 基础三 (进阶)

    mysql 基础三 阶段一 mysql 单表查询 1.查询所有记录 select * from department; ​ select * from student; ​ select * from ...