Reactjs Axios/Spring启动安全性 [英] Reactjs Axios / Spring boot security

查看:64
本文介绍了Reactjs Axios/Spring启动安全性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Java application developed with Spring Boot which is the back-end. The front-end is an application developed in ReactJs. I use REST services. I use axios for REST calls. I recently enabled the security in Spring Boot. Now I have difficulty to authenticate axios calls.

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 我还没有找到任何解决方案.我做错了什么?

I get the following error "Response for preflight is invalid (redirect)" I assume the response is a redirected to localhost:8090/login I haven't found any solutions to this. What do I do wrong?

推荐答案

这篇文章现在已经很旧了,但是我在'ole 2018好的时候在这里遇到了类似的问题.如下使用Axios可以为我工作:

This post is old now, but I ran into a similar problem here in good 'ole 2018. Using Axios as follows got things working for me:

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

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

Notice that the difference is replacing axios.get(... with axios(.... Remove the method type as a function and include it as a config option. It could have something to do with the way axios is imported (import axios from 'axios'), but I didn't delve into that once I got my stuff working.

希望有帮助!

这篇关于Reactjs Axios/Spring启动安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