导读


本篇简单介绍Shiro的使用。

技术


  • SpringBoot
  • Shiro
  • Mybatis
  • Generator
  • MySQL

    1、创建表

    创建用户表,角色表,权限表,用户角色表,角色权限表五张表 ```sql drop table if exists permission;

drop table if exists role;

drop table if exists role_to_permission;

drop table if exists user_to_role;

/==============================================================/ / Table: permission / /==============================================================/ create table permission ( id int not null auto_increment, name varchar(50) comment ‘权限名称’, url varchar(100) not null comment ‘请求路径’, permission varchar(150) not null comment ‘角色编码’, status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int default 0, remark varchar(50) default ‘’, primary key (id) );

alter table permission comment ‘权限表’;

/==============================================================/ / Table: role / /==============================================================/ create table role ( id int not null auto_increment, role_name varchar(100) not null comment ‘角色名称’, role_code varchar(150) not null comment ‘角色编码’, status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int, remark varchar(50) default ‘’, primary key (id) );

alter table role comment ‘角色表’;

/==============================================================/ / Table: role_to_permission / /==============================================================/ create table role_to_permission ( id int not null auto_increment, permission_id varchar(100) not null comment ‘权限id’, role_id varchar(150) not null comment ‘角色id’, status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int, primary key (id) );

alter table role_to_permission comment ‘角色与权限表’;

/==============================================================/ / Table: user_to_role / /==============================================================/ create table user_to_role ( id int not null auto_increment, user_id varchar(100) not null, role_id varchar(150) not null, status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int, primary key (id) );

alter table user_to_role comment ‘用户角色表’;

create table user ( id int not null auto_increment, user_id varchar(100) not null, username varchar(150) not null, password varchar(150) not null, real_name varchar(150), address varchar(150), status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int, primary key (id) );

alter table user comment ‘用户表’;

  1. SQL数据:
  2. ```sql
  3. DELIMITER ;
  4. delete from sys_user;
  5. delete from sys_role;
  6. delete from sys_resource;
  7. delete from sys_organization;
  8. insert into sys_user values(1,1,'admin','d3c59d25033dbf980d29554025c23a75','8d78869f470951332959580424d4bf4f', '1', false);
  9. insert into sys_organization values(1, '总公司', 0, '0/', true);
  10. insert into sys_organization values(2, '分公司1', 1, '0/1/', true);
  11. insert into sys_organization values(3, '分公司2', 1, '0/1/', true);
  12. insert into sys_organization values(4, '分公司11', 2, '0/1/2/', true);
  13. insert into sys_resource values(1, '资源', 'menu', '', 0, '0/', '', true);
  14. insert into sys_resource values(11, '组织机构管理', 'menu', '/organization', 1, '0/1/', 'organization:*', true);
  15. insert into sys_resource values(12, '组织机构新增', 'button', '', 11, '0/1/11/', 'organization:create', true);
  16. insert into sys_resource values(13, '组织机构修改', 'button', '', 11, '0/1/11/', 'organization:update', true);
  17. insert into sys_resource values(14, '组织机构删除', 'button', '', 11, '0/1/11/', 'organization:delete', true);
  18. insert into sys_resource values(15, '组织机构查看', 'button', '', 11, '0/1/11/', 'organization:view', true);
  19. insert into sys_resource values(21, '用户管理', 'menu', '/user', 1, '0/1/', 'user:*', true);
  20. insert into sys_resource values(22, '用户新增', 'button', '', 21, '0/1/21/', 'user:create', true);
  21. insert into sys_resource values(23, '用户修改', 'button', '', 21, '0/1/21/', 'user:update', true);
  22. insert into sys_resource values(24, '用户删除', 'button', '', 21, '0/1/21/', 'user:delete', true);
  23. insert into sys_resource values(25, '用户查看', 'button', '', 21, '0/1/21/', 'user:view', true);
  24. insert into sys_resource values(31, '资源管理', 'menu', '/resource', 1, '0/1/', 'resource:*', true);
  25. insert into sys_resource values(32, '资源新增', 'button', '', 31, '0/1/31/', 'resource:create', true);
  26. insert into sys_resource values(33, '资源修改', 'button', '', 31, '0/1/31/', 'resource:update', true);
  27. insert into sys_resource values(34, '资源删除', 'button', '', 31, '0/1/31/', 'resource:delete', true);
  28. insert into sys_resource values(35, '资源查看', 'button', '', 31, '0/1/31/', 'resource:view', true);
  29. insert into sys_resource values(41, '角色管理', 'menu', '/role', 1, '0/1/', 'role:*', true);
  30. insert into sys_resource values(42, '角色新增', 'button', '', 41, '0/1/41/', 'role:create', true);
  31. insert into sys_resource values(43, '角色修改', 'button', '', 41, '0/1/41/', 'role:update', true);
  32. insert into sys_resource values(44, '角色删除', 'button', '', 41, '0/1/41/', 'role:delete', true);
  33. insert into sys_resource values(45, '角色查看', 'button', '', 41, '0/1/41/', 'role:view', true);
  34. insert into sys_resource values(51, '会话管理', 'menu', '/sessions', 1, '0/1/', 'session:*', true);
  35. insert into sys_role values(1, 'admin', '超级管理员', '11,21,31,41,51', true);

