首先我们先看一下数据库
再看看数据库的测试数据
在我们创建好的maven项目中看一下目录结构
在pom.xml引入
<dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency>
编辑jdbc_ream.ini配置文件
[main]
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/geyao?serverTimezone=GMT%2B8
dataSource.user=root
dataSource.password=123
jdbcRealm.dataSource=$dataSource
securityManager.realms=$jdbcRealm
shiroDemo测试文件
package com.hp.hello;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;public class ShiroDemo {public static void main(String[] args) {//读取配置文件 初始化SecurityManager工厂IniSecurityManagerFactoryFactory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:jdbc_ream.ini");//获得SecurityManager实例SecurityManager instance = factory.getInstance();//把SecurityManager的实例绑定到SecurityUtils上面SecurityUtils.setSecurityManager(instance);//获取当前执行的用户Subject subject = SecurityUtils.getSubject();//创建token 令牌 用户名/密码UsernamePasswordToken token=new UsernamePasswordToken("zs", "123");try{//登录/身份认证subject.login(token);System.out.println("身份认证成功...");}catch(Exception e){e.printStackTrace();System.out.println("身份认证失败...");}//退出subject.logout();}
}
运行结果