Spring Boot Thymeleaf中使用Shiro标签 一、前言在《Spring-Boot-shiro权限控制》中当用户访问没有权限的资源时我们采取的做法是跳转到403页面但在实际项目中更为常见的做法是只显示当前用户拥有访问权限的资源链接。配合Thymeleaf中的Shiro标签可以很简单的实现这个目标。实际上Thymeleaf官方并没有提供Shiro的标签我们需要引入第三方实现地址为https://github.com/theborakompanioni/thymeleaf-extras-shiro。二、引入thymeleaf-extras-shiro!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro --dependencygroupIdcom.github.theborakompanioni/groupIdartifactIdthymeleaf-extras-shiro/artifactIdversion2.0.0/version/dependency三、ShiroConfig配置引入依赖后需要在ShiroConfig中配置该方言标签BeanpublicShiroDialectshiroDialect(){returnnewShiroDialect();}四、首页改造更改index.html用于测试Shiro标签的使用!DOCTYPEhtmlhtmlxmlns:thhttp://www.thymeleaf.orgxmlns:shirohttp://www.pollix.at/thymeleaf/shiroheadmetacharsetUTF-8title首页/title/headbodyp你好[[${user.userName}]]/ppshiro:hasRoleadmin你的角色为超级管理员/ppshiro:hasRoletest你的角色为测试账户/pdivashiro:hasPermissionuser:userth:href{/user/list}获取用户信息/aashiro:hasPermissionuser:addth:href{/user/add}新增用户/aashiro:hasPermissionuser:deleteth:href{/user/delete}删除用户/a/divath:href{/logout}注销/a/body/html值得注意的是在html页面中使用Shiro标签需要给html标签添加xmlns:shiro“http://www.pollix.at/thymeleaf/shiro”。五、测试启动项目使用wno704角色为admin具有user:useruser:adduser:delete权限账户登录使用test角色为test仅有user:user权限账户登录六、更多标签6.1 Attributepshiro:anyTagGoodbye cruel World!/p6.2 Elementshiro:anyTagpHello World!/p/shiro:anyTag6.3 The guest tagpshiro:guestPleaseahreflogin.htmlLogin/a/p6.4 The user tagpshiro:userWelcome back John! Not John? Clickahreflogin.htmlhereato login./p6.5 The authenticated tagashiro:authenticatedhrefupdateAccount.htmlUpdate your contact information/a6.6 The notAuthenticated tagpshiro:notAuthenticatedPleaseahreflogin.htmllogin/ain order to update your credit card information./p6.7 The principal tagpHello,spanshiro:principal/span, how are you today?/porpHello,shiro:principal/, how are you today?/p6.8 The hasRole tagashiro:hasRoleadministratorhrefadmin.htmlAdminister the system/a6.9 The lacksRole tagpshiro:lacksRoleadministratorSorry, you are not allowed to administer the system./p6.10 The hasAllRoles tagpshiro:hasAllRolesdeveloper, project managerYou are a developer and a project manager./p6.11 The hasAnyRoles tagpshiro:hasAnyRolesdeveloper, project manager, administratorYou are a developer, project manager, or administrator./p6.12 The hasPermission tagashiro:hasPermissionuser:createhrefcreateUser.htmlCreate a new User/a6.13 The lacksPermission tagpshiro:lacksPermissionuser:deleteSorry, you are not allowed to delete user accounts./p6.14 The hasAllPermissions tagpshiro:hasAllPermissionsuser:create, user:deleteYou can create and delete users./p6.15 The hasAnyPermissions tagpshiro:hasAnyPermissionsuser:create, user:deleteYou can create or delete users./p