当前位置:   article > 正文

基于Vue和Element UI实现前后端分离和交互_vue-element-admin 前后端交互

vue-element-admin 前后端交互

目录

前言

一、Element UI简介

1.Element UI是什么

2.Element UI的特点

二、项目搭建

1.创建一个SPA项目

2.安装 Element-UI

3.导入组件

4.创建登陆注册界面

登录组件---Login.vue

注册组件---Register.vue

定义组件与路由的对应关系

效果演示: 

三、前后端数据交互

1.安装axios

2.导入api模块,添加axios的全局配置

3.修改main.js配置vue-axios

4.数据表数据

5.编写后端登录注册controller层

6.使用封装后的axios发送请求

7.测试

四、CORS跨域

1.什么是CORS?

2.为什么会出现CORS问题?

3.特点

4.如何解决CORS问题?

5.跨域问题处理


前言

        当今的Web开发趋势中,前后端分离已经成为一种流行的架构模式。它将前端和后端的开发分离开来,使得前端和后端可以独立进行开发和部署。Vue.js作为一种流行的前端框架,结合Element UI这个优秀的UI组件库,可以帮助我们更好地实现前后端分离和交互。

一、Element UI简介

1.Element UI是什么

        Element UI是一套基于Vue.js的开源UI组件库。它提供了丰富的可复用的UI组件,包括按钮、表单、弹窗、菜单等,可以帮助开发者快速构建现代化的Web应用程序界面。Element UI具有简洁美观的设计风格,易于使用和定制。它还提供了丰富的文档和示例,方便开发者学习和使用。无论是初学者还是有经验的开发者,都可以通过Element UI来提高开发效率,创建出优秀的用户界面。

2.Element UI的特点

1. 响应式布局:Element UI的组件可以根据不同的屏幕尺寸自动调整布局,适应不同的设备。

2. 高度可定制:开发者可以根据自己的需求对组件进行定制,包括样式、功能和交互等方面。

3. 丰富的文档和示例:Element UI提供了详细的文档和示例,方便开发者学习和使用。

4. 国际化支持:Element UI支持多种语言,可以轻松实现国际化的Web应用程序。

二、项目搭建

1.创建一个SPA项目

可观看下面这篇博文

​​​​​​Vue安装并使用Vue-CLI构建SPA项目并实现路由icon-default.png?t=N7T8https://blog.csdn.net/weixin_74268571/article/details/133137962?spm=1001.2014.3001.5502

创建好之后运行项目

在SAP项目的根目录输入cmd后在cmd窗口输入npm run dev
 

2.安装 Element-UI

在安装前需先关停我们的项目,在cmd窗口中ctrl+c,ctrl+c就可以了

 接下来就可以通过命令去下载Element UI

  1. cd my-spa #进入新建项目的根目录
  2. npm install element-ui -S #安装 Element UI模块

需进入该SPA项目根目录安装Element UI模块

下载后就可以看到该依赖了 

注:

在Vue中,使用命令行工具(如npm或yarn)下载依赖时,可以使用不同的选项来指定依赖的安装方式:

  • -g:全局安装。使用该选项下载的依赖将被安装在全局环境中,可以在任何项目中使用。通常用于安装一些全局的工具或命令行工具,比如Vue CLI。

总结起来,-g用于全局安装工具或命令行工具,-s用于保存项目运行所必需的依赖,-d用于保存开发过程中需要的依赖。

3.导入组件

在main.js中导入Element UI对应组件并将Element UI挂载在Vue中

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. // 新添加1
  5. import ElementUI from 'element-ui'
  6. // 新添加2,避免后期打包样式不同,要放在import App from './App';之前
  7. import 'element-ui/lib/theme-chalk/index.css'
  8. import App from './App'
  9. import router from './router'
  10. // 新添加3
  11. Vue.use(ElementUI)
  12. Vue.config.productionTip = false
  13. /* eslint-disable no-new */
  14. new Vue({
  15. el: '#app',
  16. router,
  17. components: { App },
  18. template: '<App/>'
  19. })

