diff --git a/application/home/controller/Index.php b/application/home/controller/Index.php index 62a83df0..79c79589 100644 --- a/application/home/controller/Index.php +++ b/application/home/controller/Index.php @@ -8,11 +8,11 @@ use app\common\controller\Base; class Index extends Base { - // /** - // * 操作成功跳转 - // * - // * @return \think\Response - // */ + /** + * 操作成功跳转 + * + * @return \think\Response + */ public function mysuccess($msg = '操作成功~') { // 模版赋值 diff --git a/application/home/view/index/notfound.html b/application/home/view/index/notfound.html index cbe141b1..fad09c04 100644 --- a/application/home/view/index/notfound.html +++ b/application/home/view/index/notfound.html @@ -45,7 +45,7 @@ if( window.top == window.self ){ // location.href = "/"; $('#hrel').html("您可以:< 去首页\ - |返回上一页\ + |登录页面\ >"); }else{ // removeIframe(); @@ -57,14 +57,14 @@ // 跳转到首页 function topurl() { - location.href = "/"; + location.href = "/"; } // 跳转到上一页 - function lasturl() + function loginurl() { - location.href = "history.go(-1)"; + location.href = '/login'; } // 关闭页面 diff --git a/application/login/controller/Index.php b/application/login/controller/Index.php index 7a9fc8a8..3cfdbee9 100644 --- a/application/login/controller/Index.php +++ b/application/login/controller/Index.php @@ -5,6 +5,10 @@ namespace app\login\controller; use think\Controller; // 引用用户数据模型 use app\member\model\Member as membermod; +// 引用请求类 +use think\facade\Request; +// 引用加密类 +use WhiteHat101\Crypt\APR1_MD5; class Index extends Controller @@ -20,23 +24,38 @@ class Index extends Controller // 登录验证 public function yanzheng() { + // 实例化模型 $membermod = new membermod(); - - $list = request()->post(); - + // 实例化验证模型 $validate = new \app\login\validate\Yanzheng; + // 实例化加密类 + $md5 = new APR1_MD5(); - $data = request()->post(); + // 获取表单数据 + $data = request()->only(['username','password']); + // 验证表单数据 $result = $validate->check($data); $msg = $validate->getError(); - - - if(!$result){ $this->redirect('/notfound',[], 302, ['msg' => $msg]); } - // return true; + // 重新计算密码 + $miyao = $membermod->miyao('admin'); + $data['password'] = $md5->hash($miyao,$data['password']); + + // 验证用户名和密码 + $list = $membermod->check($data); + + if($list) + { + dump($list); + }else{ + dump($list); + } + + + } } diff --git a/application/login/validate/Yanzheng.php b/application/login/validate/Yanzheng.php index 500b6b53..6751b7d5 100644 --- a/application/login/validate/Yanzheng.php +++ b/application/login/validate/Yanzheng.php @@ -14,7 +14,7 @@ class Yanzheng extends Validate */ protected $rule = [ 'username' => 'require', - 'password' => 'require|number' + 'password' => 'require' ]; protected $message = [ diff --git a/application/member/model/Member.php b/application/member/model/Member.php index dab1db08..90abfc5c 100644 --- a/application/member/model/Member.php +++ b/application/member/model/Member.php @@ -6,5 +6,27 @@ use think\Model; class Member extends Model { - // + // 密码验证 + // $data = ['username'=>'aa,'password'=>'bb'] + public function check( $data = [] ) + { + // 验证用户名与密码是否匹配 + $list = $this + ->where('username',$data['username']) + ->where('password',$data['password']) + ->count();dump($list); + + return $list; + } + + public function miyao($username) + { + // 查询数据 + $miyao = $this + ->where('username',$username) + ->value('miyao'); + + // 返回数据 + return $miyao; + } } diff --git a/composer.json b/composer.json index 6bbd301f..618df8ec 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "topthink/framework": "5.1.*", "topthink/think-migration": "^2.0", "php4world/auth": "^1.0", - "topthink/think-captcha": "^2.0" + "topthink/think-captcha": "^2.0", + "whitehat101/apr1-md5": "^1.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index fc5c0b40..a47b3c1b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e3585313f0dee8b0dee6fecf90371b34", + "content-hash": "07150719627d3a1c8f9538fe65750f56", "packages": [ { "name": "php4world/auth", @@ -241,6 +241,56 @@ } ], "time": "2017-12-31T16:32:22+00:00" + }, + { + "name": "whitehat101/apr1-md5", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/whitehat101/apr1-md5.git", + "reference": "8b261c9fc0481b4e9fa9d01c6ca70867b5d5e819" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/whitehat101/apr1-md5/zipball/8b261c9fc0481b4e9fa9d01c6ca70867b5d5e819", + "reference": "8b261c9fc0481b4e9fa9d01c6ca70867b5d5e819", + "shasum": "", + "mirrors": [ + { + "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "4.0.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "WhiteHat101\\Crypt\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jeremy Ebler", + "email": "jebler@gmail.com" + } + ], + "description": "Apache's APR1-MD5 algorithm in pure PHP", + "homepage": "https://github.com/whitehat101/apr1-md5", + "keywords": [ + "MD5", + "apr1" + ], + "time": "2015-02-11T11:06:42+00:00" } ], "packages-dev": [], diff --git a/database/migrations/20181012140355_member.php b/database/migrations/20181012140355_member.php index bd7c259f..c7e7a5bc 100644 --- a/database/migrations/20181012140355_member.php +++ b/database/migrations/20181012140355_member.php @@ -36,8 +36,9 @@ class Member extends Migrator ->addColumn('xingming','string',['limit'=>15,'null'=>false,'comment'=>'用户姓名']) ->addColumn('xingbie','boolean',['limit'=>1,'default'=>'2','null'=>false,'comment'=>'0=女,1=男,2=未知']) ->addColumn('shengri','integer',['limit'=>11,'null'=>true,'comment'=>'出生日期']) - ->addColumn('username','string',['limit'=>18,'null'=>false,'comment'=>'用户帐号']) - ->addColumn('password','string',['limit'=>128,'null'=>false,'comment'=>'登录密码']) + ->addColumn('username','string',['limit'=>12,'null'=>false,'comment'=>'用户帐号']) + ->addColumn('password','string',['limit'=>37,'null'=>false,'comment'=>'登录密码']) + ->addColumn('miyao','string',['limit'=>8,'null'=>false,'comment'=>'密钥']) ->addColumn('denglucishu','integer',['limit'=>5,'default'=>0,'null'=>false,'comment'=>'登录次数']) ->addColumn('ip','string',['limit'=>55,'default'=>'127.0.0.1','null'=>false,'comment'=>'登录IP']) ->addColumn('lasttime','integer',['limit'=>11,'null'=>false,'comment'=>'最后登录时间']) @@ -45,6 +46,7 @@ class Member extends Migrator ->addColumn('create_time','integer',['limit'=>11,'default'=>'1539158918','null'=>false,'comment'=>'创建时间']) ->addColumn('update_time','integer',['limit'=>11,'default'=>'1539158918','null'=>false,'comment'=>'更新时间']) ->addColumn('delete_time','integer',['limit'=>11,'null'=>true,'comment'=>'删除时间']) + ->addIndex(array('username'), array('unique' => true)) ->create(); } } diff --git a/vendor/composer/autoload_psr4.php b/vendor/composer/autoload_psr4.php index 93808370..3bd1e037 100644 --- a/vendor/composer/autoload_psr4.php +++ b/vendor/composer/autoload_psr4.php @@ -11,5 +11,6 @@ return array( 'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'), 'php4world\\' => array($vendorDir . '/php4world/auth/src'), 'app\\' => array($baseDir . '/application'), + 'WhiteHat101\\Crypt\\' => array($vendorDir . '/whitehat101/apr1-md5/src'), 'Phinx\\' => array($vendorDir . '/topthink/think-migration/phinx/src/Phinx'), ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index a463400e..e6ae68ca 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -26,6 +26,10 @@ class ComposerStaticInitbe7733f58d7b733cc58f1391b7bd5053 array ( 'app\\' => 4, ), + 'W' => + array ( + 'WhiteHat101\\Crypt\\' => 18, + ), 'P' => array ( 'Phinx\\' => 6, @@ -53,6 +57,10 @@ class ComposerStaticInitbe7733f58d7b733cc58f1391b7bd5053 array ( 0 => __DIR__ . '/../..' . '/application', ), + 'WhiteHat101\\Crypt\\' => + array ( + 0 => __DIR__ . '/..' . '/whitehat101/apr1-md5/src', + ), 'Phinx\\' => array ( 0 => __DIR__ . '/..' . '/topthink/think-migration/phinx/src/Phinx', diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 7a060aeb..9cb06f02 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -244,5 +244,57 @@ "email": "448901948@qq.com" } ] + }, + { + "name": "whitehat101/apr1-md5", + "version": "v1.0.0", + "version_normalized": "1.0.0.0", + "source": { + "type": "git", + "url": "https://github.com/whitehat101/apr1-md5.git", + "reference": "8b261c9fc0481b4e9fa9d01c6ca70867b5d5e819" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/whitehat101/apr1-md5/zipball/8b261c9fc0481b4e9fa9d01c6ca70867b5d5e819", + "reference": "8b261c9fc0481b4e9fa9d01c6ca70867b5d5e819", + "shasum": "", + "mirrors": [ + { + "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "4.0.*" + }, + "time": "2015-02-11T11:06:42+00:00", + "type": "library", + "installation-source": "dist", + "autoload": { + "psr-4": { + "WhiteHat101\\Crypt\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jeremy Ebler", + "email": "jebler@gmail.com" + } + ], + "description": "Apache's APR1-MD5 algorithm in pure PHP", + "homepage": "https://github.com/whitehat101/apr1-md5", + "keywords": [ + "MD5", + "apr1" + ] } ] diff --git a/vendor/whitehat101/apr1-md5/.gitignore b/vendor/whitehat101/apr1-md5/.gitignore new file mode 100644 index 00000000..8b7ef350 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/.gitignore @@ -0,0 +1,2 @@ +/vendor +composer.lock diff --git a/vendor/whitehat101/apr1-md5/.travis.yml b/vendor/whitehat101/apr1-md5/.travis.yml new file mode 100644 index 00000000..aa14ee55 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/.travis.yml @@ -0,0 +1,14 @@ +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + - 5.6 + - hhvm + +before_script: + - composer self-update + - composer install --prefer-source --no-interaction --dev + +script: phpunit diff --git a/vendor/whitehat101/apr1-md5/LICENSE b/vendor/whitehat101/apr1-md5/LICENSE new file mode 100644 index 00000000..3e0c9c76 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Jeremy + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/vendor/whitehat101/apr1-md5/README.md b/vendor/whitehat101/apr1-md5/README.md new file mode 100644 index 00000000..71152419 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/README.md @@ -0,0 +1,70 @@ +# Apache's APR1 MD5 Hashing Algorithm in PHP +[![Build Status](https://travis-ci.org/whitehat101/apr1-md5.svg)](https://travis-ci.org/whitehat101/apr1-md5) + +There is no way that the best way to generate Apache's apr1-md5 password hashes is from a 7-year-old comment on php.net. Only a n00b would trust a crypto algorithm from a non-security website's forum. Sadly, that is how the PHP community has accessed this algorithm, until now. + +Here is a tested, referenced, documented, and packaged implementation of Apache's APR1 MD5 Hashing Algorithm in pure PHP. + +## Install + +composer.json: +```json +{ + "require": { + "whitehat101/apr1-md5": "~1.0" + } +} +``` + +## Use + +```php +use WhiteHat101\Crypt\APR1_MD5; + +// Check plaintext password against an APR1-MD5 hash +echo APR1_MD5::check('plaintext', '$apr1$PVWlTz/5$SNkIVyogockgH65nMLn.W1'); + +// Hash a password with a known salt +echo APR1_MD5::hash('PASSWORD', '__SALT__'); + +// Hash a password with a secure random salt +echo APR1_MD5::hash('PASSWORD'); + +// Generate a secure random salt +echo APR1_MD5::salt(); +``` + +The ideal `__SALT__` is an 8 character string. Valid salts are alphanumeric and `.` or `/`. Shorter salts are allowed. Longer salts are truncated after the 8th character. + +## Generate Hashes via Other Tools + +### htpasswd +```bash +$ htpasswd -nmb apache apache +apache:$apr1$rOioh4Wh$bVD3DRwksETubcpEH90ww0 + +$ htpasswd -nmb ChangeMe1 ChangeMe1 +ChangeMe1:$apr1$PVWlTz/5$SNkIVyogockgH65nMLn.W1 + +$ htpasswd -nmb WhiteHat101 WhiteHat101 +WhiteHat101:$apr1$HIcWIbgX$G9YqNkCVGlFAN63bClpoT/ +``` + +### openssl +```bash +$ openssl passwd -apr1 -salt rOioh4Wh apache +$apr1$rOioh4Wh$bVD3DRwksETubcpEH90ww0 + +$ openssl passwd -apr1 -salt PVWlTz/5 ChangeMe1 +$apr1$PVWlTz/5$SNkIVyogockgH65nMLn.W1 + +$ openssl passwd -apr1 -salt HIcWIbgX WhiteHat101 +$apr1$HIcWIbgX$G9YqNkCVGlFAN63bClpoT/ +``` + +## Testing + +```bash +composer install +vendor/bin/phpunit +``` diff --git a/vendor/whitehat101/apr1-md5/composer.json b/vendor/whitehat101/apr1-md5/composer.json new file mode 100644 index 00000000..59f8bdf2 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/composer.json @@ -0,0 +1,27 @@ +{ + "name": "whitehat101/apr1-md5", + "description": "Apache's APR1-MD5 algorithm in pure PHP", + "homepage": "https://github.com/whitehat101/apr1-md5", + "license": "MIT", + "keywords": ["md5","apr1"], + "authors": [ + { + "name": "Jeremy Ebler", + "email": "jebler@gmail.com" + } + ], + + "require": { + "php": ">=5.3.0" + }, + + "require-dev": { + "phpunit/phpunit": "4.0.*" + }, + + "autoload": { + "psr-4": { + "WhiteHat101\\Crypt\\": "src" + } + } +} diff --git a/vendor/whitehat101/apr1-md5/phpunit.xml b/vendor/whitehat101/apr1-md5/phpunit.xml new file mode 100644 index 00000000..d60b09fd --- /dev/null +++ b/vendor/whitehat101/apr1-md5/phpunit.xml @@ -0,0 +1,17 @@ + + + + + ./tests/ + + + diff --git a/vendor/whitehat101/apr1-md5/src/APR1_MD5.php b/vendor/whitehat101/apr1-md5/src/APR1_MD5.php new file mode 100644 index 00000000..f4e78d92 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/src/APR1_MD5.php @@ -0,0 +1,68 @@ +0; $i-=16) + $context .= substr($binary, 0, min(16, $i)); + for($i=$max; $i>0; $i>>=1) + $context .= ($i & 1) ? chr(0) : $mdp[0]; + $binary = pack('H32', md5($context)); + for($i=0; $i<1000; $i++) { + $new = ($i & 1) ? $mdp : $binary; + if($i % 3) $new .= $salt; + if($i % 7) $new .= $mdp; + $new .= ($i & 1) ? $binary : $mdp; + $binary = pack('H32', md5($new)); + } + $hash = ''; + for ($i = 0; $i < 5; $i++) { + $k = $i+6; + $j = $i+12; + if($j == 16) $j = 5; + $hash = $binary[$i].$binary[$k].$binary[$j].$hash; + } + $hash = chr(0).chr(0).$binary[11].$hash; + $hash = strtr( + strrev(substr(base64_encode($hash), 2)), + self::BASE64_ALPHABET, + self::APRMD5_ALPHABET + ); + return '$apr1$'.$salt.'$'.$hash; + } + + // 8 character salts are the best. Don't encourage anything but the best. + public static function salt() { + $alphabet = self::APRMD5_ALPHABET; + $salt = ''; + for($i=0; $i<8; $i++) { + $offset = hexdec(bin2hex(openssl_random_pseudo_bytes(1))) % 64; + $salt .= $alphabet[$offset]; + } + return $salt; + } + + public static function check($plain, $hash) { + $parts = explode('$', $hash); + return self::hash($plain, $parts[2]) === $hash; + } + +} diff --git a/vendor/whitehat101/apr1-md5/tests/APR1_MD5_CheckTest.php b/vendor/whitehat101/apr1-md5/tests/APR1_MD5_CheckTest.php new file mode 100644 index 00000000..d1620eb2 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/tests/APR1_MD5_CheckTest.php @@ -0,0 +1,25 @@ +assertTrue( + APR1_MD5::check('WhiteHat101','$apr1$HIcWIbgX$G9YqNkCVGlFAN63bClpoT/') + ); + } + + public function testHash_apache() { + $this->assertTrue( + APR1_MD5::check('apache','$apr1$rOioh4Wh$bVD3DRwksETubcpEH90ww0') + ); + } + + public function testHash_ChangeMe1() { + $this->assertTrue( + APR1_MD5::check('ChangeMe1','$apr1$PVWlTz/5$SNkIVyogockgH65nMLn.W1') + ); + } + +} diff --git a/vendor/whitehat101/apr1-md5/tests/APR1_MD5_HashTest.php b/vendor/whitehat101/apr1-md5/tests/APR1_MD5_HashTest.php new file mode 100644 index 00000000..ec87a356 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/tests/APR1_MD5_HashTest.php @@ -0,0 +1,61 @@ +assertEquals( + '$apr1$HIcWIbgX$G9YqNkCVGlFAN63bClpoT/', + APR1_MD5::hash('WhiteHat101','HIcWIbgX') + ); + } + + public function testHash_apache() { + $this->assertEquals( + '$apr1$rOioh4Wh$bVD3DRwksETubcpEH90ww0', + APR1_MD5::hash('apache','rOioh4Wh') + ); + } + + public function testHash_ChangeMe1() { + $this->assertEquals( + '$apr1$PVWlTz/5$SNkIVyogockgH65nMLn.W1', + APR1_MD5::hash('ChangeMe1','PVWlTz/5') + ); + } + + // Test some awkward inputs + + public function testHash_ChangeMe1_blankSalt() { + $this->assertEquals( + '$apr1$$DbHa0iITto8vNFPlkQsBX1', + APR1_MD5::hash('ChangeMe1','') + ); + } + + public function testHash_ChangeMe1_longSalt() { + $this->assertEquals( + '$apr1$PVWlTz/5$SNkIVyogockgH65nMLn.W1', + APR1_MD5::hash('ChangeMe1','PVWlTz/50123456789') + ); + } + + public function testHash_ChangeMe1_nullSalt() { + $hash = APR1_MD5::hash('ChangeMe1'); + $this->assertEquals(37, strlen($hash)); + } + + public function testHash__nullSalt() { + $hash = APR1_MD5::hash(''); + $this->assertEquals(37, strlen($hash)); + } + + // a null password gets coerced into the blank string. + // is this sensible? + public function testHash_null_nullSalt() { + $hash = APR1_MD5::hash(null); + $this->assertEquals(37, strlen($hash)); + } + +} diff --git a/vendor/whitehat101/apr1-md5/tests/APR1_MD5_SaltTest.php b/vendor/whitehat101/apr1-md5/tests/APR1_MD5_SaltTest.php new file mode 100644 index 00000000..c5716474 --- /dev/null +++ b/vendor/whitehat101/apr1-md5/tests/APR1_MD5_SaltTest.php @@ -0,0 +1,19 @@ +assertInternalType('string', APR1_MD5::salt()); + } + + public function testSaltPattern() { + $this->assertRegExp('/.{8}/', APR1_MD5::salt()); + } + + public function testSaltRamdomness() { + $this->assertNotEquals(APR1_MD5::salt(), APR1_MD5::salt()); + } + +}