vue+springboot多角色登录

news2025/1/23 11:25:20

①前端编写

将Homeview修改为manager

Manager:

<template>
  <div>
    <el-container>
      <!--    侧边栏  -->
      <el-aside :width="asideWidth" style="min-height: 100vh; background-color: #001529">
        <div style="height: 60px; color: white; display: flex; align-items: center; justify-content: center">
          <img src="@/assets/logo1.png" alt="" style="width: 40px; height: 40px">
          <span class="logo-title" v-show="!isCollapse">honey2024</span>
        </div>

        <el-menu :collapse="isCollapse" :collapse-transition="false" router background-color="#001529" text-color="rgba(255, 255, 255, 0.65)" active-text-color="#fff" style="border: none" :default-active="$route.path">
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统首页</span>
          </el-menu-item>
          <el-submenu index="info" v-if="user.role === '管理员'">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>信息管理</span>
            </template>
            <el-menu-item index="/user">用户信息</el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>

      <el-container>
        <!--        头部区域-->
        <el-header>

          <i :class="collapseIcon" style="font-size: 26px" @click="handleCollapse"></i>
          <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-left: 20px">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: '/user' }">用户管理</el-breadcrumb-item>
          </el-breadcrumb>

          <div style="flex: 1; width: 0; display: flex; align-items: center; justify-content: flex-end">
            <i class="el-icon-quanping" style="font-size: 26px" @click="handleFull"></i>
            <el-dropdown placement="bottom">
              <div style="display: flex; align-items: center; cursor: default">
                <img src="@/assets/logo1.png" alt="" style="width: 40px; height: 40px; margin: 0 5px">
                <span>{{user.name}}</span>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>

        </el-header>

        <!--        主体区域-->
        <el-main>
          <router-view></router-view>
        </el-main>

      </el-container>


    </el-container>
  </div>
</template>

<script>
import axios from "axios";
import request from '@/utils/request'

export default {
  name: 'HomeView',
  data() {
    return {
      isCollapse: false,  // 不收缩
      asideWidth: '200px',
      collapseIcon: 'el-icon-s-fold',
      users: [],
      user:JSON.parse(localStorage.getItem('honey-user')||'{}'),
      url:'',
      urls:[]
    }
  },
  mounted() {
    // axios.get('http://localhost:9090/user/selectall').then(res=>{
    //   console.log(res.data);
    //   this.users=res.data.data
    // })

    request.get('/user/selectall').then(res => {
      this.users = res.data
    })
  },
  methods: {
    preview(url){
      window.open(url)
    },
    showUrls(){
      console.log(this.urls)
    },
    handleMutipleFileUpload(response,file,fileList){
      this.urls=fileList.map(v=>v.response?.data)
    },
    handleTableFileUpload(row,file,fileList){
      console.log(row,file,fileList)
      row.avatar=file.response.data
      // this.$set(row,'avatar',file.response.data)
      console.log(row)
      request.put('/user/update',row).then(res=>{
        if(res.code==='200'){
          this.$message.success('上传成功')
        }else{
          this.$message.error(res.msg)
        }
      })
    },
    handleFileUpload(response,file,fileList){
      this.fileList=fileList
      console.log(response,file,fileList)
    },
    logout() {
      localStorage.removeItem("honey-user")
      this.$router.push('/login')
    },
    handleFull() {
      document.documentElement.requestFullscreen()
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse
      this.asideWidth = this.isCollapse ? '64px' : '200px'
      this.collapseIcon = this.isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'
    }
  }
}
</script>

<style>
.el-menu--inline {
  background-color: #000c17 !important;
}

.el-menu--inline .el-menu-item {
  background-color: #000c17 !important;
  padding-left: 49px !important;
}

.el-menu-item:hover, .el-submenu__title:hover {
  color: #fff !important;
}

.el-submenu__title:hover i {
  color: #fff !important;
}

.el-menu-item:hover i {
  color: #fff !important;
}

.el-menu-item.is-active {
  background-color: #1890ff !important;
  border-radius: 5px !important;
  width: calc(100% - 8px);
  margin-left: 4px;
}

