mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: finished DefaultBizLoginService
This commit is contained in:
parent
b26c4878f9
commit
926c8758ed
@ -0,0 +1,36 @@
|
||||
package tech.powerjob.common.utils;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* 加密工具
|
||||
*
|
||||
* @author tjq
|
||||
* @since 2023/3/25
|
||||
*/
|
||||
public class DigestUtils {
|
||||
|
||||
/**
|
||||
* 32位小写 md5
|
||||
* @param input 输入
|
||||
* @return md5
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static String md5(String input) {
|
||||
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
||||
md5.update(input.getBytes());
|
||||
byte[] byteArray = md5.digest();
|
||||
|
||||
BigInteger bigInt = new BigInteger(1, byteArray);
|
||||
// 参数16表示16进制
|
||||
StringBuilder result = new StringBuilder(bigInt.toString(16));
|
||||
// 不足32位高位补零
|
||||
while(result.length() < 32) {
|
||||
result.insert(0, "0");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package tech.powerjob.common.utils;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* md5
|
||||
*
|
||||
* @author tjq
|
||||
* @since 2023/3/25
|
||||
*/
|
||||
class DigestUtilsTest {
|
||||
|
||||
@Test
|
||||
void testMd5() {
|
||||
assert "0a7d83f084ec258aefd128569dda03d7".equals(DigestUtils.md5("6531"));
|
||||
assert "7906989e85cbc80207fd0db4b16806f6".equals(DigestUtils.md5("tjq"));
|
||||
assert "59cb4db84c02f5bee62fb0e6e02e758d".equals(DigestUtils.md5("PowerJob is a great job scheduling framework!"));
|
||||
assert "25adfe55fd639fcfd1c09e57ccddbd33".equals(DigestUtils.md5("HAHA"));
|
||||
}
|
||||
}
|
@ -1,13 +1,18 @@
|
||||
package tech.powerjob.server.auth.login.biz.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tech.powerjob.common.utils.DigestUtils;
|
||||
import tech.powerjob.server.auth.login.LoginContext;
|
||||
import tech.powerjob.server.auth.login.biz.BizLoginService;
|
||||
import tech.powerjob.server.auth.login.biz.BizUser;
|
||||
import tech.powerjob.server.common.SJ;
|
||||
import tech.powerjob.server.persistence.remote.model.UserInfoDO;
|
||||
import tech.powerjob.server.persistence.remote.repository.UserInfoRepository;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@ -25,6 +30,9 @@ public class DefaultBizLoginService implements BizLoginService {
|
||||
|
||||
public static final String DEFAULT_LOGIN_SERVICE = "PowerJob";
|
||||
|
||||
private static final String KEY_USERNAME = "username";
|
||||
private static final String KEY_PASSWORD = "password";
|
||||
|
||||
@Override
|
||||
public String type() {
|
||||
return DEFAULT_LOGIN_SERVICE;
|
||||
@ -33,6 +41,40 @@ public class DefaultBizLoginService implements BizLoginService {
|
||||
@Override
|
||||
public Optional<BizUser> login(LoginContext loginContext) {
|
||||
|
||||
final String loginInfo = loginContext.getLoginInfo();
|
||||
if (StringUtils.isEmpty(loginInfo)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Map<String, String> loginInfoMap = SJ.splitKvString(loginInfo);
|
||||
final String username = loginInfoMap.get(KEY_USERNAME);
|
||||
final String password = loginInfoMap.get(KEY_PASSWORD);
|
||||
|
||||
if (StringUtils.isAnyEmpty(username, password)) {
|
||||
log.debug("[DefaultBizLoginService] username or password is empty, login failed!");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Optional<UserInfoDO> userInfoOpt = userInfoRepository.findByUsername(username);
|
||||
if (!userInfoOpt.isPresent()) {
|
||||
log.debug("[DefaultBizLoginService] can't find user by username: {}", username);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final UserInfoDO dbUser = userInfoOpt.get();
|
||||
|
||||
if (s(username, password).equals(dbUser.getPassword())) {
|
||||
BizUser bizUser = new BizUser();
|
||||
bizUser.setUsername(username);
|
||||
return Optional.of(bizUser);
|
||||
}
|
||||
|
||||
log.debug("[DefaultBizLoginService] user[{}]'s password is not correct, login failed!", username);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static String s(String username, String password) {
|
||||
String f1 = String.format("%s_%s_z", username, password);
|
||||
return String.format("%s_%s_b", username, DigestUtils.md5(f1));
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package tech.powerjob.server.common;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Splitter;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Splitter & Joiner
|
||||
*
|
||||
@ -16,4 +18,10 @@ public class SJ {
|
||||
|
||||
public static final Joiner MONITOR_JOINER = Joiner.on("|").useForNull("-");
|
||||
|
||||
private static final Splitter.MapSplitter MAP_SPLITTER = Splitter.onPattern(";").withKeyValueSeparator(":");
|
||||
|
||||
public static Map<String, String> splitKvString(String kvString) {
|
||||
return MAP_SPLITTER.split(kvString);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user