4.创建登陆注册界面

  • 登录组件---Login.vue

  1. <template>
  2. <div class="login-wrap">
  3. <el-form class="login-container">
  4. <h1 class="title">用户登录</h1>
  5. <el-form-item label="">
  6. <el-input type="text" v-model="username" placeholder="登录账号" autocomplete="off"></el-input>
  7. </el-form-item>
  8. <el-form-item label="">
  9. <el-input type="password" v-model="password" placeholder="登录密码" autocomplete="off"></el-input>
  10. </el-form-item>
  11. <el-form-item>
  12. <el-button type="primary" style="width:100%;" @click="doSubmit()">提交</el-button>
  13. </el-form-item>
  14. <el-row style="text-align: center;margin-top:-10px">
  15. <el-link type="primary">忘记密码</el-link>
  16. <el-link type="primary" @click="gotoRegister()">用户注册</el-link>
  17. </el-row>
  18. </el-form>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'Login',
  24. data () {
  25. return {
  26. username : '',
  27. password:''
  28. }
  29. },
  30. methods:{
  31. gotoRegister(){
  32. //跳转
  33. this.$router.push('/Register');
  34. }
  35. }
  36. }
  37. </script>
  38. <style scoped>
  39. .login-wrap {
  40. box-sizing: border-box;
  41. width: 100%;
  42. height: 100%;
  43. padding-top: 10%;
  44. background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+Cjxzdmcgd2lkdGg9IjEzNjFweCIgaGVpZ2h0PSI2MDlweCIgdmlld0JveD0iMCAwIDEzNjEgNjA5IiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPgogICAgPCEtLSBHZW5lcmF0b3I6IFNrZXRjaCA0Ni4yICg0NDQ5NikgLSBodHRwOi8vd3d3LmJvaGVtaWFuY29kaW5nLmNvbS9za2V0Y2ggLS0+CiAgICA8dGl0bGU+R3JvdXAgMjE8L3RpdGxlPgogICAgPGRlc2M+Q3JlYXRlZCB3aXRoIFNrZXRjaC48L2Rlc2M+CiAgICA8ZGVmcz48L2RlZnM+CiAgICA8ZyBpZD0iQW50LURlc2lnbi1Qcm8tMy4wIiBzdHJva2U9Im5vbmUiIHN0cm9rZS13aWR0aD0iMSIgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj4KICAgICAgICA8ZyBpZD0i6LSm5oi35a+G56CB55m75b2VLeagoemqjCIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTc5LjAwMDAwMCwgLTgyLjAwMDAwMCkiPgogICAgICAgICAgICA8ZyBpZD0iR3JvdXAtMjEiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDc3LjAwMDAwMCwgNzMuMDAwMDA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TQiIGZpbGwtb3BhY2l0eT0iMC42IiBmaWxsPSIjRDFERUVEIiBjeD0iMjQzLjgzMzMzMyIgY3k9IjMwLjMzMzMzMzMiIHI9IjUuODMzMzMzMzMiPjwvY2lyY2xlPgogICAgICAgICAgICAgICAgICAgIDxjaXJjbGUgaWQ9Ik92YWwtNS1Db3B5LTUiIGZpbGw9IiNFMEI0QjciIGN4PSIxNzUuNTgzMzMzIiBjeT0iMjMyLjc1IiByPSI1LjI1Ij48L2NpcmNsZT4KICAgICAgICAgICAgICAgIDwvZz4KICAgICAgICAgICAgPC9nPgogICAgICAgIDwvZz4KICAgIDwvZz4KPC9zdmc+);
  45. /* background-color: #112346; */
  46. background-repeat: no-repeat;
  47. background-position: center right;
  48. background-size: 100%;
  49. }
  50. .login-container {
  51. border-radius: 10px;
  52. margin: 0px auto;
  53. width: 350px;
  54. padding: 30px 35px 15px 35px;
  55. background: #fff;
  56. border: 1px solid #eaeaea;
  57. text-align: left;
  58. box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  59. }
  60. .title {
  61. margin: 0px auto 40px auto;
  62. text-align: center;
  63. color: #505458;
  64. }
  65. </style>
  • 注册组件---Register.vue

  1. <template>
  2. <div class="login-wrap">
  3. <el-form class="login-container">
  4. <h1 class="title">用户注册</h1>
  5. <el-form-item label="">
  6. <el-input type="text" v-model="username" placeholder="账号" autocomplete="off"></el-input>
  7. </el-form-item>
  8. <el-form-item label="">
  9. <el-input type="password" v-model="password" placeholder="密码" autocomplete="off"></el-input>
  10. </el-form-item>
  11. <el-form-item>
  12. <el-button type="primary" style="width:100%;" @click="doSubmit()">提交</el-button>
  13. </el-form-item>
  14. <el-row style="text-align: center;margin-top:-10px">
  15. <el-link type="primary">忘记密码</el-link>
  16. <el-link type="primary" @click="gotoLogin()">用户登录</el-link>
  17. </el-row>
  18. </el-form>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'Register',
  24. data () {
  25. return {
  26. username : '',
  27. password:''
  28. }
  29. },
  30. methods:{
  31. gotoLogin(){
  32. //跳转
  33. this.$router.push('/');
  34. }
  35. }
  36. }
  37. </script>
  38. <style scoped>
  39. .login-wrap {
  40. box-sizing: border-box;
  41. width: 100%;
  42. height: 100%;
  43. padding-top: 10%;
  44. background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+Cjxzdmcgd2lkdGg9IjEzNjFweCIgaGVpZ2h0PSI2MDlweCIgdmlld0JveD0iMCAwIDEzNjEgNjA5IiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPgogICAgPCEtLSBHZW5lcmF0b3I6IFNrZXRjaCA0Ni4yICg0NDQ5NikgLSBodHRwOi8vd3d3LmJvaGVtaWFuY29kaW5nLmNvbS9za2V0Y2ggLS0+CiAgICA8dGl0bGU+R3JvdXAgMjE8L3RpdGxlPgogICAgPGRlc2M+Q3JlYXRlZCB3aXRoIFNrZXRjaC48L2Rlc2M+CiAgICA8ZGVmcz48L2RlZnM+CiAgICA8ZyBpZD0iQW50LURlc2lnbi1Qcm8tMy4wIiBzdHJva2U9Im5vbmUiIHN0cm9rZS13aWR0aD0iMSIgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj4KICAgICAgICA8ZyBpZD0i6LSm5oi35a+G56CB55m75b2VLeagoemqjCIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTc5LjAwMDAwMCwgLTgyLjAwMDAwMCkiPgogICAgICAgICAgICA8ZyBpZD0iR3JvdXAtMjEiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDc3LjAwMDAwMCwgNzMuMDAwMDAwKSI+CiAgICAgICAgICAgICAgICA8ZyBpZD0iR3JvdXAtMTgiIG9wYWNpdHk9IjAuOC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MzMyIgY3k9IjMwLjMzMzMzMzMiIHI9IjUuODMzMzMzMzMiPjwvY2lyY2xlPgogICAgICAgICAgICAgICAgICAgIDxjaXJjbGUgaWQ9Ik92YWwtNS1Db3B5LTUiIGZpbGw9IiNFMEI0QjciIGN4PSIxNzUuNTgzMzMzIiBjeT0iMjMyLjc1IiByPSI1LjI1Ij48L2NpcmNsZT4KICAgICAgICAgICAgICAgIDwvZz4KICAgICAgICAgICAgPC9nPgogICAgICAgIDwvZz4KICAgIDwvZz4KPC9zdmc+);
  45. /* background-color: #112346; */
  46. background-repeat: no-repeat;
  47. background-position: center right;
  48. background-size: 100%;
  49. }
  50. .login-container {
  51. border-radius: 10px;
  52. margin: 0px auto;
  53. width: 350px;
  54. padding: 30px 35px 15px 35px;
  55. background: #fff;
  56. border: 1px solid #eaeaea;
  57. text-align: left;
  58. box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  59. }
  60. .title {
  61. margin: 0px auto 40px auto;
  62. text-align: center;
  63. color: #505458;
  64. }
  65. </style>
  • 定义组件与路由的对应关系

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import HelloWorld from '@/components/HelloWorld'
  4. import Login from '@/views/Login'
  5. import Register from '@/views/Register'
  6. Vue.use(Router)
  7. export default new Router({
  8. routes: [
  9. {
  10. path: '/',
  11. name: 'Login',
  12. component: Login
  13. },
  14. {
  15. path: '/Register',
  16. name: 'Register',
  17. component: Register
  18. }
  19. ]
  20. })

