How does Shiro determine if a user is logged in?

Shiro can determine if a user is logged in by the following methods:

  1. Topic
  2. Can you confirm if the user is logged in?
  3. Topic
  4. retrieve the principal amount
  5. The security utilities.
  6. Retrieve the subject.
  7. 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.

bannerAds