apache shiro版本查看_深入学习SpringBoot(四):springboot整合shiro

shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/ 它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和shiro整合的功能!接下来就用springboot结合springmvc,mybatis,整合shiro完成对于用户登录的判定和权限的验证.

1.准备数据库表结构

这里主要涉及到五张表:用户表,角色表(用户所拥有的角色),权限表(角色所涉及到的权限),用户-角色表(用户和角色是多对多的),角色-权限表(角色和权限是多对多的).表结构建立的sql语句如下:

CREATE TABLE `module` ( `mid` int(11) NOT NULL AUTO_INCREMENT, `mname` varchar(255) DEFAULT NULL, PRIMARY KEY (`mid`)) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;-- ------------------------------ Records of module-- ----------------------------INSERT INTO `module` VALUES ('1', 'add');INSERT INTO `module` VALUES ('2', 'delete');INSERT INTO `module` VALUES ('3', 'query');INSERT INTO `module` VALUES ('4', 'update');-- ------------------------------ Table structure for module_role-- ----------------------------DROP TABLE IF EXISTS `module_role`;CREATE TABLE `module_role` ( `rid` int(11) DEFAULT NULL, `mid` int(11) DEFAULT NULL, KEY `rid` (`rid`), KEY `mid` (`mid`), CONSTRAINT `mid` FOREIGN KEY (`mid`) REFERENCES `module` (`mid`), CONSTRAINT `rid` FOREIGN KEY (`rid`) REFERENCES `role` (`rid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of module_role-- ----------------------------INSERT INTO `module_role` VALUES ('1', '1');INSERT INTO `module_role` VALUES ('1', '2');INSERT INTO `module_role` VALUES ('1', '3');INSERT INTO `module_role` VALUES ('1', '4');INSERT INTO `module_role` VALUES ('2', '1');INSERT INTO `module_role` VALUES ('2', '3');-- ------------------------------ Table structure for role-- ----------------------------DROP TABLE IF EXISTS `role`;CREATE TABLE `role` ( `rid` int(11) NOT NULL AUTO_INCREMENT, `rname` varchar(255) DEFAULT NULL, PRIMARY KEY (`rid`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ------------------------------ Records of role-- ----------------------------INSERT INTO `role` VALUES ('1', 'admin');INSERT INTO `role` VALUES ('2', 'customer');-- ------------------------------ Table structure for user-- ----------------------------DROP TABLE IF EXISTS `user`;CREATE TABLE `user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`uid`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ------------------------------ Records of user-- ----------------------------INSERT INTO `user` VALUES ('1', 'hlhdidi', '123');INSERT INTO `user` VALUES ('2', 'xyycici', '1992');-- ------------------------------ Table structure for user_role-- ----------------------------DROP TABLE IF EXISTS `user_role`;CREATE TABLE `user_role` ( `uid` int(11) DEFAULT NULL, `rid` int(11) DEFAULT NULL, KEY `u_fk` (`uid`), KEY `r_fk` (`rid`), CONSTRAINT `r_fk` FOREIGN KEY (`rid`) REFERENCES `role` (`rid`), CONSTRAINT `u_fk` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of user_role-- ----------------------------INSERT INTO `user_role` VALUES ('1', '1');INSERT INTO `user_role` VALUES ('2', '2');

2.建立Maven工程,建立实体类,搭建mybatis开发环境

maven工程的基本目录如下:

4911225529324577a2f3f3ac0aa17bd8

为了方便,直接在父工程中,导入全部的依赖:

  org.springframework.boot spring-boot-starter-parent 1.4.0.RELEASE1.7UTF-8org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools truetrueorg.springframework.boot spring-boot-starter-test testmysql mysql-connector-java runtimecom.alibaba druid 1.0.18org.apache.commons commons-lang3 3.4org.apache.commons commons-pool2 org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1com.alibaba druid 1.0.18org.apache.commons commons-lang3 3.4org.apache.commons commons-pool2 org.apache.shiro  shiro-core  1.2.2org.apache.shiro shiro-spring 1.2.2org.apache.shiro shiro-ehcache 1.2.2org.springframework spring-context-support javax.servlet javax.servlet-api providedjavax.servlet jstl org.springframework.boot spring-boot-starter-tomcat providedorg.apache.tomcat.embed tomcat-embed-jasper providedorg.springframework.boot spring-boot-maven-plugin repackagetruespring-boot-shiro-daospring-boot-shiro-servicespring-boot-shiro-web

可以看出这里采用的是阿里巴巴的Druid数据库.在spring-boot-shiro-web下建立application.properties文件.它主要配置对于数据库信息和jsp的支持:

##tomcat##server.tomcat.uri-encoding=UTF-8##Druid##spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=rootspring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20spring.datasource.maxWait=60000spring.datasource.timeBetweenEvictionRunsMillis=60000spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=falsespring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20spring.datasource.filters=stat,wall,log4jspring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000spring.datasource.useGlobalDataSourceStat=true##jsp##spring.mvc.view.prefix=/jsp/spring.mvc.view.suffix=.jsp

在spring-boot-shiro-web下建立数据库连接池的配置类完成对于数据库连接池的配置:

/** * 数据库连接池&Mybatis配置类 * @author Administrator * */@Configurationpublic class DruidConfiguation { @Bean public ServletRegistrationBean statViewServle(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //白名单: servletRegistrationBean.addInitParameter("allow
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值