效果演示: 

三、前后端数据交互

使用SSM项目构建Java后台,模拟提供一个用户登录的action地址,Vue通过请求指定的用户登录接口。

1.安装axios

axios是vue2提倡使用的轻量版的ajax。它是基于promise的HTTP库。它会从浏览器中创建 XMLHttpRequests,与Vue配合使用非常好。

vue.js有著名的全家桶系列:vue-router,vuex, vue-resource,再加上构建工具vue-cli,就是一个完整的vue项目的核心构成。 其中vue-resource是Vue.js的一款插件,它可以通过XMLHttpRequest或JSONP发起请求并处理响应,但在vue更新到2.0之后,作者就宣告不再对vue-resource更新,而是推荐的axios

安装指令

npm i axios -S

"qs"通常指的是"query string",即查询字符串。查询字符串是URL中的一部分,用于传递参数给服务器。在Vue中,我们可以使用"qs"库来处理和解析查询字符串和传参问题

"qs"库是一个用于序列化和解析URL查询字符串的JavaScript库。它提供了一组方法,可以将JavaScript对象转换为查询字符串,或将查询字符串解析为JavaScript对象。通过使用"qs"库,我们可以方便地处理URL中的查询参数,例如在发送HTTP请求时将参数序列化为查询字符串,或在接收到查询字符串时将其解析为JavaScript对象进行处理。通过使 用"qs"库,我们可以更方便地处理Vue应用程序中的查询字符串,提高开发效率。

