Browse Source

短信验证码登陆

master
liuxinxin 1 year ago
parent
commit
576ab537db
3 changed files with 51 additions and 8 deletions
  1. +11
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/constants/UserDeatilsServiceConstant.java
  2. +14
    -7
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialAuthProvider.java
  3. +26
    -1
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialLoginUserDetailService.java

+ 11
- 0
pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/constants/UserDeatilsServiceConstant.java View File

@@ -0,0 +1,11 @@
package com.ningdatech.pmapi.user.security.auth.constants;

/**
* @author liuxinxin
* @date 2023/2/14 上午11:29
*/

public class UserDeatilsServiceConstant {

public static final String USER_DETAILS_SERVICE_SEPARATOR = "@###@";
}

+ 14
- 7
pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialAuthProvider.java View File

@@ -1,6 +1,8 @@
package com.ningdatech.pmapi.user.security.auth.credential;

import com.ningdatech.basic.model.GenericResult;
import com.ningdatech.pmapi.user.constant.LoginTypeEnum;
import com.ningdatech.pmapi.user.security.auth.constants.UserDeatilsServiceConstant;
import com.ningdatech.zwdd.client.ZwddAuthClient;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
@@ -30,19 +32,25 @@ public class CredentialAuthProvider implements AuthenticationProvider {
throw new RuntimeException("CustomAuthProvider 只支持 CustomAuthToken");
}
CredentialAuthToken authenticationToken = (CredentialAuthToken) authentication;
String credentials = (String) authenticationToken.getCredentials();
String principal = (String) authenticationToken.getPrincipal();

UserDetails user = null;
LoginTypeEnum loginTypeEnum = authenticationToken.getLoginTypeEnum();
switch (loginTypeEnum) {
case DING_QR_LOGIN: {
// TODO 补充浙政钉扫码逻辑
user = userDetailsService.loadUserByUsername((String) authenticationToken.getPrincipal());
String code = (String) authenticationToken.getCredentials();
GenericResult<String> accountResult = zwddAuthClient.getAccountId(code);
if (accountResult.isSuccess()) {
throw new BadCredentialsException("login fail! 浙政钉校验失败");
}
String accountId = accountResult.getData();

user = userDetailsService.loadUserByUsername(accountId + UserDeatilsServiceConstant.USER_DETAILS_SERVICE_SEPARATOR + loginTypeEnum.name());
}
break;
case PHONE_VERIFICATION_CODE_LOGIN: {
// TODO
user = userDetailsService.loadUserByUsername((String) authenticationToken.getPrincipal());
// TODO 校验短信验证码
user = userDetailsService.loadUserByUsername(principal + UserDeatilsServiceConstant.USER_DETAILS_SERVICE_SEPARATOR + loginTypeEnum.name());
}
break;
case USERNAME_PASSWORD_LOGIN: {
@@ -55,8 +63,6 @@ public class CredentialAuthProvider implements AuthenticationProvider {
}
break;
}


// 将用户定义的user放入token中,这样可以在session中查询到所有自定义的用户信息
return new CredentialAuthToken(user, user.getPassword(), user.getAuthorities());
}
@@ -72,6 +78,7 @@ public class CredentialAuthProvider implements AuthenticationProvider {
}
}


@Override
public boolean supports(Class<?> authentication) {
return CredentialAuthToken.class.isAssignableFrom(authentication);


+ 26
- 1
pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialLoginUserDetailService.java View File

@@ -1,7 +1,9 @@
package com.ningdatech.pmapi.user.security.auth.credential;


import com.ningdatech.pmapi.user.constant.LoginTypeEnum;
import com.ningdatech.pmapi.user.manage.UserAuthLoginManage;
import com.ningdatech.pmapi.user.security.auth.constants.UserDeatilsServiceConstant;
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import lombok.RequiredArgsConstructor;
@@ -24,7 +26,30 @@ public class CredentialLoginUserDetailService implements UserDetailsService {

@Override
public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserFullInfoDTO userFullInfoDTO = userAuthLoginManage.queryUserInfoInPasswordAuth(username);
String[] split = username.split(UserDeatilsServiceConstant.USER_DETAILS_SERVICE_SEPARATOR);
username = split[0];
String loginTypeStr = split[1];
LoginTypeEnum loginTypeEnum = LoginTypeEnum.valueOf(loginTypeStr);

UserFullInfoDTO userFullInfoDTO = null;
switch (loginTypeEnum) {
case PHONE_VERIFICATION_CODE_LOGIN: {
userFullInfoDTO = userAuthLoginManage.queryUserInfoInPhoneNoAuth(username);
}
break;
case USERNAME_PASSWORD_LOGIN: {
userFullInfoDTO = userAuthLoginManage.queryUserInfoInPasswordAuth(username);
}
break;
case DING_QR_LOGIN: {
userFullInfoDTO = userAuthLoginManage.queryUserInfoInAccountIdAuth(username);
}
break;
default: {
throw new UsernameNotFoundException(String.format("%s user not exist", username));
}
}

if (Objects.isNull(userFullInfoDTO)) {
throw new UsernameNotFoundException(String.format("%s user not exist", username));
}


Loading…
Cancel
Save