认证(登录)功能实现
生活随笔
收集整理的這篇文章主要介紹了
认证(登录)功能实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在UAA工程的agent包中,新建AccountApiAgent類,作為調用統一用戶服務的Feign代理:
@FeignClient(value = "account-service") public interface AccountApiAgent {@PostMapping(value = "/account/l/accounts/session")RestResponse<AccountDTO> login(@RequestBody AccountLoginDTO accountLoginDTO); }注意檢查啟動類上的注解
@EnableFeignClients(basePackages = {"cn.uaa.agent"})在UAA工程的domain包中找到IntegrationUserDetailsAuthenticationHandler類,并實現其中的authentication方法:
/*** 認證處理過程* @param domain 用戶域 ,如b端用戶、c端用戶等,用戶擴展* @param authenticationType 認證類型,如密碼認證,短信認證等,用于擴展* @param token SpringSecurity的token對象,可提取用戶名、密碼等用于認證的信息* @return UnifiedUserDetails 登錄成功*/ public UnifiedUserDetails authentication(String domain, String authenticationType,UsernamePasswordAuthenticationToken token) {return null; }參數說明:
-
domain: 用戶域 ,說明了此次登錄的是b端用戶還是c端用戶,由接入方傳入
-
authenticationType: 說明了此次登錄的方式,如密碼認證,短信認證等,由接入方傳入
-
token: SpringSecurity的token對象,里面存放了接入方調用UAA接口進行認證時傳入的用戶名、密碼等需要驗證的信息
返回值說明:
-
UnifiedUserDetails SpringSecurity對象,用于存放登錄成功時返回的信息,比如賬號基本信息、權限、資源等。此方法返回該對象給Spring Security OAuth2框架,Spring Security OAuth2框架會根據里面的內容生成jwt令牌,從而使令牌中保存了登錄用戶的相關數據。
具體實現如下:
public UnifiedUserDetails authentication(String domain, String authenticationType,UsernamePasswordAuthenticationToken token) {//1.從客戶端取數據String username=token.getName();if(StringUtil.isBlank(username)){throw new BadCredentialsException("賬戶為空");}if(token.getCredentials()==null){throw new BadCredentialsException("密碼為空");}String presentedPassword=token.getCredentials().toString();//2.遠程調用統一賬戶服務,進行賬戶密碼校驗AccountLoginDTO accountLoginDTO=new AccountLoginDTO();accountLoginDTO.setDomain(domain);accountLoginDTO.setUsername(username);accountLoginDTO.setMobile(username);accountLoginDTO.setPassword(presentedPassword);AccountApiAgent accountApiAgent=(AccountApiAgent)ApplicationContextHelper.getBean(AccountApiAgent.class);RestResponse<AccountDTO> restResponse=accountApiAgent.login(accountLoginDTO);//3.異常處理if(restResponse.getCode()!=0){throw new BadCredentialsException("登錄失敗");}//4.登錄成功,把用戶數據封裝到UnifiedUserDetails對象中UnifiedUserDetails unifiedUserDetails=new UnifiedUserDetails(restResponse.getResult().getUsername(),presentedPassword,AuthorityUtils.createAuthorityList());unifiedUserDetails.setMobile(restResponse.getResult().getMobile());return unifiedUserDetails; }總結
以上是生活随笔為你收集整理的认证(登录)功能实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 认证(登录)功能需求分析
- 下一篇: 登录(生成令牌)