How does Shiro determine if a user is logged in?
Shiro can determine if a user is logged in by the following methods:
- Topic
- Can you confirm if the user is logged in?
- Topic
- retrieve the principal amount
- The security utilities.
- Retrieve the subject.
- Subject matter.
The sample code is shown below:
Subject currentUser = SecurityUtils.getSubject();
// 方法1
if (currentUser.isAuthenticated()) {
// 用户已登录
} else {
// 用户未登录
}
// 方法2
Object principal = currentUser.getPrincipal();
if (principal != null) {
// 用户已登录
} else {
// 用户未登录
}
// 方法3
Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isAuthenticated()) {
// 用户已登录
} else {
// 用户未登录
}
The above are common ways to determine if a user is logged in. Depending on the specific business needs and usage scenarios, you can choose the appropriate method to determine if the user is logged in.