Shiro Freemarker标签的使用

avatar 2019年01月22日12:51:18 9 3443 views
博主分享免费Java教学视频,B站账号:Java刘哥
如何想在 FreeMarker 的 ftl 模板文件里直接调用 shiro 的标签,需要添加依赖,并做简单的配置,下面简单介绍下,本文是建立在 SpringBoot + FreeMarker + Shiro 已经整合完成下,介绍 Shiro FreeMarker 标签的使用。

一、整合 freeMarker-shiro 标签

1.添加依赖
  1. <!--Shiro-FreeMarker标签-->
  2. <dependency>
  3.   <groupId>net.mingsoft</groupId>
  4.   <artifactId>shiro-freemarker-tags</artifactId>
  5.   <version>0.1</version>
  6. </dependency>
  2.FreeMarker 配置
  1. package com.liuyanzhao.sens.config;
  2. import com.jagregory.shiro.freemarker.ShiroTags;
  3. import com.liuyanzhao.sens.model.tag.CommonTagDirective;
  4. import com.liuyanzhao.sens.service.OptionsService;
  5. import freemarker.template.TemplateModelException;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.annotation.Configuration;
  9. import javax.annotation.PostConstruct;
  10. /**
  11.  * <pre>
  12.  *     FreeMarker配置
  13.  * </pre>
  14.  *
  15.  * @author : saysky
  16.  * @date : 2018/4/26
  17.  */
  18. @Slf4j
  19. @Configuration
  20. public class FreeMarkerConfig{
  21.     @Autowired
  22.     private freemarker.template.Configuration configuration;
  23.     @Autowired
  24.     private OptionsService optionsService;
  25.     @Autowired
  26.     private CommonTagDirective commonTagDirective;
  27.     @PostConstruct
  28.     public void setSharedVariable() {
  29.         try {
  30.             //自定义标签
  31.             configuration.setSharedVariable("commonTag", commonTagDirective);
  32.             configuration.setSharedVariable("options", optionsService.findAllOptions());
  33.             //shiro标签
  34.             configuration.setSharedVariable("shiro"new ShiroTags());
  35.             configuration.setNumberFormat("#");
  36.         } catch (TemplateModelException e) {
  37.             log.error("自定义标签加载失败:{}", e.getMessage());
  38.         }
  39.     }
  40. }
主要看43-44行  

二、标签的使用

1.guest(游客)
  1.  <@shiro.guest>
  2. 您当前是游客,<a href="javascript:void(0);" class="dropdown-toggle qqlogin" >登录</a>
  3. </@shiro.guest>
  2.user(已经登录,或者记住我登录)
  1. <@shiro.user>
  2. 欢迎[<@shiro.principal/>]登录,<a href="/logout">退出</a>
  3. </@shiro.user>
  3.authenticated(已经认证,排除记住我登录的)
  1.  <@shiro.authenticated>
  2. 用户[<@shiro.principal/>]已身份验证通过
  3. </@shiro.authenticated>
  4.notAuthenticated(和authenticated相反)
  1. <@shiro.notAuthenticated>
  2. 当前身份未认证(包括记住我登录的)
  3. </@shiro.notAuthenticated>
这个功能主要用途,识别是不是本次操作登录过的,比如支付系统,进入系统可以用记住我的登录信息,但是当要关键操作的时候,需要进行认证识别。   5.principal标签,这个要稍微重点讲讲。好多博客都是一下带过。 principal标签,取值取的是你登录的时候。在Realm实现类中的如下代码:
  1. ....
  2. return new SimpleAuthenticationInfo(user,user.getPswd(), getName());
  3. 在new SimpleAuthenticationInfo(第一个参数,....)的第一个参数放的如果是一个username,那么就可以直接用。
  4. <!--取到username-->
  5. <@shiro. principal/>
  6. 如果第一个参数放的是对象,比如我喜欢放User对象。那么如果要取username字段。
  7. <!--需要指定property-->
  8. <@shiro.principal property="username"/>
  9. 和Java如下Java代码一致
  10. User user = (User)SecurityUtils.getSubject().getPrincipals();
  11. String userusername = user.getUsername();
  6.hasRole标签(判断是否拥有这个角色)
  1. <@shiro.hasRole name="admin">
  2. 用户[<@shiro.principal/>]拥有角色admin<br/>
  3. </@shiro.hasRole>
  7.hasAnyRoles标签(判断是否拥有这些角色的其中一个
  1. <@shiro.hasAnyRoles name="admin,user,member">
  2. 用户[<@shiro.principal/>]拥有角色admin或user或member<br/>
  3. </@shiro.hasAnyRoles>
  8.lacksRole标签(判断是否不拥有这个角色)
  1. <@shiro.lacksRole name="admin">
  2. 用户[<@shiro.principal/>]不拥有admin角色
  3. </@shiro.lacksRole>
  9.hasPermission标签(判断是否有拥有这个权限)
  1. <@shiro.hasPermission name="user:add">
  2. 用户[<@shiro.principal/>]拥有user:add权限
  3. </@shiro.hasPermission>
  10.lacksPermission标签(判断是否没有这个权限)
  1. <@shiro.lacksPermission name="user:add">
  2. 用户[<@shiro.principal/>]不拥有user:add权限
  3. </@shiro.lacksPermission>
 
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:3   待审核评论数:0
  1. avatar 言曌

    评论测试!

  2. avatar 13

    12132好

  3. avatar wordpress建站

    技术牛叉