【发布时间】:2020-03-05 07:02:56
【问题描述】:
在提问之前,请注意,我已经尝试了许多来自 stackoverflow 以及许多其他网站的建议。有很多建议,但大多数都没有直接解决这个问题。
我的问题简单的一句话是,如何让我的网络服务器(这是一个基于 node js express 的 api)知道在内网应用程序中记录的 windows 用户 id(来自我的 react js 应用程序)?
到目前为止,我能够编写基于 express 的 node js 服务器,它使用 node-sspi 库并为我提供 Windows 用户 ID 和组。 这个API很简单
var express = require('express');
var app = express();
var server = require('http').createServer(app);
let cors = require('cors')
app.use(cors())
app.use(function (req, res, next) {
var nodeSSPI = require('node-sspi');
var nodeSSPIObj = new nodeSSPI({
retrieveGroups: true
});
nodeSSPIObj.authenticate(req, res, function(err){
res.finished || next();
});
});
app.get('*', function (req, res, next) {
res.json({msg: '************ Server reply by user ' + req.connection.user})
});
// Start server
var port = process.env.PORT || 3002;
server.listen(port, function () {
console.log('Express server listening on port %d in %s mode', port, app.get('env'));
});
React App.js 代码是
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
msg: "Not known",
localdata:"local status",
status:"not set",
};
}
componentDidMount()
{
fetch('http://192.168.7.179:3002/',{
method: 'GET', // *GET, POST, PUT, DELETE, etc.
headers: {
'Content-Type': 'application/json',
},
})
.then(response =>
{
this.state.status = response.status;
return response.text();
}
)
.then(msg =>
this.setState({ msg })
);
}
render() {
return (
<div className="App">
<div> LocalData is - {this.state.localdata}</div>
<div> Server data is - {this.state.msg} </div>
api call status is - { this.state.status }
</div>
);
}
}
export default App;
在上面的代码中,如果你从浏览器调用 API,你会得到正确的回复
http://192.168.7.179:3002/
{"msg":"************ Server reply by user IUYT\\user1"}
但是,来自 react 应用
LocalData is - local status
Server data is -
api call status is - 401
在 F12 窗口中
Failed to load resource: the server responded with a status of 401 (Unauthorized)
请建议从 React 应用调用所需的更改。
【问题讨论】: