版权声明:本文为博主原创文章,版权归原作者小思所有,转载或者引用本文内容请注明来源及原作者,https://blog.csdn.net/zeal9s/ https://blog.csdn.net/zeal9s/article/details/83378966
完整项目代码:https://download.csdn.net/download/zeal9s/10744751
1.新建Maven项目Shiro_01,并设置项目输出文件显示
2.访问apache官网下载地址搜索到shiro,下载shiro的core、web、整合spring的依赖并且导入到项目中的pom.xml
pom.xml
<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>Shiro_01</groupId>
<artifactId>Shiro_01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Shiro_01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--引入servlet依赖:解决jsp页面报错 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
<scope>provided</scope>
</dependency>
<!--引入shiro的核心依赖 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<!--引入shiro的web依赖 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<!--引入shiro整合spring -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>Shiro_01</finalName>
</build>
</project>
3.项目的模块图
TestShiro.java
package com.zs.test;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
/**
*
* @ClassName: TestShiro
* @Description:完成用户的简单认证
* @author 小思
* @date 2018年10月24日 下午8:19:34
*
*/
public class TestShiro {
public static void main(String[] args) {
// 创建shiro的安全管理工厂
// classpath:shiro.ini(使用何处的配置文件作为安全管理认证文件)
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini");
//通过工厂实例化安全管理者的对象
SecurityManager securityManager=factory.getInstance();
//利用帮助类将安全管理者添加到当前的安全管理环境中
SecurityUtils.setSecurityManager(securityManager);
//获取安全管理的项目
Subject subject = SecurityUtils.getSubject();
//创建保存用户用户名和密码的令牌(用户名和密码是页面收集的,是用户在页面上填写的)
UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken("admin","123");
//保存认证结果
boolean authenticated=false;
try {
//将项目进行登录认证
subject.login(usernamePasswordToken);
//获取认证结果(用户输入的用户名和密码和shiro.ini文件中的用户名和密码匹配这返回为ture,为false则会报错)
authenticated=subject.isAuthenticated();
} catch (Exception e) {
}
//输出认证结果
System.out.println(authenticated);
}
}
MyRealm.java
package com.zs.shiro;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
/**
*
* @ClassName: MyRealm
* @Description:相当于把页面收集上的数据经过数据库判断之后返回给测试类,看验证是否通过
* @author 小思
* @date 2018年10月24日 下午9:13:29
*
*/
public class MyRealm extends AuthorizingRealm{
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
// TODO Auto-generated method stub
return null;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
//将父类的AuthenticationToken强转为UsernamePasswordToken(因为需要调用UsernamePasswordToken的方法)
UsernamePasswordToken usernamePasswordToken=(UsernamePasswordToken) arg0;
//获取用户名
String uname=usernamePasswordToken.getUsername();
//模拟通过数据库查询到用户名的密码
String dbPwd="123456";
//获取密码(可以不用获取密码,可以通过用户名查询密码)
String pwd=new String(usernamePasswordToken.getPassword());
//认证信息(参数填写的是用户输入的用户名和数据库查询的密码)
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(uname,dbPwd,getName());
System.out.println("用户输入的用户名和密码\t"+uname+"\t"+pwd);
return info;
}
}
shiro-realm.ini
#配置安全管理者的认证文件(使用何处的类进行认证)
myrealm=com.zs.shiro.MyRealm
securityManager.realm=$myrealm
4.运行结果
(1)认证失败(输入的用户名和密码和数据库通过输入的用户名查询的密码不一致)
(2)认证成功(输入的用户名和密码和数据库通过输入的用户名查询的密码一致)
说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~