标签:shiro https 数据 关注 数据信息 应该 简介 lse api
<!--首先配置servlet开发包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--配置Shiro相关开发包-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.0</version>
</dependency>
<!--配置日志相关开发包,在导入log4j.properties-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.12</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
[users]
admin=hello
mldn=java
定义了2个用户
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
public class TestLoginDemo {
public static void main(String[] args) {
// 取得Factory接口对象,主要的目的是通过配置文件加载文件之中的信息,这些信息暂时不能够成为认证信息
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 取得里面所保存的所有的认证数据信息
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
// 利用一个专门的认证操作的处理类,实现认证处理的具体的实现
SecurityUtils.setSecurityManager(securityManager);
// 获取进行用户名和密码认证的接口对象
Subject subject = SecurityUtils.getSubject() ;
// 定义了一个Token,里面保存要登录的用户名和密码信息
UsernamePasswordToken token = new UsernamePasswordToken("admin","hello") ;
// 实现用户登录处理
subject.login(token);
System.out.println(subject.getPrincipal()); // 取得用户名
}
}
- 异常
1. 用户名正确,密码错误
Exception in thread "main" org.apache.shiro.authc.IncorrectCredentialsException:
Submitted credentials for token
[org.apache.shiro.authc.UsernamePasswordToken - admin, rememberMe=false] did not match the expected credentials.
2. 用户名错误,密码正确
Exception in thread "main" org.apache.shiro.authc.UnknownAccountException:
Realm [org.apache.shiro.realm.text.IniRealm@4696954c] was unable to find account data for the submitted AuthenticationToken
[org.apache.shiro.authc.UsernamePasswordToken - admina, rememberMe=false].
```
标签:shiro https 数据 关注 数据信息 应该 简介 lse api
原文地址:https://www.cnblogs.com/liusuixing/p/10787106.html