首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有消息“Error”和->的feathersjs ->错误401和NotAuthenticated

带有消息“Error”和->的feathersjs ->错误401和NotAuthenticated
EN

Stack Overflow用户
提问于 2017-06-12 13:02:27
回答 1查看 2K关注 0票数 1

我现在想为我的错误做一堆。我有一些问题,启动身份验证,但这是为我的工作项目是一个不同的版本。另外,在使用不同的服务和列命名约定(然后是默认的)时,我也遇到了问题。然后它失败了,因为我解决了'FETCH‘NEXT’的后缀和mssql。

环境

我正在一个linux系统上开发。正在使用的数据库目前在SQL2016上。所有的选择都很好,在我启用身份验证之前,我在表中进行了插入/更新。服务器和客户端的版本

代码语言:javascript
复制
Server
    "feathers": "^2.1.1",
    "feathers-authentication": "^1.2.1",
    "feathers-authentication-jwt": "^0.3.1",
    "feathers-authentication-local": "^0.3.4",
    "feathers-configuration": "^0.3.3",
    "feathers-errors": "^2.6.2",
    "feathers-hooks": "^1.8.1",
    "feathers-rest": "^1.7.1",
    "feathers-sequelize": "^1.4.5",
    "feathers-socketio": "^1.5.2",

Client
    "feathers": "^2.1.2",
    "feathers-authentication": "^1.2.4",
    "feathers-authentication-client": "^0.3.2",
    "feathers-client": "^2.2.0",
    "feathers-localstorage": "^1.0.0",
    "feathers-socketio": "^2.0.0",

问题

当我在客户端上启动身份验证(它被设置为策略本地)时,我会得到下面的错误,同时我希望对用户进行“身份验证”,并且passord是正确的。

误差

代码语言:javascript
复制
  Error authenticating! { type: 'FeathersError',
  name: 'NotAuthenticated',
  message: 'Error',
  code: 401,
  className: 'not-authenticated',
  errors: {} }

所以有些文件是不必要的。首先,让我们从后端开始。我有几个“集群”服务,所以有些代码可能需要转换。

档案:./app.js

这是主要的应用程序文件。在这里,您还将看到我的用户是如何创建的,我正在使用它进行测试。

代码语言:javascript
复制
'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 jwt = require('feathers-authentication-jwt');
const local = require('feathers-authentication-local');
const hooks = require('feathers-hooks');
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
const socketio = require('feathers-socketio');
const middleware = require('./middleware');
const servicesMfp = require('./services/A');
const servicesMic = require('./services/B');

const app = feathers();

app.configure(configuration(path.join(__dirname, '..')));

app.use(compress())
    .options('*', cors())
    .use(cors())
    .use(favicon(path.join(app.get('public'), 'favicon.ico')))
    .use('/', serveStatic(app.get('public')))
    .use(bodyParser.json())
    .use(bodyParser.urlencoded({extended: true}))
    .configure(hooks())
    .configure(rest())
    .configure(socketio())
    .configure(servicesMfp)
    .configure(servicesMic)
    .configure(middleware)
    .configure(local({
        usernameField: 'user_name',
        passwordField: 'user_password'
    }))
    .configure(jwt());

app.service('/mfp/authentication').hooks({
    before: {
        create: [
            authentication.hooks.authenticate(['jwt', 'local']),
        ],
        remove: [
            authentication.hooks.authenticate('local')
        ]
    }
});

/*
const userService = app.service('/mfp/sys_users');
const User = {
    user_email: 'ekoster3@mail.com',
    user_password: 'ekoster',
    user_name: 'ekoster2',
    status_id: 1
};
userService.create(User, {}).then(function(user) {
    console.log('Created default user', user);
});
*/

module.exports = app;

文件:./services/multifunctionalportal/authentiction/index.js

代码语言:javascript
复制
'use strict';

const authentication = require('feathers-authentication');

module.exports = function () {
    const app = this;
    let config = app.get('mfp_auth');

    app.configure(authentication(config));
};

文件:./services/multifunctionalportal/sys_user/index.js

这是服务的索引文件。这也是真正为该数据库中的数据配置身份验证的地方。

代码语言:javascript
复制
'use strict';
const authentication = require('./authentication/index');
const sys_user = require('./sys_user/index');
const sys_metadata = require('./sys_metadata/index');
const sys_term = require('./sys_term');
const sys_source = require('./sys_source/index');
const sys_source_user = require('./sys_source_user/index');
const survey = require('./survey/index');
const survey_question = require('./survey_question/index');
const Sequelize = require('sequelize');