.el-menu-item.is-active i, .el-menu-item.is-active .el-tooltip {
  margin-left: -4px;
}

.el-menu-item {
  height: 40px !important;
  line-height: 40px !important;
}

.el-submenu__title {
  height: 40px !important;
  line-height: 40px !important;
}

.el-submenu .el-menu-item {
  min-width: 0 !important;
}

.el-menu--inline .el-menu-item.is-active {
  padding-left: 45px !important;
}

/*.el-submenu__icon-arrow {*/
/*  margin-top: -5px;*/
/*}*/

.el-aside {
  transition: width .3s;
  box-shadow: 2px 0 6px rgba(0, 21, 41, .35);
}

.logo-title {
  margin-left: 5px;
  font-size: 20px;
  transition: all .3s; /* 0.3s */
}

.el-header {
  box-shadow: 2px 0 6px rgba(0, 21, 41, .35);
  display: flex;
  align-items: center;
}
</style>

Home:

<template>
  <div>
    <div style="box-shadow: 0 0 10px rgba(0,0,0,.1); padding: 10px 20px; border-radius: 5px; margin-bottom: 10px">
      早安,{{user.name}},祝你开心每一天!
    </div>
    <div style="display: flex;">
      <el-card style="width: 100%;">
        <div slot="header" class="clearfix">
          <span>青哥哥带你做毕设2024</span>
        </div>
        <div>
          2024毕设正式开始了!青哥哥带你手把手敲出来!
          <div style="margin-top: 20px">
            <div style="margin: 10px 0"><strong>主题色</strong></div>
            <el-button type="primary">按钮</el-button>
            <el-button type="success">按钮</el-button>
            <el-button type="warning">按钮</el-button>
            <el-button type="danger">按钮</el-button>
            <el-button type="info">按钮</el-button>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name:'Home',
  data(){
    return{
      user:JSON.parse(localStorage.getItem('honey-user')||'{}')
    }
  }

}
</script>

<style scoped>

</style>

User:

<template>
  <div>
    <el-table :data="tableData" stripe>
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column label="头像">
        <template v-slot="scope">
          <div style="display: flex;align-items: center">
            <el-image style="width: 50px;height: 50px;border-radius: 50%" v-if="scope.row.avatar" :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]"></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="role" label="角色"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <div style="display: flex">
            <el-button type="primary" plain size="mini">编辑</el-button>
            <el-button type="danger" plain size="mini">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name:'User',
  data(){
    return{
      tableData:[]
    }
  },
  created() {
    this.load()
  },
  methods:{
    load(){
      this.$request.get('/user/selectall').then(res=>{
        this.tableData=res.data
      })
    }
  }
}
</script>

<style scoped>

</style>

