mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: [auth] finished login part
This commit is contained in:
parent
cf8153ae39
commit
e18b9a8962
@ -9,4 +9,9 @@ package tech.powerjob.server.auth.common;
|
||||
public class AuthConstants {
|
||||
|
||||
public static final String JWT_NAME = "power_jwt";
|
||||
|
||||
/**
|
||||
* 前端跳转到指定页面指令
|
||||
*/
|
||||
public static final String FE_REDIRECT_KEY = "FE-REDIRECT:";
|
||||
}
|
||||
|
@ -14,6 +14,8 @@ import lombok.Getter;
|
||||
public enum AuthErrorCode {
|
||||
|
||||
USER_NOT_LOGIN("-100", "UserNotLoggedIn"),
|
||||
USER_NOT_EXIST("-101", "UserNotExist"),
|
||||
|
||||
|
||||
NO_PERMISSION("-200", "NoPermission"),
|
||||
|
||||
|
@ -28,7 +28,7 @@ public @interface ApiPermission {
|
||||
* 需要的权限
|
||||
* @return 权限
|
||||
*/
|
||||
Permission requiredPermission() default Permission.GLOBAL_SU;
|
||||
Permission requiredPermission() default Permission.SU;
|
||||
|
||||
/**
|
||||
* 固定权限不支持的场景,需要使用动态权限
|
||||
|
@ -9,6 +9,7 @@ import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tech.powerjob.common.exception.PowerJobException;
|
||||
import tech.powerjob.server.auth.login.*;
|
||||
import tech.powerjob.server.common.Loggers;
|
||||
@ -27,6 +28,7 @@ import java.nio.charset.StandardCharsets;
|
||||
* @author tjq
|
||||
* @since 2023/3/26
|
||||
*/
|
||||
@Service
|
||||
public class DingTalkLoginService implements ThirdPartyLoginService {
|
||||
|
||||
/*
|
||||
|
@ -1,14 +1,18 @@
|
||||
package tech.powerjob.server.auth.login.impl;
|
||||
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tech.powerjob.common.exception.PowerJobException;
|
||||
import tech.powerjob.common.serialize.JsonUtils;
|
||||
import tech.powerjob.server.auth.common.AuthConstants;
|
||||
import tech.powerjob.server.auth.common.AuthErrorCode;
|
||||
import tech.powerjob.server.auth.common.PowerJobAuthException;
|
||||
import tech.powerjob.server.auth.login.LoginTypeInfo;
|
||||
import tech.powerjob.server.auth.login.ThirdPartyLoginRequest;
|
||||
import tech.powerjob.server.auth.login.ThirdPartyLoginService;
|
||||
import tech.powerjob.server.auth.login.ThirdPartyUser;
|
||||
import tech.powerjob.server.common.Loggers;
|
||||
import tech.powerjob.server.common.SJ;
|
||||
import tech.powerjob.server.common.utils.DigestUtils;
|
||||
import tech.powerjob.server.persistence.remote.model.UserInfoDO;
|
||||
import tech.powerjob.server.persistence.remote.repository.UserInfoRepository;
|
||||
@ -36,18 +40,20 @@ public class PowerJobThirdPartyLoginService implements ThirdPartyLoginService {
|
||||
private static final String KEY_USERNAME = "username";
|
||||
private static final String KEY_PASSWORD = "password";
|
||||
|
||||
private static final String KEY_ENCRYPTION = "encryption";
|
||||
|
||||
@Override
|
||||
public LoginTypeInfo loginType() {
|
||||
return new LoginTypeInfo()
|
||||
.setType(POWER_JOB_LOGIN_SERVICE)
|
||||
.setName("PowerJob's built-in login system")
|
||||
.setName("PowerJob")
|
||||
;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateLoginUrl(HttpServletRequest httpServletRequest) {
|
||||
// 前端实现跳转,服务端返回特殊指令
|
||||
return "FE-REDIRECT:PowerJob";
|
||||
return AuthConstants.FE_REDIRECT_KEY.concat("powerjobLogin");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -57,19 +63,21 @@ public class PowerJobThirdPartyLoginService implements ThirdPartyLoginService {
|
||||
throw new IllegalArgumentException("can't find login Info");
|
||||
}
|
||||
|
||||
final Map<String, String> loginInfoMap = SJ.splitKvString(loginInfo);
|
||||
final String username = loginInfoMap.get(KEY_USERNAME);
|
||||
final String password = loginInfoMap.get(KEY_PASSWORD);
|
||||
Map<String, Object> loginInfoMap = JsonUtils.parseMap(loginInfo);
|
||||
|
||||
final String username = MapUtils.getString(loginInfoMap, KEY_USERNAME);
|
||||
final String password = MapUtils.getString(loginInfoMap, KEY_PASSWORD);
|
||||
final String encryption = MapUtils.getString(loginInfoMap, KEY_ENCRYPTION);
|
||||
|
||||
if (StringUtils.isAnyEmpty(username, password)) {
|
||||
Loggers.WEB.debug("[PowerJobLoginService] username or password is empty, login failed!");
|
||||
throw new IllegalArgumentException("username or password is empty!");
|
||||
throw new PowerJobAuthException(AuthErrorCode.INVALID_REQUEST);
|
||||
}
|
||||
|
||||
final Optional<UserInfoDO> userInfoOpt = userInfoRepository.findByUsername(username);
|
||||
if (!userInfoOpt.isPresent()) {
|
||||
Loggers.WEB.debug("[PowerJobLoginService] can't find user by username: {}", username);
|
||||
throw new PowerJobException("can't find user by username: " + username);
|
||||
throw new PowerJobAuthException(AuthErrorCode.USER_NOT_EXIST);
|
||||
}
|
||||
|
||||
final UserInfoDO dbUser = userInfoOpt.get();
|
||||
|
@ -141,7 +141,7 @@ public class PowerJobLoginServiceImpl implements PowerJobLoginService {
|
||||
// header、cookie 都能获取
|
||||
String jwtStr = httpServletRequest.getHeader(AuthConstants.JWT_NAME);
|
||||
if (StringUtils.isEmpty(jwtStr)) {
|
||||
for (Cookie cookie : httpServletRequest.getCookies()) {
|
||||
for (Cookie cookie : Optional.ofNullable(httpServletRequest.getCookies()).orElse(new Cookie[]{})) {
|
||||
if (cookie.getName().equals(AuthConstants.JWT_NAME)) {
|
||||
jwtStr = cookie.getValue();
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package tech.powerjob.server.core.service;
|
||||
|
||||
import tech.powerjob.common.utils.CommonUtils;
|
||||
import tech.powerjob.server.common.utils.DigestUtils;
|
||||
import tech.powerjob.server.persistence.remote.model.UserInfoDO;
|
||||
import tech.powerjob.server.persistence.remote.repository.UserInfoRepository;
|
||||
import com.google.common.base.Splitter;
|
||||
@ -30,8 +32,18 @@ public class UserService {
|
||||
* @param userInfoDO user
|
||||
*/
|
||||
public void save(UserInfoDO userInfoDO) {
|
||||
|
||||
CommonUtils.requireNonNull(userInfoDO.getUsername(), "userName can't be null or empty!");
|
||||
|
||||
userInfoDO.setGmtCreate(new Date());
|
||||
userInfoDO.setGmtModified(userInfoDO.getGmtCreate());
|
||||
|
||||
// 二次加密密码
|
||||
final String password = userInfoDO.getPassword();
|
||||
if (StringUtils.isNotEmpty(password)) {
|
||||
userInfoDO.setPassword(DigestUtils.rePassword(password, userInfoDO.getUsername()));
|
||||
}
|
||||
|
||||
userInfoRepository.saveAndFlush(userInfoDO);
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,11 @@ public class NamespaceDO {
|
||||
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 标签,扩展性之王,多值逗号分割
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 扩展字段
|
||||
*/
|
||||
|
@ -67,7 +67,8 @@ public class AuthController {
|
||||
* @return 登录结果
|
||||
*/
|
||||
@PostMapping("/thirdPartyLoginDirect")
|
||||
public ResultDTO<PowerJobUser> selfLogin(LoginRequest loginRequest, HttpServletResponse httpServletResponse) {
|
||||
public ResultDTO<PowerJobUser> selfLogin(@RequestBody LoginRequest loginRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
|
||||
loginRequest.setHttpServletRequest(httpServletRequest);
|
||||
try {
|
||||
final PowerJobUser powerJobUser = powerJobLoginService.doLogin(loginRequest);
|
||||
if (powerJobUser == null) {
|
||||
|
@ -14,6 +14,7 @@ public class ModifyUserInfoRequest {
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
private String nick;
|
||||
private String password;
|
||||
private String webHook;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user