|
@@ -0,0 +1,198 @@
|
|
|
+package com.gihon.controller;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.gihon.common.web.response.PageBean;
|
|
|
+import com.gihon.common.web.response.PageResponse;
|
|
|
+import com.gihon.common.web.response.Response;
|
|
|
+import com.gihon.common.web.response.ResponseStatus;
|
|
|
+import com.gihon.configSecurity.encoder.MyPasswordEncoder;
|
|
|
+import com.gihon.entity.User;
|
|
|
+import com.gihon.service.UsersService;
|
|
|
+import com.gihon.sso.entity.vo.LoginUser;
|
|
|
+import com.gihon.sso.entity.vo.SsoToken;
|
|
|
+import com.gihon.sso.entity.vo.UserInfo;
|
|
|
+import com.gihon.sso.service.TokenService;
|
|
|
+import com.gihon.sso.service.UserService;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户控制器
|
|
|
+ *
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/user")
|
|
|
+@Api(value = "用户接口", tags = "用户接口")
|
|
|
+public class UserController {
|
|
|
+ @Autowired
|
|
|
+ private UsersService usersService;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private TokenService tokenService;
|
|
|
+ /**
|
|
|
+ * 主要功能:分页查询用户列表
|
|
|
+ * @data 2021/2/19 9:45
|
|
|
+ * @author: dl
|
|
|
+ * @description 分页查看用户
|
|
|
+ * @param page 页数
|
|
|
+ * @param rows 页小大
|
|
|
+ * @return com.gihon.common.http.Response
|
|
|
+ */
|
|
|
+ //@Secured(value = {"ROLE_管理员", "ROLE_超级管理员"})
|
|
|
+ @PreAuthorize("hasAuthority('sys:user:view')")
|
|
|
+ @ApiOperation("分页查询用户列表")
|
|
|
+ @GetMapping("/getUsersByPage")
|
|
|
+ public PageResponse<PageBean<User>> getUsersByPage(
|
|
|
+ @RequestParam(required = false,defaultValue = "0") int page,
|
|
|
+ @RequestParam(required = false, defaultValue = "10") int rows) {
|
|
|
+ PageResponse byPage = usersService.getByPage(page, rows);
|
|
|
+ return byPage;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 主要功能:查询 用户信息by用户ID
|
|
|
+ * @data 2021/2/19 10:05
|
|
|
+ * @author: dl
|
|
|
+ * @description 根据用户主键
|
|
|
+ * 查找用户信息
|
|
|
+ * @param id
|
|
|
+ * @return com.gihon.common.http.Response
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @ApiOperation("查询 用户信息by用户ID")
|
|
|
+ @GetMapping("/userByID")
|
|
|
+ public Response userGetOne(@RequestParam Long id){
|
|
|
+ User userInfo = usersService.getById(id);
|
|
|
+ return Response.ok(userInfo);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 主要功能:
|
|
|
+ * @description 根据用户id 查找对应权限
|
|
|
+ * @author: dl
|
|
|
+ * @data 2021/2/20 14:24
|
|
|
+ * @param id 用户id
|
|
|
+ * @return com.gihon.common.http.Response<java.util.List<java.lang.String>>
|
|
|
+ */
|
|
|
+ @ApiOperation("根据用户id 查找对应权限")
|
|
|
+ @GetMapping("/getPermissionsByUserId")
|
|
|
+ public Response<List<String>> getPermissionsByUserId(Long id){
|
|
|
+ List<String> permissions = usersService.getPermissionsByUserId(id);
|
|
|
+ return Response.ok(permissions);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 主要功能:增加用户
|
|
|
+ * @data 2021/2/19 9:48
|
|
|
+ * @author: dl
|
|
|
+ * @description 根据输入用户信息 增加数据
|
|
|
+ * 不允许增加重复用户
|
|
|
+ * 使用自定义加密器加密用户输入密码后 存储
|
|
|
+ *
|
|
|
+ * @param user
|
|
|
+ * @return com.gihon.common.http.Response
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+ //@Secured(value = {"addUser"})
|
|
|
+ @ApiOperation("增加用户")
|
|
|
+ @PostMapping("/addUser")
|
|
|
+ public Response<User> addUser(@RequestBody User user) {
|
|
|
+ Response<User> addUser = usersService.addUser(user);
|
|
|
+ return Response.ok();
|
|
|
+ }
|
|
|
+ //增加用户角色
|
|
|
+ /**
|
|
|
+ * 主要功能:
|
|
|
+ * @description 用户(ById) 增加 角色(ById)
|
|
|
+ * @author: dl
|
|
|
+ * @data 2021/2/23 15:12
|
|
|
+ * @param user
|
|
|
+ * @param RoleIds
|
|
|
+ * @return com.gihon.common.web.response.Response
|
|
|
+ */
|
|
|
+ @ApiOperation("增加用户的角色")
|
|
|
+ @PostMapping("/addRole4User")
|
|
|
+ @ResponseBody
|
|
|
+ public Response addRole4User(@RequestBody User user,@RequestParam("RoleIds") Long[] RoleIds){
|
|
|
+ Response response = usersService.addRole4User(user,RoleIds);
|
|
|
+ return Response.ok();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 主要功能:删除用户
|
|
|
+ * @data 2021/2/19 9:50
|
|
|
+ * @author: dl
|
|
|
+ * @description 根据用户主键
|
|
|
+ * 删除用户信息
|
|
|
+ * @param id
|
|
|
+ * @return com.gihon.common.http.Response
|
|
|
+ */
|
|
|
+ @ApiOperation("删除用户")
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @GetMapping(value = "/deleteUser")
|
|
|
+ public Response deleteUser(@RequestParam Long id) {
|
|
|
+ //int i = userMapper.deleteById(id);
|
|
|
+ boolean deleteUser = usersService.removeById(id);
|
|
|
+ //boolean removeRole2User = userService.removeRole2User(id);
|
|
|
+ //添加数据失败
|
|
|
+ if (!deleteUser) {
|
|
|
+ return Response.error(ResponseStatus.MAPPER_ERROR,"删除用户失败");
|
|
|
+ }
|
|
|
+ return Response.ok();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("根据用户名 修改密码")
|
|
|
+ @PostMapping(value = "/modifyUser")
|
|
|
+ /**
|
|
|
+ * 主要功能:修改密码
|
|
|
+ * @data 2021/2/19 9:51
|
|
|
+ * @author: dl
|
|
|
+ * @description 根据 用户名密码
|
|
|
+ * 修改用户密码
|
|
|
+ * @param username
|
|
|
+ * @param password
|
|
|
+ * @return com.gihon.common.http.Response<com.gihon.user.entity.User>
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public Response<User> modifyUser(String username, String password) {
|
|
|
+ //修改新密码
|
|
|
+ MyPasswordEncoder encoder = new MyPasswordEncoder();
|
|
|
+ User user = User.builder().password(encoder.encode(password)).build();
|
|
|
+ //根据用户名
|
|
|
+ boolean update = usersService.update(user, new QueryWrapper<User>()
|
|
|
+ .eq("account", username)
|
|
|
+ );
|
|
|
+ //修改结果
|
|
|
+ if (!update) {
|
|
|
+ return Response.error(ResponseStatus.MAPPER_ERROR,"用户未存在、或修改密码访问数据库失败");
|
|
|
+ }
|
|
|
+ return Response.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @ApiOperation("登录接口")
|
|
|
+ @PostMapping("/login")
|
|
|
+ public Response<SsoToken> login(@RequestBody LoginUser loginUser){
|
|
|
+ log.debug("SSO登录开始:{}", loginUser.getUserAccount());
|
|
|
+ UserInfo userInfo = userService.queryUserForLogin(loginUser.getUserAccount(),loginUser.getPassWord());
|
|
|
+ SsoToken token = null;
|
|
|
+ Response<SsoToken> result = null;
|
|
|
+ if(userInfo!=null) {
|
|
|
+ token = tokenService.createToken(userInfo);
|
|
|
+
|
|
|
+ result = Response.ok(token);
|
|
|
+ }else {
|
|
|
+ result = Response.error(ResponseStatus.LOGIN_ERROR);
|
|
|
+ }
|
|
|
+ log.debug("SSO登录结束:{}/{}", loginUser.getUserAccount(),token);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|