
本文基于【shiro基础】springboot整合shiro 进行整合。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.1.0</version>
</dependency>配置方言,使shiro能在thymeleaf中引入使用
/**
* 整合thymeleaf
*/
@Bean
public ShiroDialect shiroDialect(){
return new ShiroDialect();
}分别引入thymeleaf及shiro,注意保持标签与声明的命名空间一致。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<div th:if="${session.user == null}">
<h2>登录</h2>
<form th:action="@{/auth/sublogin}">
<input name="userName">
<input name="password" type="password">
<input type="submit" value="登录">
</form>
</div>
<hr>
<div th:if="${session.user != null}">
<h2 th:text="${session.user.username}"></h2>
<a th:href="@{/auth/logout}">退出登录</a>
<div shiro:hasPermission="demo:p1">
<a th:href="@{/demo/p1}">p1</a>
</div>
<div shiro:hasPermission="demo:p2">
<a th:href="@{/demo/p2}">p2</a>
</div>
</div>
</body>
</html>使用示例 | 说明 |
|---|---|
<shiro:authenticated> | 已登录 |
<shiro:notAuthenticated> | 未登录 |
<shiro:guest> | 用户在没有RememberMe时 |
<shiro:user> | 用户在RememberMe时 |
<shiro:hasAnyRoles name=“abc,123” > | 在有abc或者123角色时 |
<shiro:hasRole name=“abc”> | 拥有角色abc |
<shiro:lacksRole name=“abc”> | 没有角色abc |
<shiro:hasPermission name=“abc”> | 拥有权限资源abc |
<shiro:lacksPermission name=“abc”> | 没有abc权限资源 |
<shiro:principal> | 显示用户身份名称 |