UACC接口服務調用

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());
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章