【项目实战】一、Spring boot整合JWT、Vue案例展示用户鉴权(下)

简介: 【项目实战】一、Spring boot整合JWT、Vue案例展示用户鉴权(下)

5.5、vue项目引入router

5.5.1、安装路由

cnpm install vue-router@4

5.5.2、在components文件夹下创建登录页面Login.vue

<template>
  <div class="login">
    <el-card class="box-card">
      <el-form label-width="80px" :model="form" ref="form" >
        <el-form-item  label="用户名" prop="userId">
          <el-input v-model="form.userId" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userPassword">
          <el-input type="password" v-model="form.userPassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="lo" type="primary" @click="login()" >登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button  @click="select()" >查询</el-button>
      <br>
      <el-text>{{ message }}</el-text>
    </el-card>
  </div>
</template>
<script>
import service from '../service.js'
export default {
  data(){
    return {
      form:{
        userId:'',
        userPassword:''
      },
      message:''
    }
  },
  methods:{
    login(){
      service({
        method: 'post',
        url: '/auth/login',
        data: this.form
      }).then(res => {
        console.log(res.data.data.token)
        this.$message({message:"登录成功!",type:"success"})
        window.localStorage.setItem("token",res.data.data.token)
      })
    },
    select(){
      service({
        method: 'post',
        url: '/auth/getUserMessageByToken',
        headers: {
          "Content-Type":"application/json",
          "token":window.localStorage.getItem("token")
        }
      }).then(res => {
        console.log(res.data.data)
        this.message = res.data.data
      })
    }
  }
};
</script>

5.5.3、在sec文件夹下创建router文件夹

5.5.3.1、创建router.js

const routes = [
    {
        path:'/',
        redirect:'/login',
        name: '登录页',
        hidden:true,
        component:()=>import('@/components/Login') //路由懒加载
    },
    {
        path:'/login',
        name: '登录页',
        hidden:true,
        component:()=>import('@/components/Login') //路由懒加载
    }
]
export default routes;

5.5.3.2、创建index.js

import { createRouter, createWebHistory } from "vue-router"
import routes from "./routes"
var router=createRouter({
    history:createWebHistory(),
    routes
})
export default router

5.6、在main.js文件中配置路由

5.7、在App.vue中配置起始页面及路由入口

<template>
  <Login/>
  <router-view></router-view>
</template>
<script>
import Login from './components/Login.vue'
export default {
  name: 'App',
  components: {
    Login
  }
}
</script>

5.8、vue项目引入axios

5.8.1、安装axios

npm install axios --save

5.8.2、在src文件夹下创建service.js文件

//axiosInstance.js
//导入axios
import axios from 'axios'
//使用axios下面的create([config])方法创建axios实例,其中config参数为axios最基本的配置信息。
const service = axios.create({
    baseURL:'http://localhost', //请求后端数据的基本地址,自定义
    timeout: 2000                   //请求超时设置,单位ms
})
//导出我们建立的axios实例模块,ES6 export用法
export default service

5.8.3、登录页面引入service

5.8.4、service的使用

method:请求方式

url:请求地址

data:携带参数 // 注:若后端使用RequestBody对象接收参数,则用表单传递,若用String接收参数,需要用JSON.stringify(this.form)转为String类型的JSON格式

res:返回的内容

this.$message({message:“登录成功!”,type:“success”}):返回提示信息到前端页面,type有多种类型:success、error、wraning等

window.localStorage.setItem(“token”,res.data.data.token):将返回的token值存入本地存储当中

5.9、启动vue项目

npm run serve

6、测试

6.1、输入用户名和密码点击登录

6.2、点击查询

7、结束

此项目以SpringCloud为基础,首先创建空的父类SpringCloud空工程,规范Spring boot、Spring Cloud、Spring Cloud Alibaba版本;集成前端Vue3、router、axios,使用JWT、Nacos微服务、openFeign、Gateway及GlobalFilter等根据,在网关层完成用户鉴权。

目录
相关文章
|
JSON 安全 Java
什么是JWT?如何使用Spring Boot Security实现它?
什么是JWT?如何使用Spring Boot Security实现它?
2180 5
|
6月前
|
存储 Rust 安全
Rocket框架JWT鉴权实战:保护Rust Web API的安全方案​
本篇文章是基于rust语言和rocket依赖实现网页JWT认证和鉴权,完成简单的JWT token的验证和鉴权处理,使用cargo做依赖的导入和测试。
304 1
|
9月前
|
JavaScript 前端开发 Java
Spring Boot 与 Vue.js 前后端分离中的数据交互机制
本文深入探讨了Spring Boot与Vue.js在前后端分离架构下的数据交互机制。通过对比传统`model.addAttribute()`方法与RESTful API的设计,分析了两者在耦合性、灵活性及可扩展性方面的差异。Spring Boot以RESTful API提供数据服务,Vue.js借助Axios消费API并动态渲染页面,实现了职责分明的解耦架构。该模式显著提升了系统的灵活性和维护性,适用于复杂应用场景如论坛、商城系统等,为现代Web开发提供了重要参考。
1016 0
|
XML JavaScript Java
SpringBoot集成Shiro权限+Jwt认证
本文主要描述如何快速基于SpringBoot 2.5.X版本集成Shiro+JWT框架,让大家快速实现无状态登陆和接口权限认证主体框架,具体业务细节未实现,大家按照实际项目补充。
878 11
|
JSON 安全 算法
Spring Boot 应用如何实现 JWT 认证?
Spring Boot 应用如何实现 JWT 认证?
1317 8
|
JSON 安全 Go
Go语言中使用JWT鉴权、Token刷新完整示例,拿去直接用!
本文介绍了如何在 Go 语言中使用 Gin 框架实现 JWT 用户认证和安全保护。JWT(JSON Web Token)是一种轻量、高效的认证与授权解决方案,特别适合微服务架构。文章详细讲解了 JWT 的基本概念、结构以及如何在 Gin 中生成、解析和刷新 JWT。通过示例代码,展示了如何在实际项目中应用 JWT,确保用户身份验证和数据安全。完整代码可在 GitHub 仓库中查看。
2374 1
|
JavaScript NoSQL Java
CC-ADMIN后台简介一个基于 Spring Boot 2.1.3 、SpringBootMybatis plus、JWT、Shiro、Redis、Vue quasar 的前后端分离的后台管理系统
CC-ADMIN后台简介一个基于 Spring Boot 2.1.3 、SpringBootMybatis plus、JWT、Shiro、Redis、Vue quasar 的前后端分离的后台管理系统
478 0
|
4月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
423 2
|
3月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
375 137
|
7月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
908 0