后端(springboot3+mybatis+redis+tokenJWT),前端(vue3+elementplus+axios+pinia+tokestores)前后端分离最完整最简易的项目
由于您的问题涉及到多个技术栈,并且没有明确的代码问题,我将提供一个简化的示例,展示如何在Spring Boot 3 + MyBatis + Redis + JWT环境中创建一个简单的登录接口,并使用Vue 3 + Element Plus + Axios + Pinia + TokenJWT进行前端交互。
后端(Spring Boot 3 + MyBatis + Redis + JWT):
- 引入依赖(
pom.xml
):
<!-- Spring Boot 3 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/>
</parent>
<!-- Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
- 配置(
application.properties
):
spring.datasource.url=jdbc:mysql://localhost:3306/yourdb
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.redis.host=localhost
spring.redis.port=6379
# JWT secret key
jwt.secret=your_secret_key
- 实体类和Mapper:
// User.java
public class User {
private Long id;
private String username;
private String password; // 假设使用明文密码,实际应加密
// getters and setters
}
// UserMapper.java
@Mapper
public interface UserMapper {
User selectByUsername(String username);
}
- 服务和控制器:
// UserService.java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private JwtUtil jwtUtil;
@Autowired
private StringRedisTemplate stringRedisTemplate;
public String login(String username, String password) {
User user = userMapper.selectByUsername(username);
if (user != null && user.getPassword().equals(password)) {
String token = jwtUtil.generateToken(user.getId());
stringRedisTemplate.opsForValue().set(token, username);
return token;
}
return null;
}
}
// AuthController.java
@RestController
@RequestMapping("/auth")
public class AuthControlle
评论已关闭