shiro之helloword

pom.xmljava

<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
apache

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.inispa

[users]
zs=123456
相關文章
相關標籤/搜索