feat(auth): add user profile to auth token response

- Add jwt configuration in application.yaml
- Include user profile information in authentication response
- Implement user profile response DTO with role mapping
- Fix typo in UserProfileService interface and implementation class names
- Add pagination support for user list endpoint
- Configure MyBatis Plus interceptor for MariaDB
- Add MyBatis Plus JSQLParser dependency
- Implement page-based user retrieval with role information
- Remove obsolete MyBatis configuration class
- Update controller to use paginated user data
- Replace manual user conversion with constructor mapping
- Rename UserProfileServcie to UserProfileService consistently
This commit is contained in:
Chaos
2025-11-24 17:10:01 +08:00
parent ec49ea8e25
commit a5a23a6b52
12 changed files with 121 additions and 48 deletions

View File

@@ -7,7 +7,9 @@ import cn.nopj.chaos_api.dto.request.UserProfileUpdateRequest;
import cn.nopj.chaos_api.dto.response.RoleResponse;
import cn.nopj.chaos_api.dto.response.UserProfileResponse;
import cn.nopj.chaos_api.mapper.UserMapper;
import cn.nopj.chaos_api.service.UserProfileServcie;
import cn.nopj.chaos_api.service.UserProfileService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
@@ -18,7 +20,7 @@ import java.util.List;
@Slf4j
@Service
public class UserProfileServcieImpl implements UserProfileServcie {
public class UserProfileServiceImpl implements UserProfileService {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
@@ -47,7 +49,7 @@ public class UserProfileServcieImpl implements UserProfileServcie {
throw new RuntimeException("获取所有用户信息失败");
}
return allUserWithRoles.stream().map(this::userConverterUserinfo).toList();
return allUserWithRoles.stream().map(UserProfileResponse::new).toList();
}
@Override
@@ -91,7 +93,7 @@ public class UserProfileServcieImpl implements UserProfileServcie {
if (user == null) {
throw new BizException(ErrorCode.USER_NOT_EXISTS);
}
return userConverterUserinfo(user);
return new UserProfileResponse(user);
}
@Override
@@ -108,7 +110,7 @@ public class UserProfileServcieImpl implements UserProfileServcie {
if (i == 0) {
throw new BizException(ErrorCode.USER_UPDATE_NICKNAME_FAILED);
}
return userConverterUserinfo(user);
return new UserProfileResponse(user);
}
@Override
@@ -123,7 +125,7 @@ public class UserProfileServcieImpl implements UserProfileServcie {
if (i == 0) {
throw new BizException(ErrorCode.USER_UPDATE_NICKNAME_FAILED);
}
return userConverterUserinfo(user);
return new UserProfileResponse(user);
}
@Override
@@ -148,30 +150,17 @@ public class UserProfileServcieImpl implements UserProfileServcie {
if (i == 0) {
throw new BizException(ErrorCode.USER_UPDATE_PROFILE_FAILED);
}
return userConverterUserinfo(user);
return new UserProfileResponse(user);
}
@Override
public IPage<UserProfileResponse> getAllUsers(Integer pageNum, Integer pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
IPage<User> userPage = userMapper.selectPageWithRoles(page, null);
return userPage.convert(UserProfileResponse::new);
}
/**
* User UserinfoResponse
* @param user 用户
* @return 用户信息
*/
private UserProfileResponse userConverterUserinfo(User user) {
UserProfileResponse userProfileResponse = new UserProfileResponse();
userProfileResponse.setId(user.getId());
userProfileResponse.setUsername(user.getUsername());
userProfileResponse.setNickname(user.getNickname());
userProfileResponse.setRoles(
user.getRoles().stream().map(role -> {
RoleResponse roleResponse = new RoleResponse();
roleResponse.setId(role.getId());
roleResponse.setName(role.getName());
return roleResponse;
}).toList()
);
return userProfileResponse;
}