router文件夹下的index.js:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Manager from '../views/Manager.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'manager',
    component: Manager,
    children:[
      {path:'home',name:'Home',component:()=>import('../views/manager/Home.vue')},
      {
        path:'user',name:'User',component:()=>import('../views/manager/User.vue')
      }
    ],
    redirect:'/home'
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path:'/login',
    name:'login',
    component: ()=>import('../views/login.vue')
  },
  {
    path:'/register',
    name:'register',
    component: ()=>import('../views/register.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

②后端编写 

用navicate配置修改数据库:

Springboot修改:

User:

package com.example.springboot.entity;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
public class User {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private String phone;
    private String email;
    private String address;
    private String avatar;
    private String token;
    private String role;
}

 ③编写路由守卫

修改如图部分:

修改vue中router文件夹下index.js:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Manager from '../views/Manager.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'manager',
    component: Manager,
    children:[
      {path:'home',name:'Home',component:()=>import('../views/manager/Home.vue')},
      {
        path:'user',name:'User',component:()=>import('../views/manager/User.vue')
      },
      {
        path:'403',name:'Auth',component:()=>import('../views/Auth.vue')
      }
    ],
    redirect:'/home'
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path:'/login',
    name:'login',
    component: ()=>import('../views/login.vue')
  },
  {
    path:'/register',
    name:'register',
    component: ()=>import('../views/register.vue')
  },
  {
    path:'*',
    name:'404',
    component: ()=>import('../views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  let adminPaths=['/user']
  let user=JSON.parse(localStorage.getItem('honey-user')||'{}')
  if(user.role !== '管理员' && adminPaths.includes(to.path)){
    next('/403')
  }else{
    next()
  }
})
export default router

编写404和无权访问(403)页面:自行美化

404:

<template>
  <div>
    <div style="height: 100vh;display: flex;align-items: center;justify-content: center">
      <div style="font-size: 40px">    404 找不到页面
        <router-link to="/">返回首页</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {

}
</script>

<style scoped>

</style>

Auth:

<template>
  <div>
    <div style="height: calc(100vh - 80px);display: flex;align-items: center;justify-content: center">
      <div style="font-size: 40px">
        无权访问
        <router-link to="/">返回首页</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {

}
</script>

<style scoped>

</style>

注册页修改配置:

register:

<template>
  <div style="display: flex;align-items: center;justify-content: center;background-color: #669fefff;height: 100vh;">
    <div style="display: flex;width: 50%;background-color: white;border-radius: 5px;overflow: hidden;">
      <div style="flex: 1;">
        <img src="@/assets/register.png" alt="" style="width: 100%;">
      </div>
      <div style="flex: 1;display: flex;align-items: center;justify-content: center;">
        <el-form :model="user" style="width: 80%;" :rules="rules" ref="registerRef">
          <div style="font-weight: bold; font-size: 20px;margin-bottom: 20px;text-align: center;">
            欢迎注册后台管理系统
          </div>
          <el-form-item prop="username">
            <el-input placeholder="请输入用户名" v-model="user.username" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" v-model="user.password" show-password prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input placeholder="请确认密码" v-model="user.confirmPass"></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <el-radio-group v-model="user.role">
              <el-radio label="用户">
              </el-radio>
              <el-radio label="商家">
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;" @click="register">注册</el-button>
          </el-form-item>
          <div style="display: flex;">
            <div style="flex: 1;text-align: left">已没有账号?去<span style="color:aquamarine;cursor: pointer;" @click="$router.push('/login')">登录</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name:'register',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入确认密码'));
      } else if(value !== this.user.password){
        callback(new Error('两次密码不一致'));
      } else {
        callback();
      }
    };
    return {
      code:'',
      user: {
        code:'',
        username: '',
        password: '',
        confirmPass:''
      },
      rules:{
        username:[{
          required:'true',message:'请输入账号',trigger:'blur'
        }],
        password:[{
          required:'true',message:'请输入密码',trigger:'blur'
        }],
        confirmPass:[{
          validator:validatePass,trigger:'blur'
        }],
        role:[
          {
            required:'true',message:'请选择角色',trigger:'blur'
          }
        ]
      },
    }
  },
  methods:{
    getCode(code){
      this.code=code.toLowerCase()
    },
    register(){
      this.$refs['registerRef'].validate((valid=>{
        if(valid){
          this.$request.post("/register",this.user).then(res=>{
            if(res.code === '200'){
              this.$router.push('/login')
              this.$message.success('注册成功')
            }else{
              this.$message.error(res.msg)
            }
            console.log(res);
          })
        }
      }))
    }
  }
}
</script>

<style scoped></style>

 修改springboot中的Mapper文件夹下的Usermapper:为insert数据库添加一个role属性

UserMapper:

package com.example.springboot.mapper;

import com.example.springboot.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Insert("insert into `user` (username, password, name, phone, email, address, avatar, role) " +
            "values (#{username}, #{password}, #{name}, #{phone}, #{email}, #{address}, #{avatar},#{role})")
    void insert(User user);

    @Update("update `user` set username = #{username} , password = #{password} , name = #{name} , phone=#{phone} , email = #{email} , avatar=#{avatar} where id = #{id}")
    void updateUser(User user);

    @Delete("delete from `user` where id=#{id}")
    void deleteUser(Integer id);

    @Select("select * from `user` order by id desc")
    List<User> selectall();

    @Select("select * from `user` where id =#{id} order by id desc")
    User selectbyid(Integer id);

    @Select("select * from `user` where name = #{name} order by id desc")
    List<User> selectbyname(String name);
    @Select("select * from `user` where username = #{username} and name = #{name} order by id desc")
    List<User> selectbymore(@Param("username") String username,@Param("name") String name);

    @Select("select * from `user` where username like  concat('%',#{username},'%') or name like concat('%',#{name},'%') order by id desc")
    List<User> selectbymo(@Param("username") String username,@Param("name") String name);

    @Select("select * from `user` where username = #{username} order by id desc")
    User selectbyUsername(String username);

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1576165.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

