【项目实战】一、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等根据,在网关层完成用户鉴权。

目录
打赏
0
0
0
0
62
分享
相关文章
微服务——SpringBoot使用归纳——Spring Boot开发环境搭建和项目启动
本文介绍了Spring Boot开发环境的搭建和项目启动流程。主要内容包括:jdk的配置(IDEA、STS/eclipse设置方法)、Spring Boot工程的构建方式(IDEA快速构建、官方构建工具start.spring.io使用)、maven配置(本地maven路径与阿里云镜像设置)以及编码配置(IDEA和eclipse中的编码设置)。通过这些步骤,帮助开发者顺利完成Spring Boot项目的初始化和运行准备。
38 0
微服务——SpringBoot使用归纳——Spring Boot开发环境搭建和项目启动
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
14 0
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录——使用Logger在项目中打印日志
本文介绍了如何在项目中使用Logger打印日志。通过SLF4J和Logback,可设置不同日志级别(如DEBUG、INFO、WARN、ERROR)并支持占位符输出动态信息。示例代码展示了日志在控制器中的应用,说明了日志配置对问题排查的重要性。附课程源码下载链接供实践参考。
35 0
SpringBoot项目打war包流程
本文介绍了将Spring Boot项目改造为WAR包并部署到外部Tomcat服务器的步骤。主要内容包括:1) 修改pom.xml中的打包方式为WAR;2) 排除Spring Boot内置的Tomcat依赖;3) 添加Servlet API依赖;4) 改造启动类以支持WAR部署;5) 打包和部署。通过这些步骤,可以轻松地将Spring Boot应用转换为适合外部Tomcat服务器的WAR包。
211 64
SpringBoot项目打war包流程
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
25 0
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
在微服务架构中,随着业务复杂度增加,项目可能需要调用多个微服务。为避免使用`@Value`注解逐一引入配置的繁琐,可通过定义配置类(如`MicroServiceUrl`)并结合`@ConfigurationProperties`注解实现批量管理。此方法需在配置文件中设置微服务地址(如订单、用户、购物车服务),并通过`@Component`将配置类纳入Spring容器。最后,在Controller中通过`@Resource`注入配置类即可便捷使用,提升代码可维护性。
18 0
SpringBoot项目打包成war包
通过上述步骤,我们成功地将一个Spring Boot应用打包成WAR文件,并部署到外部的Tomcat服务器中。这种方式适用于需要与传统Servlet容器集成的场景。
50 8
|
2月前
基于springboot+thymeleaf+Redis仿知乎网站问答项目源码
基于springboot+thymeleaf+Redis仿知乎网站问答项目源码
174 36
SpringBoot start.aliyun.com创建项目,解决properties乱码的问题
通过确保文件和开发环境的编码一致,配置 Maven 编码,设置 Spring Boot 应用和嵌入式服务器的编码,可以有效解决 properties 文件的乱码问题。以上步骤可以帮助开发者确保在 Spring Boot 项目中正确处理和显示多语言字符,避免因编码问题导致的乱码现象。
58 5
Spring Boot 3 集成 Spring Security + JWT
本文详细介绍了如何使用Spring Boot 3和Spring Security集成JWT,实现前后端分离的安全认证概述了从入门到引入数据库,再到使用JWT的完整流程。列举了项目中用到的关键依赖,如MyBatis-Plus、Hutool等。简要提及了系统配置表、部门表、字典表等表结构。使用Hutool-jwt工具类进行JWT校验。配置忽略路径、禁用CSRF、添加JWT校验过滤器等。实现登录接口,返回token等信息。
884 12

热门文章

最新文章