[add] #27
This commit is contained in:
parent
c544df6b2e
commit
05f051e116
@ -40,6 +40,13 @@ dependencies {
|
||||
runtimeOnly 'com.h2database:h2'
|
||||
runtimeOnly 'com.mysql:mysql-connector-j'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
|
||||
implementation "org.mapstruct:mapstruct:1.5.5.Final"
|
||||
annotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"
|
||||
|
||||
// If you are using mapstruct in test code
|
||||
testAnnotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
23
src/main/java/com/lk/paopao/controller/UserController.java
Normal file
23
src/main/java/com/lk/paopao/controller/UserController.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.lk.paopao.controller;
|
||||
|
||||
import com.lk.paopao.dto.UserDto;
|
||||
import com.lk.paopao.dto.rest.response.DataResult;
|
||||
import com.lk.paopao.dto.rest.response.ResultUtil;
|
||||
import com.lk.paopao.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("${app.version}/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/info")
|
||||
public DataResult<UserDto> info() {
|
||||
return ResultUtil.ok(userService.info());
|
||||
}
|
||||
}
|
21
src/main/java/com/lk/paopao/dto/UserDto.java
Normal file
21
src/main/java/com/lk/paopao/dto/UserDto.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.lk.paopao.dto;
|
||||
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* DTO for {@link com.lk.paopao.entity.User}
|
||||
*/
|
||||
@Value
|
||||
public class UserDto implements Serializable {
|
||||
Long id;
|
||||
String nickname;
|
||||
String username;
|
||||
String phone;
|
||||
Byte status;
|
||||
String avatar;
|
||||
Long balance;
|
||||
Boolean isAdmin;
|
||||
}
|
17
src/main/java/com/lk/paopao/mapper/UserMapper.java
Normal file
17
src/main/java/com/lk/paopao/mapper/UserMapper.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.lk.paopao.mapper;
|
||||
|
||||
import com.lk.paopao.entity.User;
|
||||
import com.lk.paopao.dto.UserDto;
|
||||
import org.mapstruct.BeanMapping;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MappingTarget;
|
||||
import org.mapstruct.NullValuePropertyMappingStrategy;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface UserMapper {
|
||||
User toEntity(UserDto userDto);
|
||||
|
||||
UserDto toDto(User user);
|
||||
|
||||
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)User partialUpdate(UserDto userDto, @MappingTarget User user);
|
||||
}
|
@ -2,15 +2,18 @@ package com.lk.paopao.service;
|
||||
|
||||
import com.lk.paopao.dto.rest.request.RegisterRequest;
|
||||
import com.lk.paopao.entity.User;
|
||||
import com.lk.paopao.exception.AuthFailedException;
|
||||
import com.lk.paopao.exception.ResourceExistedException;
|
||||
import com.lk.paopao.repository.UserRepository;
|
||||
import com.lk.paopao.security.JwtUtils;
|
||||
import jakarta.security.auth.message.AuthException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@ -83,5 +86,26 @@ public class AuthService {
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户详情。
|
||||
* @return 当前登录的UserDetails对象
|
||||
* @throws AuthFailedException 如果无法获取当前用户详情,则抛出异常
|
||||
*/
|
||||
public UserDetails getCurrentUserDetails() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication.getPrincipal() instanceof UserDetails) {
|
||||
return (UserDetails) authentication.getPrincipal();
|
||||
}
|
||||
throw new AuthFailedException("找不到用户在线信息");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息。
|
||||
* @return 当前登录的用户信息
|
||||
* @throws AuthFailedException 如果无法获取当前用户信息,则抛出异常
|
||||
*/
|
||||
public User getCurrentUser() {
|
||||
// 通过当前用户详情获取用户信息
|
||||
return userRepository.findByUsername(getCurrentUserDetails().getUsername()).orElseThrow(()->new AuthFailedException("找不到用户在线信息"));
|
||||
}
|
||||
}
|
||||
|
23
src/main/java/com/lk/paopao/service/UserService.java
Normal file
23
src/main/java/com/lk/paopao/service/UserService.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.lk.paopao.service;
|
||||
|
||||
import com.lk.paopao.dto.UserDto;
|
||||
import com.lk.paopao.mapper.UserMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
@Autowired
|
||||
private AuthService authService;
|
||||
|
||||
/**
|
||||
* 获取当前用户的基本信息
|
||||
* @return UserDto 当前用户的数据传输对象
|
||||
*/
|
||||
public UserDto info(){
|
||||
return userMapper.toDto(authService.getCurrentUser());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user