您的位置:首页 > 其它

学习shiro——简单的hello world实现

2017-06-12 00:00 323 查看
摘要: 跟着视频学习shiro,视频地址:http://www.java1234.com/a/yuanchuang/shiro/2015/1102/5171.html,学习并记录

1.pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>com.test</groupId>
<artifactId>myShiro</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>

</project>

2.配置文件:shiro.ini

[users]
jack1234=1234
hello =123

3.配置文件log4j.properties

# LOG4J配置
log4j.rootLogger=debug,stdout

### 把日志信息输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

4.HelloWorld.java文件

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
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;

/**
* Created by guanguan on 17/6/12.
*/
public class HelloWorld {
public static void main(String[] args) {
//读配置文件,初始化SecurityManager
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.properties");

//获取SecurityManager实例
SecurityManager securityManager = factory.getInstance();

//把SecurityManager实例绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);

//获取当前登录的用户
Subject currentUser = SecurityUtils.getSubject();

//构建token令牌。用户名/密码
UsernamePasswordToken token = new UsernamePasswordToken("jack1234", "1234");
try {

//身份认证
currentUser.login(token);
System.out.println("登录成功");
} catch (AuthenticationException e) {
e.printStackTrace();
System.out.println("登录失败");
}

}
}

5.运行文件

2017-06-12 10:17:17,805 DEBUG [org.apache.shiro.io.ResourceUtils] - Opening resource from class path [shiro.properties]
2017-06-12 10:17:17,830 DEBUG [org.apache.shiro.config.Ini] - Parsing [users]
2017-06-12 10:17:17,832 DEBUG [org.apache.shiro.config.IniFactorySupport] - Creating instance from Ini [sections=users]
2017-06-12 10:17:17,994 DEBUG [org.apache.shiro.realm.text.IniRealm] - Discovered the [users] section.  Processing...
2017-06-12 10:17:18,025 DEBUG [org.apache.shiro.realm.AuthenticatingRealm] - Looked up AuthenticationInfo [jack1234] from doGetAuthenticationInfo
2017-06-12 10:17:18,025 DEBUG [org.apache.shiro.realm.AuthenticatingRealm] - AuthenticationInfo caching is disabled for info [jack1234].  Submitted token: [org.apache.shiro.authc.UsernamePasswordToken - jack1234, rememberMe=false].
2017-06-12 10:17:18,026 DEBUG [org.apache.shiro.authc.credential.SimpleCredentialsMatcher] - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]
2017-06-12 10:17:18,026 DEBUG [org.apache.shiro.authc.credential.SimpleCredentialsMatcher] - Both credentials arguments can be easily converted to byte arrays.  Performing array equals comparison
2017-06-12 10:17:18,027 DEBUG [org.apache.shiro.authc.AbstractAuthenticator] - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - jack1234, rememberMe=false].  Returned account [jack1234]
2017-06-12 10:17:18,028 DEBUG [org.apache.shiro.subject.support.DefaultSubjectContext] - No SecurityManager available in subject context map.  Falling back to SecurityUtils.getSecurityManager() lookup.
2017-06-12 10:17:18,032 DEBUG [org.apache.shiro.subject.support.DefaultSubjectContext] - No SecurityManager available in subject context map.  Falling back to SecurityUtils.getSecurityManager() lookup.
2017-06-12 10:17:18,037 DEBUG [org.apache.shiro.session.mgt.AbstractValidatingSessionManager] - No sessionValidationScheduler set.  Attempting to create default instance.
2017-06-12 10:17:18,038 INFO [org.apache.shiro.session.mgt.AbstractValidatingSessionManager] - Enabling session validation scheduler...
2017-06-12 10:17:18,054 DEBUG [org.apache.shiro.session.mgt.DefaultSessionManager] - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]
登录成功

Process finished with exit code 0

以上为shiro的基本简单案例。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shiro