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:
@@ -0,0 +1,21 @@
|
||||
package cn.nopj.chaos_api.config;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@MapperScan("cn.nopj.chaos_api.mapper")
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MARIADB));
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,8 @@ import cn.nopj.chaos_api.dto.request.UpdateUsernameRequest;
|
||||
import cn.nopj.chaos_api.dto.request.UserProfileUpdateRequest;
|
||||
import cn.nopj.chaos_api.dto.response.UserProfileResponse;
|
||||
import cn.nopj.chaos_api.model.ApiResult;
|
||||
import cn.nopj.chaos_api.service.UserProfileServcie;
|
||||
import cn.nopj.chaos_api.service.UserProfileService;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@@ -26,7 +27,7 @@ import java.util.List;
|
||||
@RequestMapping("/api/user")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserProfileServcie userProfileServcie;
|
||||
private UserProfileService userProfileService;
|
||||
|
||||
/**
|
||||
* 获取所有用户信息
|
||||
@@ -34,8 +35,8 @@ public class UserController {
|
||||
*/
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@GetMapping("/all")
|
||||
ApiResult<List<UserProfileResponse>> getAllUsers(){
|
||||
return ApiResult.success(userProfileServcie.getAllUsers());
|
||||
ApiResult<IPage<UserProfileResponse>> getAllUsers(@RequestParam(defaultValue = "1" , name = "pageNum") Integer pageNum, @RequestParam(defaultValue = "10", name="pageSize") Integer pageSize){
|
||||
return ApiResult.success(userProfileService.getAllUsers(pageNum, pageSize));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,7 +47,7 @@ public class UserController {
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@PutMapping("/setUserPassword")
|
||||
ApiResult<String> setUserPassword(@RequestBody @Validated SetUserPasswordRequest request){
|
||||
userProfileServcie.setUserPassword(request.getUserId(), request.getPassword());
|
||||
userProfileService.setUserPassword(request.getUserId(), request.getPassword());
|
||||
return ApiResult.success("用户密码修改成功");
|
||||
}
|
||||
|
||||
@@ -57,7 +58,7 @@ public class UserController {
|
||||
*/
|
||||
@PutMapping("/updateUsername")
|
||||
ApiResult<String> updateUsername(@RequestAttribute("currentUsername") String username,@RequestBody @Validated UpdateUsernameRequest request){
|
||||
userProfileServcie.updateUsername(username, request.getUsername());
|
||||
userProfileService.updateUsername(username, request.getUsername());
|
||||
return ApiResult.success("用户名更新成功");
|
||||
}
|
||||
|
||||
@@ -68,7 +69,7 @@ public class UserController {
|
||||
*/
|
||||
@GetMapping("/profile")
|
||||
ApiResult<UserProfileResponse> getCurrentUserProfile(@RequestAttribute("currentUsername") String username){
|
||||
return ApiResult.success(userProfileServcie.findUserWithRoles(username));
|
||||
return ApiResult.success(userProfileService.findUserWithRoles(username));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,7 +80,7 @@ public class UserController {
|
||||
*/
|
||||
@PatchMapping("/profile")
|
||||
ApiResult<UserProfileResponse> getUserInfo(@RequestAttribute("currentUsername") String username, @RequestBody @Validated UserProfileUpdateRequest request){
|
||||
return ApiResult.success(userProfileServcie.updateUserProfile(username,request));
|
||||
return ApiResult.success(userProfileService.updateUserProfile(username,request));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,6 +90,6 @@ public class UserController {
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@PutMapping("/{userId}/nickname")
|
||||
ApiResult<UserProfileResponse> setNickname(@PathVariable Long userId, @RequestBody @Validated SetUserNicknameRequest request){
|
||||
return ApiResult.success(userProfileServcie.setUserNickname(userId,request.getNickname()));
|
||||
return ApiResult.success(userProfileService.setUserNickname(userId,request.getNickname()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import cn.nopj.chaos_api.common.exceotion.BizException;
|
||||
import cn.nopj.chaos_api.domain.entity.Role;
|
||||
import cn.nopj.chaos_api.domain.entity.User;
|
||||
import cn.nopj.chaos_api.dto.response.AuthTokenResponse;
|
||||
import cn.nopj.chaos_api.dto.response.UserProfileResponse;
|
||||
import cn.nopj.chaos_api.mapper.RoleMapper;
|
||||
import cn.nopj.chaos_api.mapper.UserMapper;
|
||||
import cn.nopj.chaos_api.service.AuthService;
|
||||
@@ -79,9 +80,16 @@ public class AuthServiceImpl implements AuthService {
|
||||
if (!userDetails.isEnabled()) {
|
||||
throw new BizException(ErrorCode.USER_NOT_ENABLED);
|
||||
}
|
||||
|
||||
User user = userMapper.findUserWithRolesByUsername(username);
|
||||
UserProfileResponse userProfileResponse = new UserProfileResponse(user);
|
||||
|
||||
AuthTokenResponse res = new AuthTokenResponse();
|
||||
res.setToken(jwtTokenUtil.generateToken(userDetails));
|
||||
res.setTokenHead(tokenHead);
|
||||
res.setUserProfile(userProfileResponse);
|
||||
|
||||
|
||||
return res;
|
||||
|
||||
}catch (BadCredentialsException | InternalAuthenticationServiceException e) {
|
||||
|
||||
@@ -35,6 +35,7 @@ mybatis-plus:
|
||||
map-underscore-to-camel-case: true
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
||||
|
||||
jwt:
|
||||
tokenHeader: Authorization
|
||||
tokenHead: Chaos
|
||||
|
||||
Reference in New Issue
Block a user