安装指令

npm i qs -S

Vue Axios是一个基于Promise的HTTP客户端,用于在Vue.js应用程序中发送HTTP请求。它是基于Axios库的封装,提供了一种简洁、灵活和强大的方式来处理HTTP请求和响应。

使用Vue Axios,你可以轻松地发送GET、POST、PUT、DELETE等不同类型的请求,并处理服务器返回的数据。它还支持拦截器,可以在请求发送前或响应返回后对请求进行拦截和处理。此外,Vue Axios还提供了取消请求、设置请求头、处理错误等功能,使得与后端API进行交互变得更加方便和可靠。 通过Vue Axios,还可以在Vue.js应用程序中轻松地集成和管理HTTP请求,从而更好地处理数据交互和与后端API进行通信。

安装指令

npm i vue-axios -S

2.导入api模块,添加axios的全局配置

将api模块导入到SPA项目的src目录下,其中api模块包含了action.js(针对后台请求接口的封装定义)和http.js(针对axios的全局配置)两个文件。

action.js

  1. /**
  2. * 对后台请求的地址的封装,URL格式如下:
  3. * 模块名_实体名_操作
  4. */
  5. export default {
  6. 'SERVER': 'http://localhost:8080/ssm/', //服务器url
  7. 'SYSTEM_USER_DOLOGIN': 'user/userLogin', //登陆
  8. 'SYSTEM_USER_DOREG': 'user/userRegister', //注册
  9. 'getFullPath': k => { //获得请求的完整地址,用于mockjs测试时使用
  10. return this.SERVER + this[k];
  11. }
  12. }

http.js

  1. /**
  2. * vue项目对axios的全局配置
  3. */
  4. import axios from 'axios'
  5. import qs from 'qs'
  6. //引入action模块,并添加至axios的类属性urls上
  7. import action from '@/api/action'
  8. axios.urls = action
  9. // axios默认配置
  10. axios.defaults.timeout = 10000; // 超时时间
  11. // axios.defaults.baseURL = 'http://localhost:8080/j2ee15'; // 默认地址
  12. axios.defaults.baseURL = action.SERVER;
  13. //整理数据
  14. // 只适用于 POST,PUT,PATCH,transformRequest` 允许在向服务器发送前,修改请求数据
  15. axios.defaults.transformRequest = function(data) {
  16. data = qs.stringify(data);
  17. return data;
  18. };
  19. // 请求拦截器
  20. axios.interceptors.request.use(function(config) {
  21. return config;
  22. }, function(error) {
  23. return Promise.reject(error);
  24. });
  25. // 响应拦截器
  26. axios.interceptors.response.use(function(response) {
  27. return response;
  28. }, function(error) {
  29. return Promise.reject(error);
  30. });
  31. export default axios;

3.修改main.js配置vue-axios

