当前位置:首页 > VUE

vue实现java登录注册

2026-02-21 19:41:46VUE

实现 Vue 前端与 Java 后端的登录注册功能

前端 Vue 实现

安装必要的依赖(如 axios)

npm install axios

创建登录和注册组件

<template>
  <div>
    <input v-model="username" placeholder="用户名">
    <input v-model="password" type="password" placeholder="密码">
    <button @click="login">登录</button>
    <button @click="register">注册</button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    login() {
      axios.post('http://localhost:8080/api/login', {
        username: this.username,
        password: this.password
      }).then(response => {
        console.log(response.data);
      }).catch(error => {
        console.error(error);
      });
    },
    register() {
      axios.post('http://localhost:8080/api/register', {
        username: this.username,
        password: this.password
      }).then(response => {
        console.log(response.data);
      }).catch(error => {
        console.error(error);
      });
    }
  }
}
</script>

Java 后端实现

创建 Spring Boot 项目并添加依赖

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
  </dependency>
</dependencies>

创建用户实体类

@Entity
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(unique = true)
  private String username;

  private String password;

  // getters and setters
}

创建用户仓库接口

public interface UserRepository extends JpaRepository<User, Long> {
  User findByUsername(String username);
}

创建控制器

@RestController
@RequestMapping("/api")
public class AuthController {

  @Autowired
  private UserRepository userRepository;

  @PostMapping("/register")
  public ResponseEntity<String> register(@RequestBody User user) {
    if (userRepository.findByUsername(user.getUsername()) != null) {
      return ResponseEntity.badRequest().body("用户名已存在");
    }
    userRepository.save(user);
    return ResponseEntity.ok("注册成功");
  }

  @PostMapping("/login")
  public ResponseEntity<String> login(@RequestBody User user) {
    User existingUser = userRepository.findByUsername(user.getUsername());
    if (existingUser == null || !existingUser.getPassword().equals(user.getPassword())) {
      return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
    }
    return ResponseEntity.ok("登录成功");
  }
}

配置跨域支持

在 Spring Boot 应用中添加配置类

@Configuration
public class CorsConfig implements WebMvcConfigurer {
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/")
      .allowedOrigins("http://localhost:8081") // Vue 应用地址
      .allowedMethods("GET", "POST", "PUT", "DELETE")
      .allowedHeaders("*");
  }
}

数据库配置

在 application.properties 中添加

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

测试流程

启动 Spring Boot 应用后,访问 H2 控制台检查用户表是否创建成功

vue实现java登录注册

http://localhost:8080/h2-console

启动 Vue 应用,测试注册和登录功能是否正常工作

标签: vuejava
分享给朋友:

相关文章

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue事件实现

vue事件实现

Vue 事件实现方法 在Vue中,事件处理是通过v-on指令或@简写来实现的。以下是几种常见的事件处理方式: 使用v-on指令绑定事件 <button v-on:click="handleC…

vue实现 弹窗

vue实现 弹窗

Vue 实现弹窗的基本方法 使用 Vue 实现弹窗可以通过多种方式,包括组件化、指令、插件等。以下是几种常见的实现方法。 组件化实现弹窗 创建一个独立的弹窗组件,通过 props 控制显示与隐藏。…

vue系统实现

vue系统实现

Vue 系统实现的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装核心依赖(如 Vue 3、Vue Router、Pinia)。配置开发环境(如 ESLint、Prett…

vue实现桌面widget

vue实现桌面widget

Vue 实现桌面 Widget 的方法 使用 Vue 实现桌面 Widget 通常需要结合 Electron 或 NW.js 等桌面应用框架,以及系统级 API 调用。以下是具体实现方法: 使用 E…

vue怎么实现直播

vue怎么实现直播

Vue实现直播的方法 使用Vue实现直播功能通常需要结合第三方直播服务或WebRTC技术。以下是几种常见的实现方式: 使用第三方直播服务 集成如腾讯云、阿里云或七牛云等提供的直播SDK,通过他们的A…