标题Shiro入门以及Shiro与web整合
Shiro框架
- 什么是Shiro?
Apache shiro是一个强大,易用的java安全框架执行身份认证,授权,密码和会话管理。
Shiro框架的核心组件
主要核心组件:Subject, Security Manager Realms
1. Subject: 即“当前操作的用户”,但是,在shiro中,Subject这一概念不仅仅指人,也可以是第三方进程,后台账户或其他类似事物.
Subject代表了当前用户的安全操作,Security Manager则管理用户的安全操作。
2. Security Manager : shiro框架的核心,典型的Facade模式,shiro通过Security Manager来管理内部组件实例,并通过它来提供安全管理的各种服务。
3.Realms : Realms实质上是一个安全先关的DAO:它封装了数据源的连接细节,并在需要时把数据提供给shiro
当配置Shiro时,你必须至少指定一个Realm,用于认证和(或)授权。配置多个Realm是可以的,但是至少需要一个。
Shiro内置了可以连接大量安全数据源(又名目录)的Realm,如LDAP、关系数据库(JDBC)、类似INI的文本配置资源以及属性文件等
。如果缺省的Realm不能满足需求,你还可以插入代表自定义数据源的自己的Realm实现。
Shiro的入门案例
第一步: 官网下载相对应的jar包,在你的maven工程里面导入pom.xml导入相关依赖
官网地址:Shiro官网地址
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
<!-- 添加相关依赖 -->
<junit.version>4.12</junit.version>
<servlet.version>4.0.0</servlet.version>
<log4j2.version>2.9.1</log4j2.version>
<slf4j.version>1.7.7</slf4j.version>
<log4j2.disruptor.version>3.2.0</log4j2.disruptor.version>
<shiro.version>1.2.5</shiro.version>
</properties>
<dependencies>
<!-- shiro核心包 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<!-- 添加shiro web支持 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro.version}</version>
</dependency>
<!--**********junit**********-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--**********servlet**********-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<!-- ********************** 日志配置 ********************** -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--2) 用于与slf4j保持桥接-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--3) 核心log4j2jar包-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!--4) web工程需要包含log4j-web,非web工程不需要-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j2.version}</version>
<scope>runtime</scope>
</dependency>
<!--5) 需要使用log4j2的AsyncLogger需要包含disruptor-->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${log4j2.disruptor.version}</version>
</dependency>
</dependencies>
<resources>
<!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--解决mybatis-generator-maven-plugin运行时没有将jdbc.properites文件放入target文件夹的问题-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
<include>*.ini</include>
</includes>
</resource>
</resources>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 编码和编译和JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
注意 我这所用的jar包是接下来需要和web一起整合所需要的jar包,如果只是单独使用shiro只需要导入Shiro-core和相关日志信息的jar包即可
第二步:编写写一个Shiro入门的案例,创建一个ShiroDemo类,Shiro认证
public static void main(String[] args) {
/**
* 1 获取数据源
* 2 获取安全管理器
* 3 安全管理器交给SecurityUtils
* 4 securityUtils去获取Subject主体
* 5 将前段jsp的信息传给形成令牌
* 6 登录操作
*
*/
String username = "zs";
String password = "123";
IniSecurityManagerFactory iniSecurityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager instance = iniSecurityManagerFactory.getInstance();
SecurityUtils.setSecurityManager(instance);
Subject subject = SecurityUtils.getSubject();
if(!subject.isAuthenticated()){
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
token.setRememberMe(true);
try {
subject.login(token);
System.out.println("login Success");
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 错误:org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - zs, rememberMe=false] did not match the expected credentials
* 原因:密码错误
* 错误:org.apache.shiro.authc.UnknownAccountException: Realm [org.apache.shiro.realm.text.IniRealm@25b485ba] was unable to find account data for the submitted AuthenticationToken [org.apache.shiro.authc.UsernamePasswordToken - ss, rememberMe=false].
* 错误原因:用户名错误
*/
subject.logout();
System.out.println("logout Success");
}
Shiro与web整合
1. 导入相关pom.xml依赖在上述Shiro入门时已将所有jar导入
2. 导入resources导入realm(Shiro-web.ini)通俗的就是数据源
[main]
#定义身份认证失败后的请求url映射,loginUrl是身份认证过滤器中的一个属性
authc.loginUrl=/login
#定义角色认证失败后的请求url映射,unauthorizedUrl是角色认证过滤器中的一个属性
roles.unauthorizedUrl=/unauthorized.jsp
#定义权限认证失败后请求url映射,unauthorizedUrl是角色认证过滤器中的一个属性
perms.unauthorizedUrl=/unauthorized.jsp
[users]
zs=123,role1
ls=123,role2
ww=123,role3
zdm=123,admin
[roles]
role1=user:create
role2=user:create,user:update
role3=user:create,user:update,user:delete,user:view,user:load
admin=user:*
#定义请求的地址需要做什么验证
[urls]
#请求login的时候不需要权限,游客身份即可(anon)
/login.do=anon
#请求/user/updatePwd.jsp的时候,需要身份认证(authc)
/user/updatePwd.jsp=authc
#请求/admin的时候,需要角色认证,必须是拥有admin角色的用户才行
/admin/*.jsp=roles[admin]
#请求/teacher的时候,需要权限认证,必须是拥有user:create权限的角色的用户才行
/user/teacher.jsp=perms["user:update"]
3. 配置web.xml,tomcat启动的时候加载Shiro所有文件
<context-param>
<param-name>shiroConfigLocations</param-name>
<param-value>classpath:shiro-web.ini</param-value>
</context-param>
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4 编写一个Shiro与web整合的案例(模仿登录和退出)
创建写一个LoginServlet类并继承HTTPServlet
配置web.xml
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.ahong.web.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
启动tomca进行测试
当我们打开登录界面输入错误的密码,会把之前设置的值带给页面
输入正确登录信息进入主页面
当我们点击用户新增的时候,此时在shiro-web.ini中的zs这个用户是没有这个权限的
创建LogoutServlet类
配置web.xml
<servlet>
<servlet-name>logoutServlet</servlet-name>
<servlet-class>com.ahong.web.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
启动tomcat测试,点击退出系统就会回到登录界面
最后我们简述一个Shiro标签,此标签使用如果在你的Shiro-web.ini当用户登录的时候有此权限就会显示,反之不显示
shiro标签
<li>
<r:hasPermission name="user:create">
<a href="admin/addUser.jsp">用户新增</a>
</r:hasPermission>
</li>
学习官网:Shiro学习网站