我是Web开发的新手,所以也许我已经走了。我在Intranet内,想对前端React应用程序使用Windows授权。我可以从浏览器URL访问端点,但是在我的React应用程序中出现未经授权的错误401。
// front end test code
var axios = require('axios')
var config = {
method:'get',
url:'someURLendPointThatWorks',
timeout: 1000,
withCredentials: true
};
axios(config)
.then(response => {console.log(response)})
.catch(error => {console.log(error)})
// express backend app.js
const express = require('express')
// file path functions
const path = require('path')
// cross origin resource sharing
const cors = require('cors')
// route imports
const ntRouter = require('./routes/nt')
const apiRouter = require('./routes/api')
var app = express()
// cross origin resource sharing
// https://daveceddia.com/access-control-allow-origin-cors-errors-in-react-express/
app.use(cors({
origin:'*',
credentials: true
}))
// Routes
app.use(ntRouter)
app.use('/api', apiRouter)
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`Listening on port ${port}`))
// routes/nt.js
const express = require('express')
const router = express.Router()
router.use((req, res, next) => {
var username = req.headers['x-iisnode-auth_user'];
var authenticationType = req.headers['x-iisnode-auth_type'];
req.NT = username
next()
})
module.exports = router
// routes/api.js
const express = require('express')
const router = express.Router()
// check to see if you're authorized, returns your NT
router.get('/', (req, res) => {
res.send(`Your NT is ${req.NT}`)
})
module.exports = router