module.exports = function () {
    const app = this;

    //TODO make it more cross DB (different dbtypes)
    const sequelize = new Sequelize(app.get('mfp_db_database'), app.get('mfp_db_user'), app.get('mfp_db_password'), {
        host: app.get('mfp_db_host'),
        port: app.get('mfp_db_port'),
        dialect: 'mssql',
        logging: true,
        dialectOptions: {
            instanceName: app.get('mfp_db_instance')
        }
    });
    app.set('mfp_sequelize', sequelize);

    app.configure(authentication);
    app.configure(sys_user);
    app.configure(sys_metadata);
    app.configure(sys_term);
    app.configure(sys_source);
    app.configure(sys_source_user);
    app.configure(survey);
    app.configure(survey_question);

    Object.keys(sequelize.models).forEach(function(modelName) {
        if ("associate" in sequelize.models[modelName]) {
            sequelize.models[modelName].associate();
        }
    });

    sequelize.sync(
        {
            force: false
        }
    );
};

上述文件中使用的配置如下

代码语言:javascript
复制
"mfp_auth": {
        "path": "/mfp/authentication",
        "service": "/mfp/sys_users",
        "entity": "sys_user",
        "strategies": [
            "local",
            "jwt"
        ],
        "secret": "WHO_KNOWS"
    }

文件:./services/multifunctionalportal/sys_user/sys_user-model.js

代码语言:javascript
复制
'use strict';

const Sequelize = require('sequelize');

module.exports = function (sequelize) {
    const sys_user = sequelize.define('sys_users', {
        user_email: {
            type: Sequelize.STRING(256),
            allowNull: false,
            unique: true
        },
        user_name: {
            type: Sequelize.STRING(256),
            allowNull: false,
            unique: true
        },
        user_password: {
            type: Sequelize.STRING(256),
            allowNull: false
        },
        status_id: {
            type: Sequelize.INTEGER,
            allowNull: false
        }
    }, {
        freezeTableName: true,
        paranoid: true,
        timestamps  : true,
        underscored : true,
        classMethods: {
            associate() {
                sys_user.belongsTo(sequelize.models.sys_metadata, {
                    allowNull: false,
                    as: 'status'
                });
                sys_user.hasMany(sequelize.models.sys_source_users, {
                    as: 'user',
                    foreignKey: 'user_id',
                    targetKey: 'user_id',
                    onDelete: 'no action'
                });
            }
        }
    });

    sys_user.sync();

    return sys_user;
};

文件:./services/multifunctionalportal/sys_user/hooks/index.js

代码语言:javascript
复制
'use strict';

const globalHooks = require('../../../../hooks');
const hooks = require('feathers-hooks');
const authentication = require('feathers-authentication');
const local = require('feathers-authentication-local');

exports.before = {
    all: [],
    find: [
        authentication.hooks.authenticate('jwt')
    ],
    get: [],
    create: [
        local.hooks.hashPassword({ passwordField: 'user_password' })
    ],
    update: [],
    patch: [],
    remove: []
};

exports.after = {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
};

下一个过场是客户。我有nuxtjs,但我也有一个客户端,它不是nuxtjs,也有相同的问题。所以我把它放在这里,因为它是一个文件,更容易调试。

代码语言:javascript
复制
'use strict';
const feathers = require('feathers/client');
const socketio = require('feathers-socketio/client');
const hooks = require('feathers-hooks');
const io = require('socket.io-client');
const authentication = require('feathers-authentication-client');
const localstorage = require('feathers-localstorage');
const process = require('../../config');
const winston = require('winston');
const tslog = () => (new Date()).toLocaleTimeString();

const mfpSocket = io(process.env.backendUrl);
const mfpFeathers = feathers()
    .configure(socketio(mfpSocket))
    .configure(hooks())
    .configure(authentication());

const surveyLog = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({
            timestamp: tslog,
            colorize: true
        }),
        new (require('winston-daily-rotate-file'))({
            filename: process.env.logDirectory + '/-' + process.env.logFileSurvey,
            timestamp: tslog,
            datePattern: 'yyyyMMdd',
            prepend: true,
            level: process.env.logLevelSurvey
        })
    ]
});


//TODO login then continue
mfpFeathers.authenticate({
    strategy: 'local',
    'user_name': 'ekoster',
    'user_password': 'ekoster2'
}).then(function(result){
    console.log('Authenticated!', result);
}).catch(function(error){
    console.error('Error authenticating!', error);
});

如果需要的话,我可以扩展这段代码,因为我删除了本节下面的内容,这些内容无助于解决(无关)。

请求

有没有可能有人能给我指明正确的方向。我还需要在其他地方配置自定义字段吗?我试着搜索这个问题,看看我是否能在“错误”中添加一些东西:但我只发现它似乎来自“羽毛-认证”中的两个文件,但我不知道在哪里。

求解

我认为问题在于服务器设置的一部分在服务的“index.js”中,而在后端的“app.js”中。只是我还不知道在哪。

