会员中心可以包含用户注册、登录、个人信息修改等功能。在Spring Cloud项目中实现会员中心可以使用Spring Boot框架和Spring Security安全框架。
下面是一个简单的会员中心的代码示例,其中包含了用户注册、登录、个人信息修改的功能,注释详细。
- 用户实体类
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String email;
private String phoneNumber;
//省略get和set方法
}
- 用户注册接口
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public ResponseEntity<?> createUser(@Valid @RequestBody User user) {
User newUser = userService.createUser(user);
return new ResponseEntity<>(newUser, HttpStatus.CREATED);
}
}
- 用户登录接口
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {
String jwt = userService.authenticateUser(loginRequest.getUsername(), loginRequest.getPassword());
return ResponseEntity.ok(new JwtAuthenticationResponse(jwt));
}
}
- 用户个人信息修改接口
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
@PutMapping("/{id}")
public ResponseEntity<?> updateUser(@PathVariable Long id, @Valid @RequestBody User user) {
User updatedUser = userService.updateUser(id, user);
return ResponseEntity.ok(updatedUser);
}
}
- 用户服务代码
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private JwtTokenProvider tokenProvider;
public User createUser(User user) {
if (userRepository.existsByUsername(user.getUsername())) {
throw new BadRequestException("Username already exists!");
}
if (userRepository.existsByEmail(user.getEmail())) {
throw new BadRequestException("Email address already in use!");
}
// 加密密码
user.setPassword(passwordEncoder.encode(user.getPassword()));
return userRepository.save(user);
}
public String authenticateUser(String username, String password) {
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
SecurityContextHolder.getContext().setAuthentication(authentication);
return tokenProvider.generateToken(authentication);
}
public User updateUser(Long id, User newUser) {
return userRepository.findById(id)
.map(user -> {
user.setUsername(newUser.getUsername());
user.setEmail(newUser.getEmail());
user.setPhoneNumber(newUser.getPhoneNumber());
return userRepository.save(user);
})
.orElseThrow(() -> new ResourceNotFoundException("User not found with id " + id));
}
}
- 配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/member-center
username: root
password: root
jpa:
hibernate:
ddl-auto: create-drop
security:
jwt:
secret: secret-key
expiration-in-ms: 604800000
这是一个简单的会员中心实现代码示例,仅供参考。如果要在实际项目中使用,需要根据实际情况进行修改和完善。
1万+



被折叠的 条评论
为什么被折叠?



