加密认证逻辑总是认证不了
来源:1-6 第一天 写段代码实现加密认证逻辑
慕田峪1594825
2016-08-25
加密总是认证不了, 打印出来this, 发现了404,
{ request:
{ method: 'GET',
url: '/favicon.ico',
header:
{ host: '127.0.0.1:8080',
connection: 'keep-alive',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36 QQBrowser/4.1.4132.400',
accept: '*/*',
referer: 'http://127.0.0.1:8080/',
'accept-encoding': 'gzip, deflate, sdch',
'accept-language': 'zh-CN,zh;q=0.8' } },
response: { status: 404, message: 'Not Found', header: {} },
app: { subdomainOffset: 2, proxy: false, env: 'development' },
originalUrl: '/favicon.ico',
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>' }
怎么破
4回答
-
慕田峪1594825
提问者
2016-08-26
'use strict' var Koa = require("koa") var path = require('path') var wechat = require('./wechat/g') var util = require('./libs/util') var wechat_file = path.join(__dirname, './config/wechat.txt') var config = require('./config') var weixin = require('./weixin') var app = new Koa() app.use(wechat(config.wechat, weixin.reply)) app.listen(8080) console.log("Listen: 8080")
032016-08-26 -
Scott
2016-08-26
大哥,代码可以格式化一下么,眼睛看瞎了。
把这段代码贴一下,试试,另外把 app.js 贴一下,现在根据你提供的素材,看不到到底是哪个环节出问题了。
'use strict' var sha1 = require('sha1') var getRawBody = require('raw-body') var Wechat = require('./wechat') var util = require('./util') module.exports = function(opts, handler) { var wechat = new Wechat(opts) return function *(next) { console.log(this.method) var token = opts.token var signature = this.query.signature var nonce = this.query.nonce var timestamp = this.query.timestamp var echostr = this.query.echostr var str = [token, timestamp, nonce].sort().join('') var sha = sha1(str) if (this.method === 'GET') { if (sha === signature) { this.body = echostr + '' } else { this.body = 'wrong' } } else if (this.method === 'POST') { if (sha !== signature) { this.body = 'wrong' return false } var data = yield getRawBody(this.req, { length: this.length, limit: '1mb', encoding: this.charset }) var content = yield util.parseXMLAsync(data) console.log(content) var message = util.formatMessage(content.xml) console.log(message) this.weixin = message yield handler.call(this, next) wechat.reply.call(this) } } }
00 -
慕田峪1594825
提问者
2016-08-26
'use strict'
var sha1 = require('sha1')
var Wechat = require('./wechat')
var getRawBody = require('raw-body')
var util = require('./util')
module.exports = function (opts, handler) {
var wechat = new Wechat(opts)
return function *(next) {
var that = this
console.log(this.query)
var token = opts.token
var signature = this.query.signature
var nonce = this.query.nonce
var timestamp = this.query.timestamp
var echostr = this.query.echostr
var str = [token, timestamp, nonce].sort().join('')
var sha = sha1(str)
if (this.method === 'GET') {
if ( sha === signature) {
this.body = echostr + ''
}
else {
this.body = 'wrong'
}
} else if (this.method === 'POST') {
if ( sha !== signature) {
this.body = 'wrong'
return false
}
var data = yield getRawBody(this.req, {
length: this.length,
limit: '1mb',
encoding: this.charset
})
// console.log(data.toString())
var content = yield util.parseXMLAsync(data)
content = util.formatMessage(content.xml)
console.log(content)
this.weixin = content
yield handler.call(this, next)
wechat.reply.call(this)
}
}
}
00 -
Scott
2016-08-26
把相关代码贴一下,检查下有没有给 this.body 赋值,有没有比对加密的参数
012016-08-26
相似问题