Download the PHP package watsonhaw/think-satoken without Composer

On this page you can find all versions of the php package watsonhaw/think-satoken. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package think-satoken

think-satoken

项目介绍

think-satoken 是一个轻量级权限认证扩展,专为 ThinkPHP(6|8) 框架设计。追求简单、直观,适合中小型项目使用。

设计理念

功能特性

部署指南

单机部署

使用默认的 File 缓存即可,无需额外配置。

多机/多实例部署

多机部署(负载均衡环境)时,token 会话数据需要在多台服务器之间共享。请在配置中指定 store 为 'redis':

同时确保在 config/cache.php 中正确配置 Redis 缓存通道(参考 ThinkPHP 缓存文档):

为什么需要 Redis? File 缓存将数据存储在本地文件系统,不同服务器之间无法共享。用户在 A 机登录后,请求打到 B 机时 B 机的文件缓存中没有该 token,会导致误判为未登录。

安装

使用 Composer 安装:

配置

配置文件 config/satoken.php(发布后):

配置说明:

配置项 类型 默认值 说明
token_name string '' 自定义请求头名称,为空时从 Authorization: Bearer 读取
store string|null null 缓存通道名称,对应 cache.php 中的 stores 键;null 使用默认缓存
timeout int 604800 Token 有效期(秒),默认 7 天
auto_renew bool true 是否启用滑动续期
renew_before int 3600 在过期前多少秒续期(秒),剩余不足此时才触发续期,设为 0 表示每次访问都续期
max_login_count int 10 同一账号最多同时在线的 token 数;超出顶掉最早的;设为 1 等价于单端登录

滑动续期示例:

登录数量限制示例:

缓存隔离建议:

即使是单机部署,也建议为 SaToken 配置独立的缓存通道(如单独的 Redis select 或独立的文件目录),通过 prefix 区分,便于管理和清理。

使用示例

1. 登录认证

多 Token 在线行为:默认同一账号最多同时保留 10 个有效 Token,互不干扰。超出上限时最早登录的被自动顶掉;各 Token 的 extra 数据独立保存,适合区分不同设备或权限范围。

2. 使用中间件

SatokenAuth 中间件会在请求处理前调用 checkLogin() 验证登录状态,验证失败时直接返回 JSON 响应。

在 app/middleware.php 中注册:

在路由中使用:

中间件响应:

状态 HTTP 状态码 返回 JSON
未提供 token 401 {"code": ..., "msg": "未提供token", "data": null}
无效 token 401 {"code": ..., "msg": "无效的token格式/无效的token", "data": null}
通过校验 — 放行

3. 令牌传递

推荐通过请求头传递令牌:

异常处理

核心 API

方法 说明
createToken(): string 生成 UUID v4 token
validateTokenFormat(string $token): bool 验证 token 格式
login(int $loginId, array $extra = []): string 登录(生成独立 token,支持多端;超出 max_login_count 顶掉最早的)
logout(?string $token = null): bool 登出(仅指定 token 失效,同账号其他 token 不受影响)
isLogin(?string $token = null): bool 检查是否已登录
checkLogin(?string $token = null): void 校验登录状态,失败抛异常
getCurrentLoginId(?string $token = null): int 获取当前登录用户 ID
getTokenInfo(?string $token = null): array 获取 token 完整信息
getExtra(?string $token = null): array 获取自定义附加数据
setExtra(?string $token = null, array $extra = []): bool 更新附加数据
getTokenExpireTime(?string $token = null): int 获取过期时间戳
getTokenRemainingTime(?string $token = null): int 获取剩余有效秒数
kickout(int $id): bool 按用户 ID 踢出(该账号所有 token 全部失效,全端下线)
kickoutByToken(string $token): bool 按 token 踢出(仅指定 token 失效)

License

MIT


All versions of think-satoken with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1||^8.0
ramsey/uuid Version ^4.7
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package watsonhaw/think-satoken contains the following files

Loading the files please wait ...