Skip to content

Commit

Permalink
修正token验证实现
Browse files Browse the repository at this point in the history
  • Loading branch information
AutumnSun1996 committed Aug 21, 2022
1 parent 993f5a8 commit 36c5080
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 1 addition & 2 deletions ocrweb_multi/config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
debug: '1'

server:
host: 127.0.0.1
port: 9003
# OCR接口Token, 为null时将跳过Token验证
token: null

global:
Expand Down
6 changes: 3 additions & 3 deletions ocrweb_multi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def index():
return send_file('static/index.html')


def json_response(data, *args):
return make_response(tojson(data), {"content-type": 'application/json'}, *args)
def json_response(data, status=200):
return make_response(tojson(data), status, {"content-type": 'application/json'})


@app.route('/lang')
Expand All @@ -43,7 +43,7 @@ def ocr():
"""执行文字识别"""
if conf['server'].get('token'):
if request.values.get('token') != conf['server']['token']:
return json_response({'msg': 'invalid token'}, 403)
return json_response({'msg': 'invalid token'}, status=403)

lang = request.values.get('lang') or 'ch'
detect = parse_bool(request.values.get('detect') or 'true')
Expand Down
5 changes: 4 additions & 1 deletion ocrweb_multi/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ <h1>
reader.readAsDataURL(targetFile);

var formdata = new FormData();
var token = location.hash.slice(1);
formdata.set('token', token);
formdata.set('image', targetFile);
formdata.set('lang', $('input[name=lang]:checked').val());

Expand All @@ -172,11 +174,12 @@ <h1>
},
error: function (evt) {
console.log(evt);
alert('检测失败: ' + evt);
alert('检测失败: ' + evt.status + ', ' + evt.statusText);
},
success: function (resp) {
$("#wrapper").hide();
if (resp.msg !== 'OK') {
console.log(resp);
alert('检测失败: ', resp.msg);
return;
}
Expand Down

0 comments on commit 36c5080

Please sign in to comment.