PHPackages                             lzpeng/think-auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. lzpeng/think-auth

ActiveLibrary

lzpeng/think-auth
=================

1.1.5(6y ago)025PHPCI failing

Since Aug 7Pushed 6y agoCompare

[ Source](https://github.com/liuzhanpeng/think-auth)[ Packagist](https://packagist.org/packages/lzpeng/think-auth)[ RSS](/packages/lzpeng-think-auth/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (14)Used By (0)

think-auth
==========

[](#think-auth)

**1.0 版本已废弃, 请使用1.1版本**

基于thinkphp5.1的用户认证系统。

安装
--

[](#安装)

composer.json 添加以下内容

```
"repositories": [
    {
        "type": "git",
        "url": "http://172.0.6.108:3000/lzpeng/think-auth"
    }
],

// 设置非安全链接
"config": {
    ...
    "secure-http": false
}

```

然后命令行运行

```
composer require lzpeng/think-auth=1.1.*

```

配置
--

[](#配置)

在项目应用或模块的config目录下创建auth.php配置文件

```
return [
    'default' => 'test1',                                   // 默认使用的认证器标识

    'test1' => [                                        // 认证器标识
        'driver' => 'session',                          // 认证器驱动; 内置支持session和simpleToken
        'sessionKey' => 'UserIdentity',                 // 会话key
        'provider' => [                                 // 认证所使用的用户提供器
            'driver' => 'model',                        // 用户提供器驱动; 内置支持model和database
            'modelClass' => 'app\common\model\User',    // 模型类; 需要实现Lzpeng\Auth\Contracts\UserIdentity接口
            'idKey' => 'id',                            // 模型对应的用户标识属性名称; 可选; 默认为'id'
            'passwordKey' => 'password',                // 用户凭证数组里的密码key; 可选; 默认为'password'
            'forceValidatePassword' => true,            // 是否强制验证密码; 如果是ture但没传入密码凭证，凭证就验证失败; false的话不传密码凭证就忽略密码凭证的验证; 可选; 默认为true
            'hasher' => [
                'driver' => 'bcrypt',                    // 密码hasher; 内置只支持bcrypt

                // 'driver' => '\test\HMacHasher'        // 只支持自定义driver; 需实现Lzpeng\Auth\Contracts\Hasher接口
                // 'algo' => 'md5',
                // 'salt' => 'xxxxxx',
            ]
        ],

        // 以下是simpleToken认证器配置例子
        // 'driver' => 'simpleToken',
        // 'tokenKey' => 'User-Token',             // token名称
        // 'cache' => [                            // 缓存配置; 支持thinkphp的缓存配置 可选; 不设置使用框架的cache配置;
        //     'type'   => 'File',
        //     'path'   => '',
        //     'prefix' => '',
        //     'expire' => 1200,
        // ],

        // 以下的database用户提供器配置例子
        // 'provider' => [
        //     'driver' => 'database',
        //     'table' => 'user',
        //     'passwordKey' => 'password',                // 用户凭证数组里的密码key; 可选; 默认为'password'
        //     'forceValidatePassword' => true,            // 是否强制验证密码; 如果是ture但没传入密码凭证，凭证就验证失败; false的话不传密码凭证就忽略密码凭证的验证; 可选; 默认为true
        //     'hasher' => [
        //         'driver' => 'bcrypt',                    // 密码hasher; 内置只支持bcrypt
        //     ]
        // ]

        'behaviors' => [                                    // 认证过程中的行为绑定; 实现了AuthBehavior接口
            'login_before' => [
                'app\behaivor\checkAttempt',
            ],
            'login_success' => [],
            'login_failure' => [],
            'logout_before' => [],
            'logout_after' => [],
        ]

    ],

    'test2' => [
        ...
    ],
]
```

使用例子
----

[](#使用例子)

用户凭证登录:

```
use Lzpeng\Auth\Auth;
use Lzpeng\Auth\Exceptions\AuthenticationException;

try {
    $result = Auth::login(['username' => 'test', 'password' => 'password']);
    // session认证器，$result返回为null
    // simpleToken认证器，$result返回为token, 用于返回给客户端

    // 认证成功处理逻辑
} catch (AuthenticationException $ex) {
    // 异常处理
}
```

跳过认证，直接设置认证用户

```
try {
    $user = fromOtherSystem($id);
    $result = Auth::setUser($user);
    // 认证成功处理逻辑
} catch (AuthenticationException $ex) {
    // 异常处理
}
```

判断当前用户是否已登录:

```
if (Auth::isLogined()) {
    // 用户已认证登录
}
```

获取当前用户标识

```
$id = Auth::getId();     // 如果未认证登录将返回null
```

获取当前用户对象：

```
$user = Auth::getUser();    // 如果未认证登录将返回null
```

用户登出:

```
Auth::logout();
```

多认证器使用: 用于单一模块有多个用户系统的时候

```
Auth::make('test1')->login(['username' => 'xxx', 'password' = 'xxx']);
Auth::make('test2')->login(['admin' => 'xxx', 'password' => 'xxxx]);

$user = Auth::make('test1')->getUser();
$admin = Auth::make('test2')->getUser();
```

扩展
--

[](#扩展)

通过实现Lzpeng\\Auth\\Contracts\\Authenticator接口可以实现自定义认证器

通过实现Lzpeng\\Auth\\Contracts\\UserProvider接口可以实现自定义用户提供器

通过实现Lzpeng\\Auth\\Contracts\\UserIdentity接口可以实现自定义用户对象

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~19 days

Total

12

Last Release

2263d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b085c9e9145b16a586a1574c9ece3fc1793c14c0d120f9f3c74b301b19165925?d=identicon)[liuzhanpeng](/maintainers/liuzhanpeng)

---

Top Contributors

[![liuzhanpeng](https://avatars.githubusercontent.com/u/6972018?v=4)](https://github.com/liuzhanpeng "liuzhanpeng (96 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lzpeng-think-auth/health.svg)

```
[![Health](https://phpackages.com/badges/lzpeng-think-auth/health.svg)](https://phpackages.com/packages/lzpeng-think-auth)
```

###  Alternatives

[topthink/think

the new thinkphp framework

8.0k1.2M13](/packages/topthink-think)[topthink/think-queue

The ThinkPHP6 Queue Package

640675.0k75](/packages/topthink-think-queue)[topthink/think-swoole

Swoole extend for thinkphp

477174.4k19](/packages/topthink-think-swoole)[topthink/think-captcha

captcha package for thinkphp

132934.4k68](/packages/topthink-think-captcha)[topthink/think-worker

workerman extend for thinkphp

202227.2k9](/packages/topthink-think-worker)[topthink/think-migration

96460.8k121](/packages/topthink-think-migration)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
