【问题标题】:Reactjs Axios / Spring boot securityReactjs Axios / Spring Boot 安全性
【发布时间】:2016-08-18 22:22:35
【问题描述】:

我有一个使用 Spring Boot 开发的 Java 应用程序,它是后端。 前端是用 ReactJs 开发的应用程序。我使用 REST 服务。 我使用 axios 进行 REST 调用。 我最近在 Spring Boot 中启用了安全性。现在我很难对 axios 调用进行身份验证。

var config = {
        auth: {
            username: 'bruker',
            password: 'passord'
        }

    };
    axios.get('http://localhost:8090/employee/all', config).then(function (response) {
        console.log(response)
    }.bind(this)).catch(function (response) {
        console.log(response)
    }.bind(this))

我收到以下错误“预检响应无效(重定向)” 我假设响应被重定向到localhost:8090/login 我还没有找到任何解决方案。我做错了什么?

【问题讨论】:

    标签: reactjs spring-security spring-boot axios


    【解决方案1】:

    这篇文章现在已经过时了,但我在 2018 年的 ole 中遇到了类似的问题。按以下方式使用 Axios 对我来说很有效:

        axios('/user', {
            method: 'POST',
            auth: {
                username: myUser,
                password: myPassword
            }
        }).then((response => {
            ...
        })).catch((error) => {
            ...
        })
    

    请注意,区别在于将axios.get(... 替换为axios(...。 将方法类型作为函数删除,并将其作为配置选项包含在内。这可能与导入 axios (import axios from 'axios') 的方式有关,但一旦我的东西开始工作,我就没有深入研究。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 1970-01-01
      • 2021-07-14
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      相关资源
      最近更新 更多