Javascript 如何使用Vue.js将用户登录到spring安全应用程序?
嗨,这是我关于堆栈溢出的第一篇文章。我想将后端和前端分开,并使用我的SpringBoot应用程序创建RESTAPI。我使用spring安全性登录。它使用了一种叫做JSESSION_ID的东西,但是如何使用Vue.js登录和授权用户呢?我想制作一个表单,用于登录到我的服务并使用JSESSION_ID授权请求 这是我在Vue.js中的第一个应用程序。我尝试使用教程和文档中的代码,但这并没有解决我的问题。我对CORS也有意见Javascript 如何使用Vue.js将用户登录到spring安全应用程序?,javascript,java,spring,vue.js,vuejs2,Javascript,Java,Spring,Vue.js,Vuejs2,嗨,这是我关于堆栈溢出的第一篇文章。我想将后端和前端分开,并使用我的SpringBoot应用程序创建RESTAPI。我使用spring安全性登录。它使用了一种叫做JSESSION_ID的东西,但是如何使用Vue.js登录和授权用户呢?我想制作一个表单,用于登录到我的服务并使用JSESSION_ID授权请求 这是我在Vue.js中的第一个应用程序。我尝试使用教程和文档中的代码,但这并没有解决我的问题。我对CORS也有意见 export default { name: 'Lo
export default {
name: 'Login',
data(){
return{
username: '',
password: '',
error: false
}
},
updated(){
if (localStorage.token){
this.$router.replace(this.$route.query.redirect || '/')
}
},
methods:{
login(){
this.$http.post('http://localhost:8080/login', { username: this.username, password: this.password })
.then(request => this.loginSuccessful(request))
.catch(() => this.loginFailed())
},
loginSuccessful (req) {
if (!req.data.token) {
this.loginFailed();
return
}
this.error = false;
localStorage.token = req.data.token;
this.$router.replace(this.$route.query.redirect || '/')
},
loginFailed () {
this.error = 'Login failed!';
delete localStorage.token
}
}
}
我希望使用REST Spring启动API从Vue.js登录并授权用户。问题在于,您试图将对象
{username:this.username,password:this.password}
作为数据传递给请求,但未指定内容类型,该类型可能默认为“text/html”或“application/json”。您可以尝试先将其转换为表单数据,然后将其传递给请求,以下是AXIOS的一个示例:
login() {
let formData = new FormData();
formData.set("username", this.username);
formData.set("password", this.password);
AXIOS.post('/login', formData, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
.then((result) => {
this.loginSuccessful(result);
})
.catch((error) => {
this.loginFailed();
})
}
也许它也可以在不指定头的情况下工作,比如
AXIOS.post('/login',formData,null)
,但还没有尝试过它。问题是,您试图将对象{username:this.username,password:this.password}
作为数据传递给请求,但没有指定内容类型,它可能默认为“text/html”或“application/json”。您可以尝试先将其转换为表单数据,然后将其传递给请求,以下是AXIOS的一个示例:
login() {
let formData = new FormData();
formData.set("username", this.username);
formData.set("password", this.password);
AXIOS.post('/login', formData, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
.then((result) => {
this.loginSuccessful(result);
})
.catch((error) => {
this.loginFailed();
})
}
也许它也可以在不指定头的情况下工作,比如
AXIOS.post('/login',formData,null)
,还没有尝试过。您的身份验证提供程序是什么?它是org.springframework.security.authentication中的默认身份验证提供程序。您的身份验证提供程序是什么?它是org.springframework.security.authentication中的默认身份验证提供程序