当前位置:首页 > VUE

springboot加vue实现登录

2026-01-23 04:34:58VUE

技术选型与准备

后端采用Spring Boot 2.x,提供RESTful API;前端使用Vue 3(Composition API) + Vite构建工具,配合Axios进行HTTP请求。需安装Node.js、JDK 8+及Maven环境。

后端实现(Spring Boot)

  1. 项目结构
    创建Spring Boot项目,添加依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  2. 用户实体与数据库
    定义用户实体类并配置JPA或MyBatis:

    @Entity
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String username;
        private String password;
        // Getters & Setters
    }
  3. 安全配置
    自定义UserDetailsService并配置Spring Security:

    springboot加vue实现登录

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/api/login").permitAll()
                .anyRequest().authenticated();
        }
    }
  4. 登录API
    创建控制器处理登录请求:

    @RestController
    @RequestMapping("/api")
    public class AuthController {
        @PostMapping("/login")
        public ResponseEntity<?> login(@RequestBody LoginRequest request) {
            // 验证逻辑,返回JWT或Session标识
            return ResponseEntity.ok().body("登录成功");
        }
    }

前端实现(Vue 3)

  1. 项目初始化
    使用Vite创建Vue项目:

    springboot加vue实现登录

    npm create vite@latest vue-login --template vue
    cd vue-login
    npm install axios vue-router
  2. 登录页面
    创建Login.vue组件:

    <template>
      <form @submit.prevent="handleLogin">
        <input v-model="form.username" placeholder="用户名" />
        <input v-model="form.password" type="password" placeholder="密码" />
        <button type="submit">登录</button>
      </form>
    </template>
    
    <script setup>
    import { ref } from 'vue';
    import axios from 'axios';
    
    const form = ref({ username: '', password: '' });
    const handleLogin = async () => {
      try {
        const res = await axios.post('http://localhost:8080/api/login', form.value);
        console.log(res.data);
      } catch (error) {
        console.error(error);
      }
    };
    </script>
  3. 路由配置
    router/index.js中配置路由:

    import { createRouter, createWebHistory } from 'vue-router';
    import Login from '../views/Login.vue';
    
    const routes = [{ path: '/login', component: Login }];
    const router = createRouter({ history: createWebHistory(), routes });
    export default router;

跨域处理

后端添加CORS配置:

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("*");
    }
}

联调与测试

  1. 启动Spring Boot应用(默认端口8080)。
  2. 运行Vue开发服务器:npm run dev(默认端口3000)。
  3. 访问http://localhost:3000/login,输入凭证测试登录功能。

扩展建议

  • 后端:集成JWT实现无状态认证,或使用Spring Session管理会话。
  • 前端:添加Vuex/Pinia管理登录状态,结合路由守卫控制权限。

标签: springbootvue
分享给朋友:

相关文章

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录组…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue单选实现

vue单选实现

Vue 单选实现 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选效果。当用户选择不同的选…