官网参考信息:
http://shiro.apache.org/spring.html
1-创建shiro所需的角色、用户、权限表
首先设计三张表
t_role 角色表
t_user 用户表
t_permission 权限表
2-IDEA创建一个maven的webapp项目
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.company.shiro</groupId> <artifactId>ssm_shiro</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>ssm_shiro Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!--servlet及jsp api 运行时都由TOMCAT应用服务器提供, maven配置是开发时使用,scope设置为provided,表明该包只在编译和测试的时候有效,运行时不与tomcat的已有jar包发生冲突 还有一种方法就是在buildpath中增加tomcat的配置 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <!--添加JSTL支持 javax.servlet/jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--添加spring支持 spring-test+junit实现对Spring ioc容器的测试 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!--添加MyBatis支持--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!--添加Spring整合MyBatis的支持--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--添加SLF4J+LOG4J Binding日志支持 可以设定scope为compile --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!--添加mysql数据库支持 scope-runtime 表示只在测试和运行时有效,不参与编译过程--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> <scope>runtime</scope> </dependency> <!--添加shiro支持 shiro-spring依赖shiro-web,shiro-web依赖shiro-core --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency> <!--单元测试 junit只在测试时有效,scope=test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>ssm_shiro</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF8</encoding> </configuration> </plugin> </plugins> </build> </project> |
Url 匹配方式
? 匹配一个字符 /admin? 可以匹配/admin1, /admin2 但是不能匹配/admin12, /admin
* 匹配零个或者一个或者多个字符 /admin* 可以匹配 /admin ,/admin1, /admin12 但是不能匹配路径/admin/abc
** 匹配零个或者多个路径 /admin/** 可以匹配/admin, /admin/a ,/admin/a/b
anon所有url都可以匿名访问
自定义Realm域
除非系统非常简单,可以使用shrio.ini文件,正常,我们所需要的信息都存储在数据库中,需要编写自定义Realm实现对角色、用户、权限的匹配。