feathers.js -> 身份验证令牌丢失
feathers.js -> Authentication token missing
我有一个 feathters.js 应用程序,现在我想保护创建和更新挂钩。我使用 socket.io 客户端,目前正在使用 JWT。我已经添加了我认为需要添加的内容,但得到的是 Error: Authentication token missing
和 Error Authenticating
。后来我从我的代码中了解到这一点。我有后端/前端情况
这就是我到目前为止所实施的。
文件:backend\backend.js(在backend\index.js中调用以配置应用程序)
'use strict';
const path = require('path');
const serveStatic = require('feathers').static;
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const feathers = require('feathers');
const configuration = require('feathers-configuration');
const authentication = require('feathers-authentication');
const hooks = require('feathers-hooks');
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
const socketio = require('feathers-socketio');
const middleware = require('./middleware/index');
const services = require('./services/index');
const appFeathers = feathers();
appFeathers.configure(configuration(path.join(__dirname, '..')));
appFeathers.use(compress())
.options('*', cors())
.use(cors())
.use(favicon(path.join(appFeathers.get('public'), 'favicon.ico')))
.use('/', serveStatic(appFeathers.get('public')))
.use(bodyParser.json())
.use(bodyParser.urlencoded({extended: true}))
.configure(hooks())
.configure(rest())
.configure(socketio())
.configure(services)
.configure(middleware)
.configure(authentication());
module.exports = appFeathers;
文件:backend\config\default.json
{
"host": "localhost",
"port": 3001,
"mysql_connection": "mysql://CONNECTION_STRING",
"public": "../public/",
"auth": {
"idField": "id",
"token": {
"secret": "SECRET_KEY"
},
"local": {}
}
}
在前端的工作组件中:
<template>
<div class="vttIndex">
idnex.vue
todo: eagle.js slideshow
todo: first info
<ul>
<li v-for="message in listMessages">
{{ message }}
</li>
</ul>
</div>
</template>
<script>
import feathers from 'feathers/client';
import socketio from 'feathers-socketio/client';
import hooks from 'feathers-hooks';
import io from 'socket.io-client';
import authentication from 'feathers-authentication/client';
import * as process from "../nuxt.config";
const vttSocket = io(process.env.backendUrl);
const vttFeathers = feathers()
.configure(socketio(vttSocket))
.configure(hooks())
.configure(authentication());
const serviceMessage = vttFeathers.service('messages');
vttFeathers.authenticate({
type: 'token',
'token ': 'SECRET_KEY'
}).then(function(result){
console.log('Authenticated!', result);
}).catch(function(error){
console.error('Error authenticating!', error);
});
export default {
layout: 'default',
data: function() {
return {
listMessages: []
}
},
mounted: function() {
serviceMessage.find().then(page => {
this.listMessages = page.data;
});
serviceMessage.on('created', (serviceMessage) => {
this.listMessages.push(serviceMessage);
});
}
}
</script>
作为令牌,我有后端 json 文件的密钥。如您所见,现在我只尝试记录控制台消息。它正在做一些事情,因为我的错误消息来自那里。
问题
我哪里遗漏了这个功能?
目标
以备不时之需。我的目标是让所有 'public' 数据都成为 select,并在我的客户端中使用令牌,然后管理部分可能使用 0auth。所以一般 'SELECT' 东西是通过令牌来保护的,而不是根本没有身份验证。
解决方案
好的,我解决了,有点。我首先需要创建一个用户。然后我需要与用户进行本地登录。那returns一个令牌。如果我用那个,那就完全没有问题了。
要使用令牌,您必须首先确保它已生成。我使用密钥作为令牌是不正确的。当您第一次使用 'local' 类型(默认电子邮件和密码)进行验证时,它将创建一个令牌,然后您可以将其与方法 'token'
一起使用
我有一个 feathters.js 应用程序,现在我想保护创建和更新挂钩。我使用 socket.io 客户端,目前正在使用 JWT。我已经添加了我认为需要添加的内容,但得到的是 Error: Authentication token missing
和 Error Authenticating
。后来我从我的代码中了解到这一点。我有后端/前端情况
这就是我到目前为止所实施的。
文件:backend\backend.js(在backend\index.js中调用以配置应用程序)
'use strict';
const path = require('path');
const serveStatic = require('feathers').static;
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const feathers = require('feathers');
const configuration = require('feathers-configuration');
const authentication = require('feathers-authentication');
const hooks = require('feathers-hooks');
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
const socketio = require('feathers-socketio');
const middleware = require('./middleware/index');
const services = require('./services/index');
const appFeathers = feathers();
appFeathers.configure(configuration(path.join(__dirname, '..')));
appFeathers.use(compress())
.options('*', cors())
.use(cors())
.use(favicon(path.join(appFeathers.get('public'), 'favicon.ico')))
.use('/', serveStatic(appFeathers.get('public')))
.use(bodyParser.json())
.use(bodyParser.urlencoded({extended: true}))
.configure(hooks())
.configure(rest())
.configure(socketio())
.configure(services)
.configure(middleware)
.configure(authentication());
module.exports = appFeathers;
文件:backend\config\default.json
{
"host": "localhost",
"port": 3001,
"mysql_connection": "mysql://CONNECTION_STRING",
"public": "../public/",
"auth": {
"idField": "id",
"token": {
"secret": "SECRET_KEY"
},
"local": {}
}
}
在前端的工作组件中:
<template>
<div class="vttIndex">
idnex.vue
todo: eagle.js slideshow
todo: first info
<ul>
<li v-for="message in listMessages">
{{ message }}
</li>
</ul>
</div>
</template>
<script>
import feathers from 'feathers/client';
import socketio from 'feathers-socketio/client';
import hooks from 'feathers-hooks';
import io from 'socket.io-client';
import authentication from 'feathers-authentication/client';
import * as process from "../nuxt.config";
const vttSocket = io(process.env.backendUrl);
const vttFeathers = feathers()
.configure(socketio(vttSocket))
.configure(hooks())
.configure(authentication());
const serviceMessage = vttFeathers.service('messages');
vttFeathers.authenticate({
type: 'token',
'token ': 'SECRET_KEY'
}).then(function(result){
console.log('Authenticated!', result);
}).catch(function(error){
console.error('Error authenticating!', error);
});
export default {
layout: 'default',
data: function() {
return {
listMessages: []
}
},
mounted: function() {
serviceMessage.find().then(page => {
this.listMessages = page.data;
});
serviceMessage.on('created', (serviceMessage) => {
this.listMessages.push(serviceMessage);
});
}
}
</script>
作为令牌,我有后端 json 文件的密钥。如您所见,现在我只尝试记录控制台消息。它正在做一些事情,因为我的错误消息来自那里。
问题
我哪里遗漏了这个功能?
目标
以备不时之需。我的目标是让所有 'public' 数据都成为 select,并在我的客户端中使用令牌,然后管理部分可能使用 0auth。所以一般 'SELECT' 东西是通过令牌来保护的,而不是根本没有身份验证。
解决方案
好的,我解决了,有点。我首先需要创建一个用户。然后我需要与用户进行本地登录。那returns一个令牌。如果我用那个,那就完全没有问题了。
要使用令牌,您必须首先确保它已生成。我使用密钥作为令牌是不正确的。当您第一次使用 'local' 类型(默认电子邮件和密码)进行验证时,它将创建一个令牌,然后您可以将其与方法 'token'
一起使用