Spring集成
添加依赖
4.2.4.RELEASE
org.projectlombok
lombok
1.16.16
javax.servlet
servlet-api
2.5
provided
org.springframework
spring-core
${org.springframework.version}
org.springframework
spring-context
${org.springframework.version}
org.springframework
spring-context-support
${org.springframework.version}
org.springframework
spring-expression
${org.springframework.version}
org.springframework
spring-web
${org.springframework.version}
org.springframework
spring-aop
${org.springframework.version}
org.springframework
spring-webmvc
${org.springframework.version}
org.springframework
spring-jdbc
${org.springframework.version}
org.springframework
spring-tx
${org.springframework.version}
mysql
mysql-connector-java
5.1.21
com.alibaba
druid
1.0.14
org.aspectj
aspectjrt
1.7.4
org.aspectj
aspectjweaver
1.7.4
cglib
cglib
3.1
org.slf4j
slf4j-api
1.7.25
org.slf4j
slf4j-log4j12
1.7.25
log4j
log4j
1.2.17
jstl
jstl
1.2
taglibs
standard
1.1.2
commons-logging
commons-logging
1.1.3
commons-collections
commons-collections
3.2.1
org.apache.shiro
shiro-core
1.2.2
org.apache.shiro
shiro-web
1.2.2
net.sf.ehcache
ehcache-core
2.6.8
org.apache.shiro
shiro-ehcache
1.2.2
org.apache.shiro
shiro-quartz
1.2.2
org.apache.shiro
shiro-spring
1.2.2
创建数据库 shiro
/*
Navicat Premium Data Transfer
Source Server : icanci
Source Server Type : MySQL
Source Server Version : 50540
Source Host : localhost:3306
Source Schema : shiro
Target Server Type : MySQL
Target Server Version : 50540
File Encoding : 65001
Date: 26/02/2020 12:15:10
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for permission
-- ----------------------------
DROP TABLE IF EXISTS `permission`;
CREATE TABLE `permission` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`resource` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`sn` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Table structure for role_permission
-- ----------------------------
DROP TABLE IF EXISTS `role_permission`;
CREATE TABLE `role_permission` (
`role_id` bigint(20) NOT NULL,
`permission_id` bigint(20) NOT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Table structure for user_role
-- ----------------------------
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
`user_id` bigint(20) NOT NULL,
`role_id` bigint(20) NOT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///shiro?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=ok
log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.cn.wolfcode.shiro=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
spring.xml
mvc.xml
spring-shiro.xml
/logout=logout
/**=authc
redirect:/nopermission.jsp
shiro-ehcache.xml
web.xml
Archetype Created Web Application
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:mvc.xml
1
SpringMVC
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*
shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
true
shiroFilter
/*
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
Title
欢迎【 】光临 注销 重新加载权限
员工列表
部门列表
shiro静态授权
步骤:
1:分析权限控制选择
1>编程式,缺点:必须进入请求方法中才能判断是否有权限,放弃
2>jsp标签方式, 缺点:虽然在页面上没有显式请求按钮,但是可以通过浏览器地址栏中输入请求访问, 放弃
3>注解方式:优点,可以在请求进入方法之前进行权限控制。 推荐2:在需要权限控制的方法上面贴上权限标签:(此处仅仅讨论居于权限的表达式空:permission)
3:在spring-shiro.xml文件中配置权限注解支持, 让权限注解生效@RequiresPermissions
4:添加用户权限(静态操作方式:模拟查询数据库)
shiro权限-角色-用户关系分析
加载权限表达式
步骤:
1:在所有的controller中的需要进行权限控制方法里面贴上2给标签:
@RequiresPermissions表示权限表达式, PermissionName自定义标签, 表示权限表达式名称:
/**
* 标记权限的名称
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PermissionName {
String value();
}
2:在PermissionController编辑reload方法, 加载权限
@Controller
public class PermissionController {
//请求映射处理映射器
//springmvc在启动时候将所有贴有请求映射标签:RequestMapper 方法收集起来封装到该对象中
@Autowired
private RequestMappingHandlerMapping rmhm;
@Autowired
private IPermissionDAO permissionDAO;
@RequestMapping("/reload")
public String reload() throws Exception{
//将系统中所有权限表达式加载进入数据库
//0:从数据库中查询出所有权限表达式,然后对比,如果已经存在了,跳过,不存在添加
List resourcesList = permissionDAO.getAllResources();
//1:获取controller中所有带有@RequestMapper标签的方法
Map handlerMethods = rmhm.getHandlerMethods();
Collection methods = handlerMethods.values();
for (HandlerMethod method : methods) {
//2:遍历所有方法,判断当前方法是否贴有@RequiresPermissions权限控制标签
RequiresPermissions anno = method.getMethodAnnotation(RequiresPermissions.class);
if(anno != null){
//3:如果有,解析得到权限表达式,封装成Permission对象保存到Permission表中
//权限表达式
String resource = anno.value()[0];
//去除重复的
if(resourcesList.contains(resource)){
continue;
}
Permission p = new Permission();
p.setResource(resource);
//设置权限名称
p.setName(method.getMethodAnnotation(PermissionName.class).value());
//保存
permissionDAO.save(p);
}
}
return "main";
}
}
用户-角色-权限数据初始化(权限的分配)
数据库方式授权
步骤:
1: 需要执行上述的数据权限分配
2:在自定义的UserRealm添加2个属性:IRoleDAO IPermissionDAO
注意:同时修改spring-shiro.xml文件中UserRealm定义,注入dao实现类
3:重写改动原先授权操作,改为使用数据库的方式授权
缓存管理
步骤
1:添加依赖jar包: 之前的pom已经添加
2:在spring-shiro.xml文件中配置ehcache一些相关配置
3:添加缓存配置文件shiro-ehcache.xml
4:在shiro中使用缓存操作
5:测试:
在UserRealm的授权操作:doGetAuthorizationInfo 点一个断点, 然后多次请求需要权限控制的方法, 如果debug仅仅进入一次,表示缓存操作成功
6:清空缓存
在UserRealm添加一个清空缓存的方法
添加凭证匹配器
细节完善
只是做了一些记录 具体的操作还不是很熟悉 不久会有完成的配置和说明过程