什么是拨号VPS(Virtual Private Server)

拨号VPS&#xff08;Virtual Private Server&#xff09;是指通过拨号方式连接到互联网的虚拟专用服务器。它使用调制解调器&#xff08;称为拨号调制解调器&#xff09;来连接到互联网&#xff0c;通常是通过标准电话线或数字电话线接入&#xff0c;而不是传统的互联网连接方式…

14届蓝桥杯省赛 C/C++ B组 T4 飞机降落 (DFS)

记录此题提醒自己&#xff0c;此类时间轴问题可以通过DFS解决 DFS不是能解决所有题吗 对于此题&#xff0c;我们将降落的飞机的个数和时间轴作为DFS的形参&#xff0c;这样可以节省手动回溯的过程。 并且在DFS的过程中我们要加入一些贪心策略&#xff0c;否则直接爆搜有可能搜…

最优算法100例之38-构建乘积数组

专栏主页:计算机专业基础知识总结(适用于期末复习考研刷题求职面试)系列文章https://blog.csdn.net/seeker1994/category_12585732.html 题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不…

医疗器械FDA | 常见的网络安全材料发补问题都有哪些?

FDA网络安全资料发补咨询点此​​获取https://work.weixin.qq.com/ca/cawcde5ee29d239046 ————————--- 01 安全文档编写问题 FDA网络安全文档编写格式、内容、可读性等未满足官方要求&#xff0c;则将可能被要求发补整改编写后的文档。 02 安全管理问题 a. 网络安…

最优算法100例之31-正则表达式匹配

专栏主页&#xff1a;计算机专业基础知识总结&#xff08;适用于期末复习考研刷题求职面试&#xff09;系列文章https://blog.csdn.net/seeker1994/category_12585732.html 题目描述 请实现一个函数用来匹配包括.和*的正则表达式。模式中的字符.表示任意一个字符&#xff0c;而…

python爬虫学习第十六天--------URLError和HTTPError、cookie登录、Handler处理器

&#x1f388;&#x1f388;作者主页&#xff1a; 喔的嘛呀&#x1f388;&#x1f388; &#x1f388;&#x1f388;所属专栏&#xff1a;python爬虫学习&#x1f388;&#x1f388; ✨✨谢谢大家捧场&#xff0c;祝屏幕前的小伙伴们每天都有好运相伴左右&#xff0c;一定要天天…

vivado中移位寄存器的优化(二)

移位寄存器优化用于改善移位寄存器单元&#xff08;SRLs&#xff09;与其他逻辑单元之间的负裕量路径的时序。如果存在对移位寄存器单元&#xff08;SRL16E或SRLC32E&#xff09;的时序违规&#xff0c;优化会从SRL寄存器链的开始或结束位置提取一个寄存器&#xff0c;并将其放…

centos安装使用elasticsearch

1.首先可以在 Elasticsearch 官网 Download Elasticsearch | Elastic 下载安装包 2. 在指定的位置(我的是/opt/zhong/)解压安装包 tar -zxvf elasticsearch-7.12.1-linux-x86_64.tar.gz 3.启动es-这种方式启动会将日志全部打印在当前页面&#xff0c;一旦使用 ctrlc退出就会导…

java web day28

多表查询 内连接 写法 外链接 子查询 标量子查询 列子查询 行子查询 表子查询

小程序实现订阅功能和测试发送订阅信息

现在一次性订阅是只能用户点一次才能发送一次&#xff0c;而针对长期模板只有规定的几种类目政务、民生、交通等等的才可以&#xff0c;所以说感觉这功能其实已经不是很适合使用了&#xff0c;只适合一些特别的场景才可以使用。 地址&#xff1a;https://developers.weixin.qq…

