Skip to content

Commit e8e42e0

Browse files
committed
basic auth
1 parent 1c90dec commit e8e42e0

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

.gitignore

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
*.pid.lock
11+
12+
# Directory for instrumented libs generated by jscoverage/JSCover
13+
lib-cov
14+
15+
# Coverage directory used by tools like istanbul
16+
coverage
17+
18+
# nyc test coverage
19+
.nyc_output
20+
21+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
22+
.grunt
23+
24+
# node-waf configuration
25+
.lock-wscript
26+
27+
# Compiled binary addons (http://nodejs.org/api/addons.html)
28+
build/Release
29+
30+
# Dependency directories
31+
node_modules
32+
jspm_packages
33+
34+
# Optional npm cache directory
35+
.npm
36+
37+
# Optional REPL history
38+
.node_repl_history

app.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var config = require('./config')
2+
var express = require('express')
3+
var authMiddleware = require('./middleware/auth')
4+
5+
6+
var app = express();
7+
8+
app.use(authMiddleware.userAuth)
9+
10+
app.get('/', function (req, res) {
11+
res.send('hello world')
12+
})
13+
14+
app.listen(config.web_app_port, function () {
15+
console.log(`${new Date}, app is listening at ${config.web_app_port}`)
16+
});

config.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var config = {
2+
web_app_port: 8999,
3+
// 简单的用户系统认证配置
4+
account: {
5+
alsotang: 'hehefuhehe',
6+
linktang: 'muerqiaohuiche',
7+
}
8+
}
9+
10+
11+
exports = module.exports = config;

middleware/auth.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var basicAuth = require('basic-auth')
2+
var config = require('../config')
3+
4+
5+
exports.userAuth = function (req, res, next) {
6+
var credentials = basicAuth(req)
7+
8+
if (!credentials ||
9+
!config.account.hasOwnProperty(credentials.name) ||
10+
config.account[credentials.name] !== credentials.pass
11+
) {
12+
res.status(401)
13+
res.set('WWW-Authenticate', 'Basic realm="example"')
14+
res.send('Access denied')
15+
return
16+
}
17+
18+
next()
19+
}

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "node-project-9",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/qcourse/node-project-9.git"
12+
},
13+
"author": "alsotang <[email protected]>",
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/qcourse/node-project-9/issues"
17+
},
18+
"homepage": "https://github.com/qcourse/node-project-9#readme",
19+
"dependencies": {
20+
"express": "^4.14.0",
21+
"eventproxy": "^0.3.4",
22+
"basic-auth": "^1.0.4"
23+
}
24+
}

0 commit comments

Comments
 (0)