All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.javaclub.base.web.AdminUserController Maven / Gradle / Ivy
package com.github.javaclub.base.web;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.javaclub.base.annotation.SysLog;
import com.github.javaclub.base.annotation.WithApiResult;
import com.github.javaclub.base.domain.AdminUser;
import com.github.javaclub.base.param.UserLoginParam;
import com.github.javaclub.base.service.AdminUserProfileBuilder;
import com.github.javaclub.base.service.AdminUserService;
import com.github.javaclub.base.service.impl.TokenStoreService;
import com.github.javaclub.base.utils.SecurityUtils;
import com.github.javaclub.base.utils.ConfigUtils;
import com.github.javaclub.sword.BizException;
import com.github.javaclub.sword.annotation.swagger.ApiRequestObject;
import com.github.javaclub.sword.annotation.swagger.ApiRequestProperty;
import com.github.javaclub.sword.annotation.swagger.ApiResponseObject;
import com.github.javaclub.sword.annotation.swagger.ApiResponseProperty;
import com.github.javaclub.sword.core.BizObjects;
import com.github.javaclub.sword.core.Strings;
import com.github.javaclub.sword.domain.enumtype.BasicMessage;
import com.github.javaclub.toolbox.ToolBox.Maps;
import com.github.javaclub.toolbox.ToolBox.Web;
import com.github.javaclub.toolbox.enumtype.ActionType;
import com.github.javaclub.toolbox.enumtype.SysAccountEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
@RestController
@RequestMapping("/admin/user")
@WithApiResult
@Api(tags = "管理端: 账号行为")
@AllArgsConstructor
public class AdminUserController {
private final AdminUserService adminUserService;
private final TokenStoreService tokenStoreService;
private final AdminUserProfileBuilder adminUserProfileBuilder;
@PostMapping(value = "/login")
@ApiOperation(value = "登录系统")
@ApiResponseObject(name = "AdminUserLoginResp", description = "后台用户登录响应",
properties = {
@ApiResponseProperty(name = "token", description = "鉴权token", type = "string"),
@ApiResponseProperty(name = "userProfile", description = "用户基本信息", type = "object", refType = "AdminUser")
}
)
@SysLog(value = "用户登录", actionType = ActionType.LOGIN)
public Map login(@RequestBody UserLoginParam param) throws Exception {
BizObjects.requireNotEmpty(param.getAccount(), "登录账号不能为空!");
BizObjects.requireNotEmpty(param.getPassword(), "登录密码不能为空!");
AdminUser user = adminUserService.checkUserLogin(param.getAccount().trim(), param.getPassword().trim());
adminUserProfileBuilder.onLogin(user);
String token = tokenStoreService.generateAuthToken(SysAccountEnum.ADMIN.getValue(), user.getId(), user);
return Maps.createMap(
"token", token,
"userProfile", user
);
}
@GetMapping(value = "/logout")
@ApiOperation(value = "退出系统")
@ApiImplicitParam(value = "鉴权token", name = "token", paramType = "header", dataType = "string", required = true)
@SysLog(value = "退出系统", actionType = ActionType.LOGOUT)
public Boolean logout(@RequestHeader("token") String token) throws Exception {
BizObjects.requireTrue(Strings.isNotBlank(token), BasicMessage.NO_AUTHORIZED);
AdminUser user = checkAdminUser();
return tokenStoreService.deleteAllToken(SysAccountEnum.ADMIN.getValue(), user.getId(), token);
}
@GetMapping("/profile")
@ApiOperation(value = "获取登录用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true)
})
public AdminUser profile() {
AdminUser user = checkAdminUser();
return adminUserService.getById(user.getId());
}
@PostMapping("/alertPassword")
@ApiOperation(value = "用户修改登录密码")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true)
})
@ApiRequestObject(name = "AlertPasswordRequestParam", description = "修改密码请求参数",
properties = {
@ApiRequestProperty(name = "password", description = "原密码", type = "string", required = true),
@ApiRequestProperty(name = "newPassword", description = "新密码", type = "string", required = true)
}
)
@SysLog(value = "修改密码", actionType = ActionType.UPDATE)
public Boolean alertPassword(@RequestBody Map param) {
AdminUser admin = checkAdminUser();
AdminUser dbUser = adminUserService.selectById(admin.getId());
String password = ConfigUtils.generatePasswordMD5(param.get("password"));
if (!Strings.equals(password, dbUser.getPassword())) {
throw new BizException(BasicMessage.OLD_PWD_NOT_MATCH);
}
// 新密码
String newPassword = ConfigUtils.generatePasswordMD5(param.get("newPassword"));
// 更新密码
adminUserService.updatePasswordByUserId(dbUser.getId(), newPassword);
String token = Web.getCurrentRequestHeader("token");
// 当前token退出登录(退出所有已登录账号)
tokenStoreService.deleteAllToken(SysAccountEnum.ADMIN.getValue(), admin.getId(), null);
return true;
}
AdminUser checkAdminUser() {
return BizObjects.requireNotNull(SecurityUtils.getAdminUser(), BasicMessage.LOGIN_INVALID);
}
}