先来一发shiro的helloWorld吧
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-beta0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
classpath下shiro.ini
[users]
ruge=ruge
root=root
classpach:log4j.properties
log4j.rootLogger=debug, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.com.liwei.shiro.dao=TRACE
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=INFO
HelloWorld
package com.ruge.shiro.helloword;
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;
/**
* @author 爱丽丝、如歌
* @Description: TODO
* @date 2017/12/29 12:48
*/
public class helloWord {
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("ruge","ruge");
try{
/**
* 身份认证
*/
subject.login(token);
System.out.println("登录成功");
}catch (AuthenticationException e){
e.printStackTrace();
System.out.println("登录失败");
}
/**
* 退出
*/
subject.logout();
}
}
控制台输出
D:\download\tool\jdk\install\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59383,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "D:\download\tool\jdk\install\jre\lib\charsets.jar;D:\download\tool\jdk\install\jre\lib\deploy.jar;D:\download\tool\jdk\install\jre\lib\ext\access-bridge-64.jar;D:\download\tool\jdk\install\jre\lib\ext\cldrdata.jar;D:\download\tool\jdk\install\jre\lib\ext\dnsns.jar;D:\download\tool\jdk\install\jre\lib\ext\jaccess.jar;D:\download\tool\jdk\install\jre\lib\ext\jfxrt.jar;D:\download\tool\jdk\install\jre\lib\ext\localedata.jar;D:\download\tool\jdk\install\jre\lib\ext\nashorn.jar;D:\download\tool\jdk\install\jre\lib\ext\sunec.jar;D:\download\tool\jdk\install\jre\lib\ext\sunjce_provider.jar;D:\download\tool\jdk\install\jre\lib\ext\sunmscapi.jar;D:\download\tool\jdk\install\jre\lib\ext\sunpkcs11.jar;D:\download\tool\jdk\install\jre\lib\ext\zipfs.jar;D:\download\tool\jdk\install\jre\lib\javaws.jar;D:\download\tool\jdk\install\jre\lib\jce.jar;D:\download\tool\jdk\install\jre\lib\jfr.jar;D:\download\tool\jdk\install\jre\lib\jfxswt.jar;D:\download\tool\jdk\install\jre\lib\jsse.jar;D:\download\tool\jdk\install\jre\lib\management-agent.jar;D:\download\tool\jdk\install\jre\lib\plugin.jar;D:\download\tool\jdk\install\jre\lib\resources.jar;D:\download\tool\jdk\install\jre\lib\rt.jar;D:\pro_idea\demo\shiro2\target\classes;D:\download\tool\maven\repository\org\apache\shiro\shiro-core\1.4.0\shiro-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-lang\1.4.0\shiro-lang-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-cache\1.4.0\shiro-cache-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-hash\1.4.0\shiro-crypto-hash-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-core\1.4.0\shiro-crypto-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-cipher\1.4.0\shiro-crypto-cipher-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-config-core\1.4.0\shiro-config-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-config-ogdl\1.4.0\shiro-config-ogdl-1.4.0.jar;D:\download\tool\maven\repository\commons-beanutils\commons-beanutils\1.9.3\commons-beanutils-1.9.3.jar;D:\download\tool\maven\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-event\1.4.0\shiro-event-1.4.0.jar;D:\download\tool\maven\repository\org\slf4j\slf4j-log4j12\1.8.0-beta0\slf4j-log4j12-1.8.0-beta0.jar;D:\download\tool\maven\repository\org\slf4j\slf4j-api\1.8.0-beta0\slf4j-api-1.8.0-beta0.jar;D:\download\tool\maven\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\download\tool\maven\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.1\lib\idea_rt.jar" com.ruge.shiro.helloword.helloWord
Connected to the target VM, address: '127.0.0.1:59383', transport: 'socket'
2018-01-02 09:50:59,045 [main] INFO [org.apache.shiro.session.mgt.AbstractValidatingSessionManager] - Enabling session validation scheduler...
Disconnected from the target VM, address: '127.0.0.1:59383', transport: 'socket'
登录成功
Process finished with exit code 0
登录失败日志
D:\download\tool\jdk\install\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59508,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "D:\download\tool\jdk\install\jre\lib\charsets.jar;D:\download\tool\jdk\install\jre\lib\deploy.jar;D:\download\tool\jdk\install\jre\lib\ext\access-bridge-64.jar;D:\download\tool\jdk\install\jre\lib\ext\cldrdata.jar;D:\download\tool\jdk\install\jre\lib\ext\dnsns.jar;D:\download\tool\jdk\install\jre\lib\ext\jaccess.jar;D:\download\tool\jdk\install\jre\lib\ext\jfxrt.jar;D:\download\tool\jdk\install\jre\lib\ext\localedata.jar;D:\download\tool\jdk\install\jre\lib\ext\nashorn.jar;D:\download\tool\jdk\install\jre\lib\ext\sunec.jar;D:\download\tool\jdk\install\jre\lib\ext\sunjce_provider.jar;D:\download\tool\jdk\install\jre\lib\ext\sunmscapi.jar;D:\download\tool\jdk\install\jre\lib\ext\sunpkcs11.jar;D:\download\tool\jdk\install\jre\lib\ext\zipfs.jar;D:\download\tool\jdk\install\jre\lib\javaws.jar;D:\download\tool\jdk\install\jre\lib\jce.jar;D:\download\tool\jdk\install\jre\lib\jfr.jar;D:\download\tool\jdk\install\jre\lib\jfxswt.jar;D:\download\tool\jdk\install\jre\lib\jsse.jar;D:\download\tool\jdk\install\jre\lib\management-agent.jar;D:\download\tool\jdk\install\jre\lib\plugin.jar;D:\download\tool\jdk\install\jre\lib\resources.jar;D:\download\tool\jdk\install\jre\lib\rt.jar;D:\pro_idea\demo\shiro2\target\classes;D:\download\tool\maven\repository\org\apache\shiro\shiro-core\1.4.0\shiro-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-lang\1.4.0\shiro-lang-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-cache\1.4.0\shiro-cache-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-hash\1.4.0\shiro-crypto-hash-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-core\1.4.0\shiro-crypto-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-cipher\1.4.0\shiro-crypto-cipher-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-config-core\1.4.0\shiro-config-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-config-ogdl\1.4.0\shiro-config-ogdl-1.4.0.jar;D:\download\tool\maven\repository\commons-beanutils\commons-beanutils\1.9.3\commons-beanutils-1.9.3.jar;D:\download\tool\maven\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-event\1.4.0\shiro-event-1.4.0.jar;D:\download\tool\maven\repository\org\slf4j\slf4j-log4j12\1.8.0-beta0\slf4j-log4j12-1.8.0-beta0.jar;D:\download\tool\maven\repository\org\slf4j\slf4j-api\1.8.0-beta0\slf4j-api-1.8.0-beta0.jar;D:\download\tool\maven\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\download\tool\maven\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.1\lib\idea_rt.jar" com.ruge.shiro.helloword.helloWord
Connected to the target VM, address: '127.0.0.1:59508', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:59508', transport: 'socket'
登录失败
org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - ruge, rememberMe=false] did not match the expected credentials.
at org.apache.shiro.realm.AuthenticatingRealm.assertCredentialsMatch(AuthenticatingRealm.java:603)
at org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:581)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)
at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:274)
at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260)
at com.ruge.shiro.helloword.helloWord.main(helloWord.java:43)
Process finished with exit code 0