在main.js文件中引入api模块和vue-axios模块

  1. import axios from '@/api/http'
  2. import VueAxios from 'vue-axios'
  3. Vue.use(VueAxios,axios)

4.数据表数据

5.编写后端登录注册controller层

  1. package com.ctb.ssm.controller;
  2. import com.ctb.ssm.service.IUserService;
  3. import com.ctb.ssm.util.JsonResponseBody;
  4. import com.ctb.ssm.util.PageBean;
  5. import com.ctb.ssm.vo.UserVo;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.util.List;
  13. import java.util.Map;
  14. import com.ctb.ssm.jwt.*;
  15. @Controller
  16. @RequestMapping("/user")
  17. public class UserController {
  18. @Autowired
  19. private IUserService userService;
  20. /**
  21. * 登录
  22. * @param userVo
  23. * @param response
  24. * @return
  25. */
  26. @RequestMapping("/userLogin")
  27. @ResponseBody
  28. public JsonResponseBody<?> userLogin(UserVo userVo, HttpServletResponse response){
  29. if(userVo.getUsername().equals("admin")&&userVo.getPassword().equals("123")){
  30. //私有要求claim
  31. // Map<String,Object> json=new HashMap<String,Object>();
  32. // json.put("username", userVo.getUsername());
  33. //生成JWT,并设置到response响应头中
  34. // String jwt=JwtUtils.createJwt(json, JwtUtils.JWT_WEB_TTL);
  35. // response.setHeader(JwtUtils.JWT_HEADER_KEY, jwt);
  36. return new JsonResponseBody<>("用户登陆成功!",true,0,null);
  37. }else{
  38. return new JsonResponseBody<>("用户名或密码错误!",false,0,null);
  39. }
  40. }
  41. /**
  42. * 注册
  43. * @param userVo
  44. * @param response
  45. * @return
  46. */
  47. @RequestMapping("/userRegister")
  48. @ResponseBody
  49. public JsonResponseBody<?> userRegister(UserVo userVo, HttpServletResponse response){
  50. userVo.setId("9");
  51. int i = userService.insertSelective(userVo);
  52. if(i>0){
  53. return new JsonResponseBody<>("用户注册成功!",true,0,null);
  54. }else{
  55. return new JsonResponseBody<>("用户注册失败!",false,0,null);
  56. }
  57. }
  58. }

6.使用封装后的axios发送请求

Login.vue登录提交中发get请求

  1. <template>
  2. <div class="login-wrap">
  3. <el-form class="login-container">
  4. <h1 class="title">用户登录</h1>
  5. <el-form-item label="">
  6. <el-input type="text" v-model="username" placeholder="登录账号" autocomplete="off"></el-input>
  7. </el-form-item>
  8. <el-form-item label="">
  9. <el-input type="password" v-model="password" placeholder="登录密码" autocomplete="off"></el-input>
  10. </el-form-item>
  11. <el-form-item>
  12. <el-button type="primary" style="width:100%;" @click="doSubmit()">提交</el-button>
  13. </el-form-item>
  14. <el-row style="text-align: center;margin-top:-10px">
  15. <el-link type="primary">忘记密码</el-link>
  16. <el-link type="primary" @click="gotoRegister()">用户注册</el-link>
  17. </el-row>
  18. </el-form>
  19. </div>
  20. </template>
  21. <script>
  22. import axios from 'axios'
  23. import qs from 'qs'
  24. export default {
  25. name: 'Login',
  26. data () {
  27. return {
  28. username: '',
  29. password: ''
  30. }
  31. },
  32. methods:{
  33. gotoRegister(){
  34. this.$router.push('/Register');
  35. },
  36. doSubmit(){
  37. let url = this.axios.urls.SYSTEM_USER_DOLOGIN;
  38. let params = {
  39. username: this.username,
  40. password: this.password
  41. };
  42. //get请求
  43. axios.get(url, {
  44. params: params
  45. }).then(r => {
  46. console.log(r);
  47. if (r.data.success) {
  48. this.$message({
  49. message: r.data.msg,
  50. type: 'success'
  51. });
  52. } else {
  53. this.$message.error(r.data.msg);
  54. }
  55. }).catch(e => {
  56. });
  57. //post请求
  58. // axios.post(url,params).then(r => {
  59. // console.log(r);
  60. // if (r.data.success) {
  61. // this.$message({
  62. // message: r.data.msg,
  63. // type: 'success'
  64. // });
  65. // } else {
  66. // this.$message.error(r.data.msg);
  67. // }
  68. // }).catch(e => {
  69. // });
  70. }
  71. }
  72. }
  73. </script>
  74. <style scoped>
  75. .login-wrap {
  76. box-sizing: border-box;
  77. width: 100%;
  78. height: 100%;
  79. padding-top: 10%;
  80. background-image: url('../assets/cfpl.png');
  81. background-repeat: no-repeat;
  82. background-position: center right;
  83. background-size: 100%;
  84. }
  85. .login-container {
  86. border-radius: 10px;
  87. margin: 0px auto;
  88. width: 350px;
  89. padding: 30px 35px 15px 35px;
  90. background: #fff;
  91. border: 1px solid #eaeaea;
  92. text-align: left;
  93. box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  94. }
  95. .title {
  96. margin: 0px auto 40px auto;
  97. text-align: center;
  98. color: #505458;
  99. }
  100. </style>