2、使用mybatis的逆向生成文件

1、引入依赖

  1. <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
  2. <dependency>
  3. <groupId>org.mybatis.generator</groupId>
  4. <artifactId>mybatis-generator-core</artifactId>
  5. <version>1.3.5</version>
  6. </dependency>
  1. <plugins>
  2. <!-- mybatis generator 自动生成代码插件 -->
  3. <plugin>
  4. <groupId>org.mybatis.generator</groupId>
  5. <artifactId>mybatis-generator-maven-plugin</artifactId>
  6. <version>1.3.5</version>
  7. <configuration>
  8. <!-- 配置文件路径 -->
  9. <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
  10. <overwrite>true</overwrite>
  11. <verbose>true</verbose>
  12. </configuration>
  13. </plugin>
  14. </plugins>

2、generatorConfig.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <classPathEntry
  7. location="G:\apache-maven-3.6.0\package\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar"/>
  8. <context id="DB2Tables" targetRuntime="MyBatis3">
  9. <commentGenerator>
  10. <property name="suppressDate" value="true"/>
  11. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  12. <property name="suppressAllComments" value="true"/>
  13. </commentGenerator>
  14. <!--数据库链接URL,用户名、密码 -->
  15. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  16. connectionURL="jdbc:mysql://localhost:3306/junk-classes?serverTimezone=UTC"
  17. userId="root" password="123456">
  18. </jdbcConnection>
  19. <javaTypeResolver>
  20. <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
  21. <property name="forceBigDecimals" value="false"/>
  22. </javaTypeResolver>
  23. <!-- 生成实体类的包名和位置 ,targetPackage指的是包名,targetProject值得是路径位置-->
  24. <javaModelGenerator targetPackage="com.ck.junk.model" targetProject="src/main/java">
  25. <property name="enableSubPackages" value="true"/>
  26. <property name="trimStrings" value="true"/>
  27. </javaModelGenerator>
  28. <!-- 生成映射文件的包名和位置-->
  29. <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
  30. <property name="enableSubPackages" value="true"/>
  31. </sqlMapGenerator>
  32. <!-- 生成DAO的包名和位置-->
  33. <javaClientGenerator type="XMLMAPPER" targetPackage="com.ck.junk.dao" targetProject="src/main/java">
  34. <property name="enableSubPackages" value="true"/>
  35. </javaClientGenerator>
  36. <!--生成对应表及类名-->
  37. <!--</table>-->
  38. <!--<table tableName="junk_infos" domainObjectName="JunkInfosModel" enableCountByExample="false"-->
  39. <!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
  40. <!--selectByExampleQueryId="false">-->
  41. <!--<property name="my.isgen.usekeys" value="true"/>-->
  42. <!--<property name="useActualColumnNames" value="false"/>-->
  43. <!--<generatedKey column="id" sqlStatement="JDBC"/>-->
  44. <!--</table>-->
  45. <!--<table tableName="junk_oper_log" domainObjectName="JunkOperLogModel" enableCountByExample="false"-->
  46. <!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
  47. <!--selectByExampleQueryId="false">-->
  48. <!--<property name="my.isgen.usekeys" value="true"/>-->
  49. <!--<property name="useActualColumnNames" value="false"/>-->
  50. <!--<generatedKey column="id" sqlStatement="JDBC"/>-->
  51. <!--</table>-->
  52. <!--<table tableName="junk_types" domainObjectName="JunkTypesModel" enableCountByExample="false"-->
  53. <!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
  54. <!--selectByExampleQueryId="false">-->
  55. <!--<property name="my.isgen.usekeys" value="true"/>-->
  56. <!--<property name="useActualColumnNames" value="false"/>-->
  57. <!--<generatedKey column="id" sqlStatement="JDBC"/>-->
  58. <!--</table>-->
  59. <table tableName="junk_leave_meg" domainObjectName="JunkLeaveMegModel" enableCountByExample="false"
  60. enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
  61. selectByExampleQueryId="false">
  62. <property name="my.isgen.usekeys" value="true"/>
  63. <property name="useActualColumnNames" value="false"/>
  64. <generatedKey column="id" sqlStatement="JDBC"/>
  65. </table>
  66. </context>
  67. </generatorConfiguration>