20170612 1630年新文件我对一些文件做了一些调整。同样的结果,但更适合。不过,下一步似乎并没有被称为。

文件: app.js

代码语言:javascript
复制
'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 hooks = require('feathers-hooks');
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
const socketio = require('feathers-socketio');
const middleware = require('./middleware');
const servicesMfp = require('./services/multifunctionalportal');
const servicesMic = require('./services/mexonincontrol');

const app = feathers();

app.configure(configuration(path.join(__dirname, '..')));

app.use(compress())
    .options('*', cors())
    .use(cors())
    .use(favicon(path.join(app.get('public'), 'favicon.ico')))
    .use('/', serveStatic(app.get('public')))
    .use(bodyParser.json())
    .use(bodyParser.urlencoded({extended: true}))
    .configure(hooks())
    .configure(rest())
    .configure(socketio())
    .configure(servicesMfp)
    .configure(servicesMic)
    .configure(middleware);

/*
const userService = app.service('/mfp/sys_users');
const User = {
    user_email: 'ekoster3@mexontechnology.com',
    user_password: 'ekoster',
    user_name: 'ekoster2',
    status_id: 1
};
userService.create(User, {}).then(function(user) {
    console.log('Created default user', user);
});
*/

module.exports = app;

文件:./services/多功能门户/index.js

代码语言:javascript
复制
'use strict';
const authentication = require('./authentication/index');
const jwt = require('feathers-authentication-jwt');
const local = require('feathers-authentication-local');
const sys_user = require('./sys_user/index');
const sys_metadata = require('./sys_metadata/index');
const sys_term = require('./sys_term');
const sys_source = require('./sys_source/index');
const sys_source_user = require('./sys_source_user/index');
const survey = require('./survey/index');
const survey_question = require('./survey_question/index');
const Sequelize = require('sequelize');

module.exports = function () {
    const app = this;

    //TODO make it more cross DB (different dbtypes)
    const sequelize = new Sequelize(app.get('mfp_db_database'), app.get('mfp_db_user'), app.get('mfp_db_password'), {
        host: app.get('mfp_db_host'),
        port: app.get('mfp_db_port'),
        dialect: 'mssql',
        logging: true,
        dialectOptions: {
            instanceName: app.get('mfp_db_instance')
        }
    });
    app.set('mfp_sequelize', sequelize);

    app.configure(authentication);
    app.configure(local({
        usernameField: 'user_name',
        passwordField: 'user_password'
    }));
    app.configure(jwt());
    app.configure(sys_user);
    app.configure(sys_metadata);
    app.configure(sys_term);
    app.configure(sys_source);
    app.configure(sys_source_user);
    app.configure(survey);
    app.configure(survey_question);

    Object.keys(sequelize.models).forEach(function(modelName) {
        if ("associate" in sequelize.models[modelName]) {
            sequelize.models[modelName].associate();
        }
    });

    sequelize.sync(
        {
            force: false
        }
    );
};

文件:./services/multifunctionalportal/authentication/index.js

代码语言:javascript
复制
'use strict';
const authentication = require('feathers-authentication');

module.exports = function () {
    const app = this;
    const config = app.get('mfp_auth');
    const authService = app.service('/mfp/authentication');

    app.configure(authentication(config));

    authService.before({
        create: [
            authentication.hooks.authenticate(['jwt', 'local']),
        ],
        remove: [
            authentication.hooks.authenticate('local')
        ]
    });
};

20170612 16:45“要求”中的更改更改错误

我已经将'./services/multifunctionalportal/index.js‘中的身份验证要求从" require (./ authentication /index)“更改为”require(’for authentication‘)“,现在它给出了关于找不到app.passport的错误。如果在身份验证之前配置了身份验证,那么它就是本地的。

20170612 19:00移动配置以进行身份验证

因此,我在“多功能门户/身份验证”服务的“index.js”中设置了诱惑。我把它移到了它自己的服务的“index.js”中,现在消息已经没有了,但是现在我有了一个没有用户的令牌。因此,如果我输入了错误的密码,它仍然在创建一个令牌。如果您查看后端日志,则不会出现用户选择。

20170612 20:00在循环中

最后一个变化是由钩子丢失引起的。当前用于身份验证的钩子位于身份验证服务的index.js中。如果我将它们移到app.js中,那么问题就消失了,并且消息没有身份验证返回。因此,似乎某种配置是不正确的。当前是否可以在初始错误的“消息”部分提示错误消息

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-13 10:20:27

这里的解决方案是,测试用户的插入序列是'user_password‘'user_name’,登录测试是用'user_name‘'user_password’进行的,在用户/密码相等的新用户中,我解决了这个问题。当这个用户开始工作时,我想出了办法。

错误并不是说登录失败是因为密码不正确,但是当您执行DEBUG=feathers-authentication* npm start时,它确实会显示它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44500186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档