软件设计师26--关系代数

软件设计师26--关系代数 考点1&#xff1a;关系模式相关概念例题&#xff1a; 考点1&#xff1a;关系模式相关概念 并∪&#xff1a;结果是两张表所有记录的合并&#xff0c;相同记录只显示一次。 交∩&#xff1a;结果是两张表中相同的记录。 差-&#xff1a;S1-S2&#xff0…

Linux 下安装 openjdk 17【详细步骤】

👉 目标 了解为什么不选择 oracle jdk17,而是选择 openjdk17linux 下安装 openjdk17 详细步骤为什么不选择 oracle jdk17? 官网:Java Downloads | Oracle 中国 官网描述:JDK 17 binaries are free to use in production and free to redistribute, at no cost, under t…

Goingpub国自然基金-免费查询

可进行年份、学部、项目类别等检索&#xff0c;支持生成主题词汇总分析报告。 最最最关键&#xff0c;免费&#xff0c;只需要你注册登录一下&#xff0c;防止被爬虫侵扰。 界面简单&#xff0c;实用&#xff0c;支持模糊搜索&#xff0c;包含最新2023年数据&#xff0c;共56…

蚁群优化算法(Ant Colony Optimization Algorithm)

注意&#xff1a;本文引用自专业人工智能社区Venus AI 更多AI知识请参考原站 &#xff08;[www.aideeplearning.cn]&#xff09; 算法引言 蚁群算法&#xff0c;是一种模拟蚂蚁觅食行为的优化算法。想象一下&#xff0c;当你在野餐时&#xff0c;不小心洒了一些糖在地上。一…

2024免费Mac电脑用户的系统清理和优化软件CleanMyMac

作为产品营销专家&#xff0c;对于各类产品的特性与优势有着深入的了解。CleanMyMac是一款针对Mac电脑用户的系统清理和优化软件&#xff0c;旨在帮助用户轻松管理、优化和保护Mac电脑。以下是关于CleanMyMac的详细介绍&#xff1a; CleanMyMac X2024全新版下载如下: https://…

React18从入门到实战

文章目录 一、React环境的搭建二、项目文件的介绍&#xff08;1&#xff09;package.json&#xff0c;他是项目存放依赖包的地方&#xff0c;里面包括了一些项目核心包及下载的其他插件包&#xff08;2&#xff09;src文件夹是项目源码目录&#xff0c;平时开发页面就在其中&am…

如何成为一名优秀的工程师下

身为工程师&#xff0c;理所当然要重视实践&#xff0c;自然科学不管发展到何时都离不开实验。 电子学本身就是 为了指导工程实践。所以不要谈空洞的理论。现在很多毕业生都面临这样的问题&#xff0c;总是谈一些空洞的理论&#xff0c;甚至错误的但还不以为然的理论。实践可以…

【软考】23种设计模式详解,记忆方式,并举例说明

23种设计模式详解&#xff0c;举例说明 一、创建型模式1.1、抽象工厂模式&#xff08;Abstract Factory&#xff09;1.1.1、简介1.1.2、意图与应用场景1.1.3、结构1.1.4、优缺点1.1.4、示例代码&#xff08;简化版&#xff09; 1.2、建造者模式&#xff08;Builder&#xff09;…

MySQL主从的介绍与应用

mysql主从 文章目录 mysql主从1. 主从简介1.1 主从作用1.2 主从形式 2. 主从复制原理3. 主从复制配置3.1 mysql安装&#xff08;两台主机安装一致&#xff0c;下面只演示一台主机操作&#xff09;3.2 mysql主从配置3.2.1 确保从数据库与主数据库里的数据一样3.2.2 在主数据库里…

34-4 CSRF漏洞 - CSRF跨站点请求伪造

一、漏洞定义 CSRF(跨站请求伪造)是一种客户端攻击,又称为“一键式攻击”。该漏洞利用了Web应用程序与受害用户之间的信任关系,通过滥用同源策略,使受害者在不知情的情况下代表攻击者执行操作。与XSS攻击不同,XSS利用用户对特定网站的信任,而CSRF则利用了网站对用户网页…