UACC接口服务调用
准备工作
查看业务service层项目pom文件是否有引入uacc相关依赖,为后面接口调用做环境配置
<dependency> <groupId>com.hrt.framework.uac</groupId> <artifactId>unified-authentication-center-feign</artifactId> <version>1.0.0-SNAPSHOT</version> <scope>compile</scope> </dependency>
在application.yml文件配置FeiginClients,用于服务的发现
feginClients: name: #name属性会作为微服务的名称,用于服务发现 unified-authority-control-server: unified-authority-control-server-pig #这里对应自己使用的uacc项目应用名
在项目启动文件,如PlatformApplication里添加@EnableFeignClients、和@EnableDiscoveryClient注解。
@EnableFeignClients注解代表启用feign客户端;
@EnableDiscoveryClient 注解代表:注册中心不是是eureka,而是其他类的注册中心如consul。若为eureka则使用@EnableEurekaClient注解
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableScheduling @EnableFeignClients(basePackages = {"com.hrt.uacc.feign","com.hrt.framework.uac.feign.account","com.hrt.uacc.feign.dict","com.hrt.framework.uac.feign.accountapplication"}) @EnableDiscoveryClient @SpringBootApplication public class PlatformApplication { public static void main(String[] args) { SpringApplication.run(PlatformApplication.class, args); } }
@EnableFeignClients(basePackages = "com.hrt.uacc.feign”)
代表会扫描所在包的当前包及子包
添加账户
添加系统登录账户,主要使用到了uacc(unified-authority-control-center)项目和
uac(unified-authentication-center)项目所暴露的接口,可进入各项目的feign文件下自行查看。
uacc主要是提供用户信息角色相关的接口。
uac提供和登录账户相关的接口。
以下是uacc项目用户模块所暴露的接口:
@Repository @FeignClient(name = "${feginClients.name.unified-authority-control-server}", path = "user") public interface UserService { /** * @param vo * @return com.hrt.framework.web.core.Result * @author zhoubin * @description 新增用户 * @throw **/ @PostMapping("create") @ResponseBody Result create(@RequestBody @Validated UserAddVo vo); /** * @param username * @return com.hrt.framework.web.core.Result * @methodName getUserByUsername * @author boyd_chow * @description 根据用户名获取用户信息 * @date 2019/7/2 15:51 **/ @GetMapping("getUserByUsername") @ResponseBody Result getUserByUsername(@RequestParam("username") String username); /** * @param vo * @return com.hrt.framework.web.core.Result * @author zhoubin * @description 修改用户 * @throw **/ @PutMapping("edit") @ResponseBody Result edit(@RequestBody @Validated UserEditVo vo); @PostMapping("bindRoles") @ResponseBody Result bindRoles(@RequestBody UserRoleVo userRoleVo); /** * @param id * @return com.hrt.framework.web.core.Result * @author zhoubin * @description 删除用户 * @throw **/ @DeleteMapping("delete") @ResponseBody Result delete(@RequestParam("id") String id); /** * @param id * @return com.hrt.framework.web.core.Result * @description 根据id获取系统用户信息 * @throw **/ @GetMapping("get") @ResponseBody public Result get(@RequestParam("id") String id); /** * @param userRoleVo 根据userId 和 roleIdList * @return com.hrt.framework.web.core.Result * @description 用户解绑角色 * @throw **/ @DeleteMapping("unBindRoles") @ResponseBody public Result unBindRoles(@RequestBody UserRoleVo userRoleVo); }
以下是uac项目account模块所暴露的接口:
@Repository @FeignClient(name = "unified-authentication-center-server", path = "account") public interface AccountService { /** * @param vo * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 新增账号 * @throw **/ @PostMapping("create") @ResponseBody Result create(@RequestBody @Validated AccountAddVo vo); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 修改密码 * @throw **/ @PutMapping("updatePassword") @ResponseBody Result updatePassword(@RequestBody @Validated AccountEditVo vo); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 修改密码 * @throw **/ @PutMapping("updatePasswordNotRequiredOldPassword") @ResponseBody Result updatePasswordNotRequiredOldPassword(@RequestBody AccountEditVo vo); /** * @param id * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 删除账号 * @throw **/ @DeleteMapping("delete") @ResponseBody Result delete(@RequestParam("id") String id); /** * @param vo * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 分页查询账号 * @throw **/ @GetMapping("page") @ResponseBody Result page(@ModelAttribute AccountQueryVo vo); /** * @param id * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 根据id获取账号 * @throw **/ @GetMapping("get") @ResponseBody Result get(@RequestParam("id") String id); @GetMapping("getByUsername") @ResponseBody Result getByUsername(@RequestParam("username") String username); /** * @param idList * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 批量删除账号 * @throw **/ @DeleteMapping("batchDeleteAccounts") @ResponseBody Result batchDeleteAccounts(@RequestBody List<String> idList); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 修改启用状态 * @throw **/ @PutMapping("updateEnabled") @ResponseBody Result updateEnabled(@RequestBody AccountEditVo vo); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 修改改锁定状态 * @throw **/ @PutMapping("updateAccountNonLocked") @ResponseBody Result updateAccountNonLocked(@RequestBody AccountEditVo vo); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 修改账号过期时间 * @throw **/ @PutMapping("updateAccountExpiredTime") @ResponseBody Result updateAccountExpiredTime(@RequestBody AccountEditVo vo); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 修改凭证过期时间 * @throw **/ @PutMapping("updateCredentialsExpiredTime") @ResponseBody Result updateCredentialsExpiredTime(@RequestBody AccountEditVo vo); /** * @param username * @return com.hrt.framework.web.core.Result * @author boyd_chow * @description 根据用户名删除账号 * @throw **/ @DeleteMapping("deleteByUsername") @ResponseBody Result deleteByUsername(@RequestParam("username") String username); /** * @param usernameList * @return com.hrt.framework.web.core.Result * @author zhoubin * @description 根据用户名删除账号(批量) * @throw **/ @DeleteMapping("deleteByUsernames") @ResponseBody Result deleteByUsernames(@RequestBody List<String> usernameList); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author zhoubin * @description 修改自定义账号 * @throw **/ @PutMapping("updateAccount") @ResponseBody Result updateAccount(@RequestBody AccountEditVo vo); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author zhoubin * @description 修改自定义账号 * @throw **/ @PutMapping("updateMobile") @ResponseBody Result updateMobile(@RequestBody AccountEditVo vo); /** * @param vo, bindingResult * @return com.hrt.framework.web.core.Result * @author zhoubin * @description 修改自定义账号 * @throw **/ @PutMapping("updateEmail") @ResponseBody Result updateEmail(@RequestBody AccountEditVo vo); }
添加登录账户主要使用到的接口是:
UserService的: userService.create(userAddVo) ;//账户添加 userService.bindRoles(userRole) ;//角色绑定 AccountService的: accountService.updateEnabled(accountEditVo) ;//设置账户启用、禁用状态
示例代码:
service层
@Autowired private com.hrt.uacc.feign.user.UserService userService; @Autowired private AccountService accountService; /** * @return com.hrt.framework.web.core.Result * @Param [vo, userId] * @Author youjp * @Description //TODO 添加用户信息 * @throw **/ public Result addUser(UserAddVo vo, String currentUserId) throws GenericException { //账户信息存储 String userId=null; Result result=null; if(!StringUtils.isEmpty(vo.getUsername()) && !StringUtils.isEmpty(vo.getPassword())){ com.hrt.uacc.user.vo.UserAddVo userAddVo = new com.hrt.uacc.user.vo.UserAddVo(); userAddVo.setName(vo.getName); //用户名 userAddVo.setUsername(vo.getUsername()); //登录账户 userAddVo.setPassword(vo.getPassword()); //密码 userAddVo.setCreateUser(currentUserId); //创建人 userAddVo.setState(vo.getState()); //账户状态 userAddVo.setAppId("平台应用id"); //绑定应用id userAddVo.setLastModifyUser(currentUserId); result = userService.create(userAddVo); result = userService.create(userAddVo); if (ResultEnum.SUCCESS.getCode() == result.getCode()) { userId = result.getData().toString(); // 返回用户id,存入业务用户表,便于后面修改账户信息 //添加账户成功,绑定角色 UserRoleVo userRole = new UserRoleVo(); userRole.setUserId(userId); userRole.setCode("admin"); //传入角色编码,如管理员:admin result = userService.bindRoles(userRole); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } //设置账户状态,默认启用 AccountEditVo accountEditVo = new AccountEditVo(); accountEditVo.setUsername(username); accountEditVo.setCurrentUser(currentUserId); accountEditVo.setEnabled(true); accountEditVo.setMobile(vo.getPhone()); result = accountService.updateEnabled(accountEditVo); System.out.println("账户状态code"+result.getCode()); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } } else { throw new GenericException(result.getCode(), result.getMsg()); } } return Result.success(); }
修改用户
修改登录账户信息主要使用到的接口是:
UserService的: userService.get(userId) ;//获取用户信息 userService.edit(userEditVo) ;//修改用户信息 AccountService的: accountService.updateEnabled(accountEditVo) ;//设置账户启用、禁用状态 accountService.updateMobile(updatePhone) ; //修改登录账户手机号 accountService.updatePasswordNotRequiredOldPassword(accountEditVo); //修改密码,不需旧密码 accountService.updatePassword(accountEditVo); //修改密码,需要旧密码
示例代码:
service层
@Autowired private com.hrt.uacc.feign.user.UserService userService; @Autowired private AccountService accountService; @Autowired private AccountApplicationService accountApplicationService; /** * @return com.hrt.framework.web.core.Result * @Param [vo, currentUserId] * @Author youjp * @Description //TODO 修改用户信息 * @throw **/ public Result editUser(UserEditVo vo, String currentUserId) throws GenericException { //获取账户信息 Result result = null; String username= null; result=userService.get(vo.getId()); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); }else { if(!StringUtils.isEmpty(result.getData())){ com.hrt.uacc.user.po.User uaccUser = JsonUtils.parseObject(result.getData().toString(),com.hrt.uacc.user.po.User.class); username=uaccUser.getUsername(); //获取用户名 } } //启动或禁用用户账户、登录账户 if (!StringUtils.isEmpty(vo.getState()) && !StringUtils.isEmpty(username)) { com.hrt.uacc.user.vo.UserEditVo userEditVo = new com.hrt.uacc.user.vo.UserEditVo(); userEditVo.setId(vo.getId()); userEditVo.setState(checkState(vo.getState())); userEditVo.setUsername(username); userEditVo.setName(vo.getName()); result = userService.edit(userEditVo); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } AccountEditVo accountEditVo = new AccountEditVo(); accountEditVo.setUsername(username); accountEditVo.setCurrentUser(currentUserId); accountEditVo.setEnabled(checkState(vo.getState())); result = accountService.updateEnabled(accountEditVo); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } } //修改手机号码 if (!StringUtils.isEmpty(vo.getPhone()) && !StringUtils.isEmpty(username)) { com.hrt.uacc.user.vo.UserEditVo userEditVo = new com.hrt.uacc.user.vo.UserEditVo(); userEditVo.setId(vo.getId()); userEditVo.setUsername(username); userEditVo.setMobilePhone(vo.getPhone()); userEditVo.setName(vo.getName()); result = userService.edit(userEditVo); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } AccountEditVo updatePhone = new AccountEditVo(); updatePhone.setUsername(username); updatePhone.setCurrentUser(currentUserId); updatePhone.setMobile(vo.getPhone()); result = accountService.updateMobile(updatePhone); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } } //修改账户密码,不需旧密码 if (!StringUtils.isEmpty(vo.getNewpass()) && !StringUtils.isEmpty(username)) { AccountEditVo accountEditVo = new AccountEditVo(); accountEditVo.setUsername(username); accountEditVo.setNewPassword(vo.getNewpass()); accountEditVo.setCurrentUser(currentUserId); result = accountService.updatePasswordNotRequiredOldPassword(accountEditVo); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } } //修改角色、appId if (!StringUtils.isEmpty(vo.getRoleName()) && !StringUtils.isEmpty(username)) { //修改应用id AccountApplicationAddVo accountApplicationAddVo = new AccountApplicationAddVo(); accountApplicationAddVo.setUsername(username); accountApplicationAddVo.setApplicationIds(getAppIds(vo.getRoleName())); result = accountApplicationService.create(accountApplicationAddVo); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } //解绑角色 String userRoleName=getRoleNameByUserId(vo.getId()); //用户已拥有角色 UserRoleVo userRole = new UserRoleVo(); userRole.setUserId(vo.getId()); userRole.setRoleIdList(getRoleIds(userRoleName)); result=userService.unBindRoles(userRole); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } //绑定新角色 userRole = new UserRoleVo(); userRole.setUserId(vo.getId()); userRole.setCode(“角色编码”); //传入角色编码,如管理员:admin result = userService.bindRoles(userRole); if (result.getCode() != 200) { throw new GenericException(result.getCode(), result.getMsg()); } } return Result.success(); }
修改密码,需要旧密码
/** * @return com.hrt.framework.web.core.Result * @Param [] * @Author youjp * @Description //TODO 修改用户账户密码 * @throw **/ public Result updatePass(UserEditVo vo, String userId, String userName) { AccountEditVo accountEditVo = new AccountEditVo(); accountEditVo.setUsername(userName); accountEditVo.setNewPassword(vo.getNewpass()); accountEditVo.setOldPassword(vo.getPassword()); accountEditVo.setCurrentUser(userId); Result result = accountService.updatePassword(accountEditVo); //状态码为200时,密码修改成功 if (ResultEnum.SUCCESS.getCode() == result.getCode()) { return Result.success(); } else { throw new GenericException(result.getCode(), result.getMsg()); } }