1、概念
(1)概念
当用户认证进入到主页之后需要显示用户的信息以及用户的权限信息,Shiro提供了一整套的标签用于在页面进行权限信息的展示
(2)在Thymeleaf模板中的使用
- 导入thymeleaf模板对shiro支持的依赖:
<dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>2.0.0</version> </dependency>
- 在html页面中引入命名空间:
xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"
- 在配置类中配置Shiro的方言:
@Bean public ShiroDialect getShiroDialect(){ return new ShiroDialect(); }
2、<shiro:guest>
(1)在index页面运用<shiro:guest>标签
<!DOCTYPE html> <html lang="en" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <h3>index</h3> <hr/> <shiro:guest> 欢迎游客访问<a href="login.html">登录</a> </shiro:guest> </body> </html>
在过滤器中将index页面设置为不过滤的页面,直接访问index页面:
通过登录页面登录后跳转到index页面:
也就是说<shiro:guest>标签内部定义的内容,只有游客访问的时候才能看到,登录后的用户是看不到的
3、<shiro:user>标签
(1)在index页面书写<shiro:user>标签
<!DOCTYPE html> <html lang="en" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <h3>index</h3> <hr/> <shiro:guest> 欢迎游客访问<a href="login.html">登录</a> </shiro:guest> <shiro:user> 用户登录成功 </shiro:user> </body> </html>
登录后跳转到index页面:
不登录直接访问index页面:
<shiro:user>标签中的内容是只有用户登录之后才能看到,游客登录的话是看不到的
4、<shiro:principal/>标签
(1)在index页面中的<shiro:user>内部书写<shiro:principal/>标签
<!DOCTYPE html> <html lang="en" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <h3>index</h3> <hr/> <shiro:guest> 欢迎游客访问<a href="login.html">登录</a> </shiro:guest> <shiro:user> 用户[<shiro:principal/>]登录成功 </shiro:user> </body> </html>
登录后跳转到index页面,页面可以显示登录用户的用户名:
<shiro:principal/>标签不用添加任何内容,可以显示已经拥有认证身份的用户的用户名
5、<shiro:hasRole标签
(1)在index页面的<shiro:user>内部书写<shiro:hasRole标签
<!DOCTYPE html> <html lang="en" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <h3>index</h3> <hr/> <shiro:guest> 欢迎游客访问<a href="login.html">登录</a> </shiro:guest> <shiro:user> 用户[<shiro:principal/>]登录成功 当前用户的身份<shiro:hasRole name="管理员">管理员</shiro:hasRole> <shiro:hasRole name="学生">学生</shiro:hasRole> </shiro:user> </body> </html>
登录后跳转到index页面,页面可以显示用户的身份信息
查看数据库,该用户的身份为:
用户表:
用户身份表:
6、<shiro:hasPermission标签
(1)在index页面的<shiro:user>内部书写<shiro:hasPermission标签
<!DOCTYPE html> <html lang="en" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <h3>index</h3> <hr/> <shiro:guest> 欢迎游客访问<a href="login.html">登录</a> </shiro:guest> <shiro:user> 用户[<shiro:principal/>]登录成功 当前用户的身份<shiro:hasRole name="管理员">管理员</shiro:hasRole> <shiro:hasRole name="学生">学生</shiro:hasRole> <hr/> 学生: <ul> <shiro:hasPermission name="s_select"><li><a href="#">查询</a></li></shiro:hasPermission> </ul> <hr/> 教师: <ul> <shiro:hasPermission name="t_select"><li><a href="#">查询</a></li></shiro:hasPermission> </ul> <ul> <shiro:hasPermission name="t_delete"><li><a href="#">删除</a></li></shiro:hasPermission> </ul> <ul> <shiro:hasPermission name="t_update"><li><a href="#">修改</a></li></shiro:hasPermission> </ul> </shiro:user> </body> </html>
用身份为教师的用户登录:
身份为学生的用户登录:
用身份为超级管理员的用户账号登录:
不登录直接访问index页面(游客):
7、数据库设计
(1)用户表:users
(2)用户角色表:user_roles
(3)角色权限表:roles_permissions
总结:这些表的名称与字段的名称是固定的,也就是shiro默认的,因为我们并没有书写与操作数据库有关的代码。否则,会出现错误,提示字段或表不存在
8、shiro的认证流程
(1)用subject调用login方法,方法的参数包含用户名和密码
public void checkLogin(String username,String password) throws Exception{ Subject subject= SecurityUtils.getSubject(); UsernamePasswordToken token=new UsernamePasswordToken(username,password); subject.login(token); }
(2)将认证信息(用户名和密码)传递给SecurityManager
(3)SecurityManager调用认证器(Authenticator)进行认证
(4)Authenticator就会将token传递给绑定的realm,在realm中进行用户的认证检查,如果认证通过就正常执行,不通过就会抛出异常