我扩展了环回的用户模型,使其适用于我的自定义用户模型。这是用户模型(自定义模型)文件
{
"name": "user",
"base": "User",
"options": {
"idInjection": false,
"mysql": {
"schema": "BANNERG",
"table": "User"
}
},
"properties": {
"id": {
"type": "Number",
"required": true,
"length": null,
"precision": 10,
"scale": 0,
"id": 1,
"mysql": {
"columnName": "id",
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "N"
}
},
"accId": {
"type": "Number",
"required": false,
"length": null,
"precision": 10,
"scale": 0,
"mysql": {
"columnName": "acc_id",
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "Y"
}
},
"fname": {
"type": "String",
"required": false,
"length": 300,
"precision": null,
"scale": null,
"mysql": {
"columnName": "fname",
"dataType": "varchar",
"dataLength": 300,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"lname": {
"type": "String",
"required": false,
"length": 300,
"precision": null,
"scale": null,
"mysql": {
"columnName": "lname",
"dataType": "varchar",
"dataLength": 300,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"username": {
"type": "String",
"required": false,
"length": 300,
"precision": null,
"scale": null,
"mysql": {
"columnName": "username",
"dataType": "varchar",
"dataLength": 300,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"email": {
"type": "String",
"required": false,
"length": 250,
"precision": null,
"scale": null,
"mysql": {
"columnName": "email",
"dataType": "varchar",
"dataLength": 250,
"dataPrecision": null,
"dataScale": null,
"nullable": "N"
}
},
"phone": {
"type": "String",
"required": false,
"length": 100,
"precision": null,
"scale": null,
"mysql": {
"columnName": "phone",
"dataType": "varchar",
"dataLength": 100,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"status": {
"type": "Number",
"required": true,
"length": null,
"precision": 10,
"scale": 0,
"mysql": {
"columnName": "status",
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "Y"
}
},
"passwordHash": {
"type": "String",
"required": false,
"length": 300,
"precision": null,
"scale": null,
"mysql": {
"columnName": "password_hash",
"dataType": "varchar",
"dataLength": 300,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"salt": {
"type": "String",
"required": false,
"length": 300,
"precision": null,
"scale": null,
"mysql": {
"columnName": "salt",
"dataType": "varchar",
"dataLength": 300,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"tokenSalt": {
"type": "Number",
"required": false,
"length": null,
"precision": 10,
"scale": 0,
"mysql": {
"columnName": "token_salt",
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "Y"
}
},
"parentUserid": {
"type": "Number",
"required": false,
"length": null,
"precision": 10,
"scale": 0,
"mysql": {
"columnName": "parent_userid",
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "Y"
}
},
"lastLogginTime": {
"type": "Date",
"required": false,
"length": null,
"precision": null,
"scale": null,
"mysql": {
"columnName": "last_loggin_time",
"dataType": "timestamp",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"passwordChange": {
"type": "String",
"required": false,
"length": 20,
"precision": null,
"scale": null,
"mysql": {
"columnName": "password_change",
"dataType": "varchar",
"dataLength": 20,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"userAccountAccess": {
"type": "String",
"required": false,
"length": 2000,
"precision": null,
"scale": null,
"mysql": {
"columnName": "user_account_access",
"dataType": "varchar",
"dataLength": 2000,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"createTs": {
"type": "Date",
"required": false,
"length": null,
"precision": null,
"scale": null,
"mysql": {
"columnName": "create_ts",
"dataType": "timestamp",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"updateTs": {
"type": "Date",
"required": false,
"length": null,
"precision": null,
"scale": null,
"mysql": {
"columnName": "update_ts",
"dataType": "timestamp",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"createUser": {
"type": "Number",
"required": false,
"length": null,
"precision": 10,
"scale": 0,
"mysql": {
"columnName": "create_user",
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "Y"
}
},
"updateUser": {
"type": "Number",
"required": false,
"length": null,
"precision": 10,
"scale": 0,
"mysql": {
"columnName": "update_user",
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "Y"
}
},
"password": {
"type": "String",
"required": false,
"length": 45,
"precision": null,
"scale": null,
"mysql": {
"columnName": "password",
"dataType": "varchar",
"dataLength": 45,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"realm": {
"type": "String",
"required": false,
"length": 45,
"precision": null,
"scale": null,
"mysql": {
"columnName": "realm",
"dataType": "varchar",
"dataLength": 45,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"verificationtoken": {
"type": "String",
"required": false,
"length": 45,
"precision": null,
"scale": null,
"mysql": {
"columnName": "verificationToken",
"dataType": "varchar",
"dataLength": 45,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
}
},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"accessType": "*"
}
]
}

在我的sql数据库中,用户名设置为loopback,密码设置为loopback。
但是当我尝试使用这个json凭证登录时。
我收到此错误堆栈
{
"error": {
"statusCode": 401,
"name": "Error",
"message": "login failed",
"code": "LOGIN_FAILED",
"stack": "Error: login failed\n at /home/rahul/Loopback/Helloworld/node_modules/loopback/common/models/user.js:249:26\n at /home/rahul/Loopback/Helloworld/node_modules/loopback-datasource-juggler/lib/dao.js:2168:62\n at allCb (/home/rahul/Loopback/Helloworld/node_modules/loopback-datasource-juggler/lib/dao.js:2100:7)\n at /home/rahul/Loopback/Helloworld/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:1196:14\n at cbForWork (/home/rahul/Loopback/Helloworld/node_modules/loopback-datasource-juggler/lib/observer.js:234:34)\n at /home/rahul/Loopback/Helloworld/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:428:7\n at handleResponse (/home/rahul/Loopback/Helloworld/node_modules/loopback-connector-mysql/lib/mysql.js:182:17)\n at Query._callback (/home/rahul/Loopback/Helloworld/node_modules/loopback-connector-mysql/lib/mysql.js:193:7)\n at Query.Sequence.end (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)\n at Query.ErrorPacket (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/sequences/Query.js:90:8)\n at Protocol._parsePacket (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/Protocol.js:279:23)\n at Parser.write (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/Parser.js:76:12)\n at Protocol.write (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/Protocol.js:39:16)\n at Socket.<anonymous> (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/Connection.js:103:28)\n at emitOne (events.js:96:13)\n at Socket.emit (events.js:191:7)"
}
}
答案 0 :(得分:0)
它的解决方案非常简单。只需将自定义模型重命名为&#34; CustomUser&#34;或者你喜欢的其他名字,你就完成了。我自己试了一下。 供参考:https://github.com/strongloop/loopback-example-access-control/issues/73