shiro 基本环境搭建(Basic environment construction of Shiro)

javase演示

第一步 导包
第二步:书写 shiro.ini 文件

[users]
zs=123
sxt=root
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFact ory; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.apache.shiro.mgt.SecurityManager;
public class TestA { public static void main(String[] args) {
//[1]解析 shiro.ini 文件
Factory<SecurityManager> factory =new IniSecurityManagerFactory("classpath:shiro.ini "); 
//[2]通过 SecurityManager 工厂获得 SecurityManager 实例
SecurityManager securityManager = factory.getInstance(); 
//[3]把 SecurityManager 对象设置到运行环境中 SecurityUtils.setSecurityManager(securityManag er);
//[4]通过 SecurityUtils 获得主体
subject Subject subject = SecurityUtils.getSubject();
//[5]书写自己输入的账号和密码---相当于用户自 己输入的账号和密码 //我们拿着自己书写用户名密码去和 shiro.ini 文 件中的账号密码比较
UsernamePasswordToken token =new UsernamePasswordToken("sxt","root");
//[6]进行身份的验证
subject.login(token);
//[7]通过方法判断是否登录成功
if(subject.isAuthenticated()){
	System.out.println("登录成功"); }else { System.out.println("登录失败");
}
}
}
————————

Javase demo

First step guide package
Step 2: write Shiro INI file

[users]
zs=123
sxt=root
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFact ory; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.apache.shiro.mgt.SecurityManager;
public class TestA { public static void main(String[] args) {
//[1]解析 shiro.ini 文件
Factory<SecurityManager> factory =new IniSecurityManagerFactory("classpath:shiro.ini "); 
//[2]通过 SecurityManager 工厂获得 SecurityManager 实例
SecurityManager securityManager = factory.getInstance(); 
//[3]把 SecurityManager 对象设置到运行环境中 SecurityUtils.setSecurityManager(securityManag er);
//[4]通过 SecurityUtils 获得主体
subject Subject subject = SecurityUtils.getSubject();
//[5]书写自己输入的账号和密码---相当于用户自 己输入的账号和密码 //我们拿着自己书写用户名密码去和 shiro.ini 文 件中的账号密码比较
UsernamePasswordToken token =new UsernamePasswordToken("sxt","root");
//[6]进行身份的验证
subject.login(token);
//[7]通过方法判断是否登录成功
if(subject.isAuthenticated()){
	System.out.println("登录成功"); }else { System.out.println("登录失败");
}
}
}