Register.vue注册提交中发post请求

  1. <template>
  2. <div class="login-wrap">
  3. <el-form class="login-container">
  4. <h1 class="title">用户注册</h1>
  5. <el-form-item label="">
  6. <el-input type="text" v-model="username" placeholder="注册账号" autocomplete="off"></el-input>
  7. </el-form-item>
  8. <el-form-item label="">
  9. <el-input type="password" v-model="password" placeholder="注册密码" autocomplete="off"></el-input>
  10. </el-form-item>
  11. <el-form-item>
  12. <el-button type="primary" style="width:100%;" @click="doSubmit()">提交</el-button>
  13. </el-form-item>
  14. <el-row style="text-align: center;margin-top:-10px">
  15. <el-link type="primary">忘记密码</el-link>
  16. <el-link type="primary" @click="gotoLogin()">用户登录</el-link>
  17. </el-row>
  18. </el-form>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'Register',
  24. data () {
  25. return {
  26. username: '',
  27. password: ''
  28. }
  29. },
  30. methods:{
  31. gotoLogin(){
  32. this.$router.push('/');
  33. },
  34. doSubmit(){
  35. let url = this.axios.urls.SYSTEM_USER_DOREG;
  36. let params = {
  37. username: this.username,
  38. password: this.password
  39. };
  40. this.axios.post(url,params).then(r => {
  41. console.log(r);
  42. if (r.data.success) {
  43. this.$message({
  44. message: r.data.msg,
  45. type: 'success'
  46. });
  47. //注册完成后自动进入登入界面
  48. this.$router.push('/');
  49. } else {
  50. this.$message.error(r.data.msg);
  51. }
  52. }).catch(e => {
  53. console.log(e);
  54. });
  55. }
  56. }
  57. }
  58. </script>
  59. <style scoped>
  60. .login-wrap {
  61. box-sizing: border-box;
  62. width: 100%;
  63. height: 100%;
  64. padding-top: 10%;
  65. background-image: url('../assets/cfpl.png');
  66. background-repeat: no-repeat;
  67. background-position: center right;
  68. background-size: 100%;
  69. }
  70. .login-container {
  71. border-radius: 10px;
  72. margin: 0px auto;
  73. width: 350px;
  74. padding: 30px 35px 15px 35px;
  75. background: #fff;
  76. border: 1px solid #eaeaea;
  77. text-align: left;
  78. box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  79. }
  80. .title {
  81. margin: 0px auto 40px auto;
  82. text-align: center;
  83. color: #505458;
  84. }
  85. </style>

7.测试

在前后端分离后我们需同时运行前后端项目,那么我们的前后端项目所用端口号为避免发生冲突,应修改为不同的,如后端Tomcat运行端口号为8080,那我们的SPA项目端口号应为其它。

我们应该如何修改SPA项目的端口号呢?

在config/index.js中进行修改

注意:

端口号的范围是从0到65535。

0到1023的端口号被称为"知名端口",用于一些常见的网络服务;

1024到49151的端口号被称为"注册端口",用于一些已经注册的应用程序;

49152到65535的端口号被称为"动态或私有端口",用于临时分配给客户端应用程序。