3、执行 mybatis-generator:generate 命令

具体截图如下:
image.png

4、运行结果

image.png
image.png
image.png
虽然出现几个问题,but最后成功了,以上是生成结果。

3、问题详解

问题描述1:

出现http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd报红

原因:缺少相关依赖或者配置

解决办法:

1、引入 mybatis-generator-core 依赖:

  1. <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
  2. <dependency>
  3. <groupId>org.mybatis.generator</groupId>
  4. <artifactId>mybatis-generator-core</artifactId>
  5. <version>1.3.5</version>
  6. </dependency>

2、在pom.xml文件添加:

  1. <plugins>
  2. <!-- mybatis generator 自动生成代码插件 -->
  3. <plugin>
  4. <groupId>org.mybatis.generator</groupId>
  5. <artifactId>mybatis-generator-maven-plugin</artifactId>
  6. <version>1.3.5</version>
  7. <configuration>
  8. <!-- 配置文件路径 -->
  9. <configurationFile>${basedir}/src/main/resources/${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
  10. <overwrite>true</overwrite>
  11. <verbose>true</verbose>
  12. </configuration>
  13. </plugin>
  14. </plugins>

或者在setting—>Schemas and DTDs中 添加相应的URL
image.png
问题得以解决。

问题描述2:

原因:xml文件内容重复

如下所示,生成的xml文件,内容重复。
image.png

解决办法:删除就可以了

image.png
或者将其属性设置为false

  1. <commentGenerator>
  2. <!--&lt;!&ndash; 是否去除自动生成的注释 true:是 : false:否 &ndash;&gt;-->
  3. <property name="suppressAllComments" value="false"/>
  4. <!--&lt;!&ndash; 是否去除所有自动生成的文件的时间戳,默认为false &ndash;&gt;-->
  5. <property name="suppressDate" value="true"/>
  6. </commentGenerator>

image.png
重新生成xml文件就没事了。

4、Shiro权限验证

问题描述2:

执行 mybatis-generator:generate 命令出现以下错误:

原因:MySQL包放错位置

  1. [INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ shiro-demo ---
  2. [INFO] Connecting to the Database
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] BUILD FAILURE
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] Total time: 1.094 s
  7. [INFO] Finished at: 2019-05-21T13:04:22+08:00
  8. [INFO] Final Memory: 13M/232M
  9. [INFO] ------------------------------------------------------------------------
  10. [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project shiro-demo: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver -> [Help 1]
  11. [ERROR]
  12. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  13. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  14. [ERROR]
  15. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  16. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

解决方案:

将MySQL的依赖放到这个下面即可,或者mysql的版本不对,请自行查看。
image.png

  1. <!--mybatis.generator逆向生成Sql依赖-->
  2. <plugin>
  3. <groupId>org.mybatis.generator</groupId>
  4. <artifactId>mybatis-generator-maven-plugin</artifactId>
  5. <version>1.3.5</version>
  6. <configuration>
  7. <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
  8. <overwrite>true</overwrite>
  9. <verbose>true</verbose>
  10. </configuration>
  11. <dependencies>
  12. <dependency>
  13. <groupId>mysql</groupId>
  14. <artifactId>mysql-connector-java</artifactId>
  15. <version>5.1.33</version>
  16. </dependency>
  17. </dependencies>
  18. </plugin>

参考