• Shiro学习2----spring boot整合(JdbcRealm)


    1.JdbcRealm介绍

    认证流程(JdbcRealm)

    • subject调用login方法,将包含用户名密码的token传递给SecurityManager
    • SecurityManager就会调用认证器(Authenticator)进行认证
    • Authenticator就将token传递给绑定的Realm,在Realm中进行用户的认证检查;如果认证通过则正常执行,不通过则抛出认证异常

    2.JdbcRealm规定的表结构

    • 用户信息表:users
      1. create table users(
      2. id int primary key auto_increment,
      3. username varchar(60) not null unique,
      4. password varchar(20) not null,
      5. password_salt varchar(20)
      6. );
      7. insert into users(username,password) values("zhagnsan","123456");
      8. insert into users(username,password) values("lisi","123456");
      9. insert into users(username,password) values("wangwu","123456");
      10. insert into users(username,password) values("zhaoliu","123456");
      11. insert into users(username,password) values("chenqi","123456");
    • 角色信息表:user_roles
      1. create table user_roles(
      2. id int primary key auto_increment,
      3. username varchar(60) not null,
      4. role_name varchar(100) not null
      5. );
      6. --admin系统管理员
      7. --cmanager库管人员
      8. --xmanager销售人员
      9. --kmanager客服人员
      10. --zmanager行政人员
      11. insert into user_roles(username,role_name) values("zhagnsan",'admin');
      12. insert into user_roles(username,role_name) values("lisi",'cmanager');
      13. insert into user_roles(username,role_name) values("wangwu",'xmanager');
      14. insert into user_roles(username,role_name) values("zhaoliu",'kmanager');
      15. insert into user_roles(username,role_name) values("chenqi",'zmanager');
    •  权限信息表:roles_permissions
    1. create table roles_permissions(
    2. id int primary key auto_increment,
    3. role_name varchar(100) not null,
    4. permission varchar(100) not null
    5. );
    6. insert into roles_permissions(role_name,permission) values('admin','*');
    7. insert into roles_permissions(role_name,permission) values("cmanager","sys:c:save");
    8. insert into roles_permissions(role_name,permission) values("cmanager","sys:c:delete");
    9. insert into roles_permissions(role_name,permission) values("cmanager","sys:c:update");
    10. insert into roles_permissions(role_name,permission) values("cmanager","sys:c:find");
    11. insert into roles_permissions(role_name,permission) values("xmanager","sys:c:find");
    12. insert into roles_permissions(role_name,permission) values("xmanager","sys:x:save");
    13. insert into roles_permissions(role_name,permission) values("xmanager","sys:x:delete");
    14. insert into roles_permissions(role_name,permission) values("xmanager","sys:x:update");
    15. insert into roles_permissions(role_name,permission) values("xmanager","sys:x:find");
    16. insert into roles_permissions(role_name,permission) values("xmanager","sys:k:save");
    17. insert into roles_permissions(role_name,permission) values("xmanager","sys:k:delete");
    18. insert into roles_permissions(role_name,permission) values("xmanager","sys:k:update");
    19. insert into roles_permissions(role_name,permission) values("xmanager","sys:k:find");
    20. insert into roles_permissions(role_name,permission) values("kmanager","sys:k:find");
    21. insert into roles_permissions(role_name,permission) values("kmanager","sys:k:update");
    22. insert into roles_permissions(role_name,permission) values("zmanager","sys:*:find");

    3.SpringBoot整合Shiro

    创建spring boot应用添加依赖(同1)

    配置shiro

    1. package com.qfedu.shiro3.config;
    2. import org.apache.shiro.realm.jdbc.JdbcRealm;
    3. import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
    4. import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import javax.sql.DataSource;
    8. import java.util.HashMap;
    9. import java.util.Map;
    10. @Configuration
    11. public class ShiroConfig {
    12. //shiro方言
    13. @Bean
    14. public ShiroDialect getShiroDialect(){
    15. return new ShiroDialect();
    16. }
    17. @Bean
    18. public JdbcRealm getJdbcReam(DataSource dataSource){
    19. JdbcRealm jdbcRealm = new JdbcRealm();
    20. //JdbcRealm会自动从数据库查询用户及权限数据(数据库的表结构要符合JdbcRealm的规范)
    21. jdbcRealm.setDataSource(dataSource);
    22. //JdbcRealm默认开启认证功能,需要手动开启授权功能
    23. jdbcRealm.setPermissionsLookupEnabled(true);
    24. return jdbcRealm;
    25. }
    26. @Bean
    27. public DefaultWebSecurityManager getDefaultWebSecurityManager(JdbcRealm jdbcRealm){
    28. DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    29. securityManager.setRealm(jdbcRealm);
    30. return securityManager;
    31. }
    32. @Bean
    33. public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager){
    34. ShiroFilterFactoryBean filter = new ShiroFilterFactoryBean();
    35. //过滤器就是shiro进行权限校验的核心,进行认证和授权是需要SecurityManager的
    36. filter.setSecurityManager(securityManager);
    37. /*设置shiro的拦截规则
    38. anon 匿名用户可访问
    39. authc 认证用户可访问
    40. user 使用RemeberMe的用户可访问
    41. perms 对应权限可访问
    42. role 对应角色可访问
    43. */
    44. Map filterMap=new HashMap<>();
    45. filterMap.put("/", "anon");
    46. filterMap.put("/login.html", "anon");
    47. filterMap.put("/regist.html", "anon");
    48. filterMap.put("/user/login", "anon");
    49. filterMap.put("user/regist", "anon");
    50. filterMap.put("/static/**", "anon");
    51. filterMap.put("/**", "authc");
    52. filter.setFilterChainDefinitionMap(filterMap);
    53. filter.setLoginUrl("/login.html");
    54. //设置未授权访问的页面路径
    55. filter.setUnauthorizedUrl("/login.html");
    56. return filter;
    57. }
    58. }

    4.认证页面同1

    5.Shiro标签的使用

    当用户认证进入到主页面后,需要显示用户信息和当前用户的权限信息,shiro就提供了一套标签用在页面来进行权限数据的呈现

    shiro提供了可供jsp使用的标签以及Thymeleaf中标签

    jsp页面引用

    <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>

    Thymeleaf模板中引用

    pom文件中导入thymeleaf模板对shiro标签支持的依赖

    1. <dependency>
    2. <groupId>com.github.theborakompanioni</groupId>
    3. <artifactId>thymeleaf-extras-shiro</artifactId>
    4. <version>2.1.0</version>
    5. </dependency>

    在shiroconfig中配置shiro的方言支持

    1. @Configuration
    2. public class ShiroConfig {
    3. @Bean
    4. public ShiroDialect getShiroDialect(){
    5. return new ShiroDialect();
    6. }

    Thymeleaf模板中引入shiro的命名空间

    1. <html xmlns:th="http://www.thymeleaf.org"
    2. xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    3. ...
    4. </html>

    常用标签

    1. html>
    2. <html xmlns:th="http://www.thymeleaf.org"
    3. xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    4. <head>
    5. <meta charset="UTF-8">
    6. <title>Titletitle>
    7. head>
    8. <body>
    9. index
    10. <hr/>
    11. <shiro:guest>
    12. 游客:<a href="login.html">登录a>
    13. shiro:guest>
    14. <shiro:user>
    15. 用户【<shiro:principal/>】欢迎您
    16. 当前用户为<shiro:hasRole name="admin">超级管理员shiro:hasRole>
    17. 订单管理
    18. <shiro:hasPermission name="sys:x:save"><a href="#">添加订单a>shiro:hasPermission>
    19. shiro:user>
    20. body>
    21. html>

  • 相关阅读:
    Spring-RabbitMQ 消费者消息确认案例实践
    docker系列(2) - 常用命令篇
    [激光器原理与应用-14]:激光器企业组织内部结构解剖
    基于Unet的环路滤波
    关于 Rancher 与防火墙 firewalld 的一些注意事项
    SparkCore系列-5、RDD 函数练习
    计算机视觉全系列实战教程:(八)图像变换-点运算、灰度变换、直方图变换
    Java进阶(九)日志框架
    【8】Docker中部署Redis
    CICD—Linux下Jenkins+Gitlab 自动化打包安卓-APK
  • 原文地址:https://blog.csdn.net/m0_61107050/article/details/126849356