FreeMarker中使用Shiro标签

在FreeMarker中使用Shiro标签时,需要以下几个步骤:

  1. 引入依赖的jar文件 引入jar文件:shiro-freemarker-tags,可以直接从中下载依赖文件然后引入到项目中即可。
  2. 添加配置类 配置类文件如下所示:
@Component
public class ShiroTagFreeMarkerConfigurer implements InitializingBean {
          
   
	@Autowired
	private Configuration configuration;
	@Autowired
	private FreeMarkerViewResolver resolver;
	@Override
	public void afterPropertiesSet() throws Exception {
          
   
		// 加上这句后,可以在页面上使用shiro标签
		configuration.setSharedVariable("shiro", new ShiroTags());
		// 加上这句后,可以在页面上用${context.contextPath}获取contextPath
		resolver.setRequestContextAttribute("context");
	}
}
  1. 逻辑认证器UserRealm 编写逻辑认证器UserRealm的代码逻辑,这里就省略了,可以参考中的写法。
  2. 在FreeMerker中使用Shiro标签 例如在会员中心的连接的标签中添加Shiro的验证,判断当前用户是否具有会员权限:
<@shiro.hasPermission name="user:vip">
   <a href="vip/index">会员中心</a></li>
</@shiro.hasPermission>
除了权限验证的Shiro标签,还有一些其他常见的标签,可以参考博文:
经验分享 程序员 微信小程序 职场和发展