Vue前后端分离实现登录的一个简单demo
2024-09-08 18:30:29
1、建立一个Maven项目,并添加Spring相关依赖
2、编写Controller类相应的接口和配置类
LoginController类,编写接口的业务逻辑
package com.springboot.controller;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/login")
public class LoginController {
@RequestMapping(value = {"/login_submit"}, method = RequestMethod.POST)
public boolean login(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("abc") && password.equals("123456")) {
return true;
}
return false;
}
}
CorsConfig类,编写配置类用来解决跨域请求(接口是http,而axios一般请求的是https。从https到http是跨域,因此要配置跨域请求)
package com.springboot.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* 解决跨域问题
*/
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); //允许任何域名
corsConfiguration.addAllowedHeader("*"); //允许任何头
corsConfiguration.addAllowedMethod("*"); //允许任何方法
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); //注册
return new CorsFilter(source);
}
}
编写html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 官网提供的 axios 在线地址 -->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.20.0-0/axios.min.js"></script>
<div id="app">
<input type="text" placeholder="请输入用户名" v-model="username" />
<br>
<input type="password" placeholder="请输入密码" v-model="password" />
<input type="button" v-on:click="login" value="登录" />
{{username}}
{{password}}
</div>
<script>
var app = new Vue({
el: "#app",
data: {
username: "",
password: ""
},
methods: {
login: function() {
let param = new URLSearchParams()
param.append('username', this.username)
param.append('password', this.password)
/*axios使用POST请求传参到接口http://localhost:8082/login/login_submit*/
axios({
method: 'post',
url: 'http://localhost:8082/login/login_submit',
data: param
}).then(
function(response) { //获取接口http://localhost:8082/login/login_submit的返回值
if (response.data == true) {
alert("登录成功");
} else {
alert("登录失败,用户名密码错误");
}
console.log(response);
},
function(err) {
alert("登录失败,用户名密码错误");
}
);
}
}
})
</script>
</body>
</html>
在Intellij Idea中启动Maven项目(即启动接口)
在HBuilder中启动html项目
最新文章
- JavaScript多线程之HTML5 Web Worker
- 安装 SSL 证书
- jsp_属性范围_page
- How to programmatically new a java class which implements sepecified interface in eclipse plugin development
- 8个免费实用的C++GUI库(转载)
- 如何在.NET MVC中使用jQuery并返回JSON数据
- centos系统常用软件环境搭建
- Uber将在泰国首推";优步摩托";服务
- Java实现查看当前目录下的文件
- RxJS速成 (下)
- width:100vh有感而发
- To making it count.
- 实现全选、全不选功能 JQuery
- A1114. Family Property
- win7频繁提示资源管理器已停止工作解决办法
- 移动IP 它最初设想每个人都在编写应用层(7)API而不是传输层(4)API 对于QUIC,连接的标识符不是“套接字”(源/目标端口/地址协议组合)的传统概念,而是分配给连接的64位标识符
- UI5-文档-4.22-Expression Binding
- DateReader读取数据
- Are you sure your NDK_MODULE_PATH variable is properly defined?(2)
- mysql数据库从删库到跑路之mysql库操作
热门文章
- 220722 T1 分树 (模拟)
- windows下利用_popen,_wpoen创建管道进行系统命令输出数据
- 华为设备配置Stelnet命令
- selenium4-定位单个页面元素
- 【原创】All in One i.MXRT1050/RT1020 SPI Flash Algorithm for J-Flash
- Ubuntu安装Docker及镜像加速器
- 【单元测试】Junit 4(四)--Junit4参数化
- Flask框架:运用Ajax轮询动态绘图
- springcloud组件梳理之hystrix
- Idea在windows和mac中的一些快捷指令