登录效果演示

注册效果演示 

数据库数据展示

四、CORS跨域

1.什么是CORS?

CORS是一种浏览器安全机制,用于限制跨域资源的访问。当浏览器发起跨域请求时,会先向目标服务器发送一个预检请求(OPTIONS请求),以确定是否允许跨域访问。如果服务器返回的响应中包含了合适的CORS头部信息,浏览器才会继续发送真正的跨域请求。

2.为什么会出现CORS问题?

  • 前端代码通过AJAX或Fetch API向不同域的服务器发送请求。
  • 前端代码中使用了不同域的资源,比如字体、图片、脚本等。

3.特点

  1. 安全性高:CORS 是一种安全的跨域访问解决方案,通过限制允许跨域访问的源和方法,可以有效地防止恶意攻击。

  2. 灵活性强:CORS 支持不同类型的请求,包括 GET、POST、PUT、DELETE 等,同时也支持不同类型的数据传输格式,比如 JSON、XML 等。

  3. 无需修改客户端代码:CORS 是一种服务器端的解决方案,因此无需修改客户端代码即可实现跨域访问。

  4. 无需额外授权:与 JSONP 不同,CORS 不需要额外授权,只需在服务器端设置响应头即可。

4.如何解决CORS问题?

  • 服务器端设置CORS头部信息:在服务器端的响应中添加合适的CORS头部信息,允许指定的域进行跨域访问。可以通过设置Access-Control-Allow-Origin、Access-Control-Allow-Methods、Access-Control-Allow-Headers等头部字段来控制跨域访问的权限。
  1. Access-Control-Allow-Origin: * // 允许所有域名访问
  2. Access-Control-Allow-Methods: GET, POST, PUT, DELETE // 允许的请求方法
  3. Access-Control-Allow-Headers: Content-Type // 允许的请求头
  • 使用代理服务器:通过在同域下设置一个代理服务器,将跨域请求转发到目标服务器,然后将响应返回给前端。这样前端代码就可以绕过CORS限制,实现跨域访问。
  • JSONP:JSONP(JSON with Padding)是一种利用script标签跨域获取数据的方法。它的原理是利用script标签可以跨域加载资源的特性,通过动态创建script标签,向服务器请求JSON数据,从而实现跨域获取数据。需要注意的是,JSONP只支持GET请求。

5.跨域问题处理

        需要配置tomcat允许跨域访问,需编写一个过滤器,添加一个响应头Access-Control-Allow-Origin即可

  1. package com.ctb.ssm.util;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. import javax.servlet.http.HttpServletResponse;
  10. /**
  11. * 配置tomcat允许跨域访问
  12. *
  13. * @author Administrator
  14. *
  15. */
  16. public class CorsFilter2 implements Filter {
  17. @Override
  18. public void init(FilterConfig filterConfig) throws ServletException {
  19. }
  20. @Override
  21. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
  22. throws IOException, ServletException {
  23. HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
  24. // Access-Control-Allow-Origin就是我们需要设置的域名
  25. // Access-Control-Allow-Headers跨域允许包含的头。
  26. // Access-Control-Allow-Methods是允许的请求方式
  27. httpResponse.setHeader("Access-Control-Allow-Origin", "*");// *,任何域名
  28. httpResponse.setHeader("Access-Control-Allow-Headers", "responseType,Origin, X-Requested-With, Content-Type, Accept");
  29. httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
  30. //允许客户端处理一个新的响应头jwt
  31. //httpResponse.setHeader("Access-Control-Expose-Headers", "jwt,Content-Disposition");
  32. filterChain.doFilter(servletRequest, servletResponse);
  33. }
  34. @Override
  35. public void destroy() {
  36. }
  37. }

将Tomcat跨域处理配置到web.xml中,对所有请求进行拦截处理

  1. <!--CrosFilter跨域过滤器-->
  2. <filter>
  3. <filter-name>corsFilter</filter-name>
  4. <filter-class>com.ctb.ssm.util.CorsFilter2</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>corsFilter</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>

它设置了适当的 CORS 响应头,以允许跨域请求,包括允许的来源域、请求头和请求方法,有效的控制跨域访问的权限,解决跨域问题。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/111754
推荐阅读
相关标签
  

闽ICP备14008679号