shiro之helloword

pom.xml

<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-core</artifactId>
			<version>1.2.4</version>
		</dependency>
		
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.16</version>
		</dependency>

HelloWorld

package shiro01.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 HelloWorld {
	
	public static void main(String[] args) {
		//读取配置文件,初始化SecurityManager工厂
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//获取SecurityManager实例
		SecurityManager securityManager = factory.getInstance();
		//把SecurityManager绑定到SecurityUtils
		SecurityUtils.setSecurityManager(securityManager);
		//得到当前执行的用户
		Subject subject = SecurityUtils.getSubject();
		//创建token令牌  用户名/密码形式
		UsernamePasswordToken token = new UsernamePasswordToken("zs", "123456");
		
		try {
			//用户登录
			subject.login(token);
			System.out.println("登录成功");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("登录失败");
		}
		//用户注销
		subject.logout();
	}

}

shiro.ini

[users]
zs=123456


你可能感兴趣的:(shiro之helloword)