33

学习 Shiro(一):初识 Shiro

 4 years ago
source link: https://www.tuicool.com/articles/V7JrYnM
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

更新至 Shiro 1.4.1 版本

Apache Shiro 是一个简单易用且功能强大的 Java 安全框架,提供了 认证(Authentication)授权(Authorization)加密Cryptography)会话管理 的功能。

对比另一个安全框架 Spring Security ,Shiro 更简洁也更通用。

nUbMv2a.png!web

Shiro 的核心特性包括:

  • 认证(Authentication)
  • 授权(Authorization)
  • 会话管理
  • 加密(Cryptography)

在核心特性的基础上,又提供了 Web 支持和缓存等特性。

认证(Authentication)

认证实际回答了who are you的问题。

实体(Subject)

实体代表了 单个 应用用户,维护了用户状态,提供了安全操作,包括认证(登录、登出)、鉴权(访问控制)和会话访问。

Subject currentUser = SecurityUtils.getSubject();

认证 Token

通过提供主体(Principal)和凭证(Credential)完成身份认证过程。

主体(Principal),用于唯一确定实体(Subject)。例如:用户名、邮箱、手机号。

凭证(Credential),用于证明实体(Subject)身份。例如:密码、证书、指纹。

这也同样反映到认证 Token 接口 org.apache.shiro.authc.AuthenticationToken 的定义上:

public interface AuthenticationToken extends Serializable {

    Object getPrincipal();

    Object getCredentials();

}

ZJRB7zn.png!web

认证过程

n6jYBf7.png!web

认证过程大约经历了以下几个步骤:

  1. 实体(Subject)使用认证 token 登录;
  2. Security Manager 执行登录操作;
  3. Security Manager 内部的 Authenticator 对 token 进行认证;
  4. Authenticator 调用 Realm 获取认证信息。;
  5. 如果存在多个 Realm,由 Authentication Strategy 决定是否认证成功。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK