11 changed files with 262 additions and 12 deletions
Unified View
Diff Options
-
33src/main/java/com/qniao/zsh/api/command/login/admin/LoginAdminCommandController.java
-
22src/main/java/com/qniao/zsh/api/command/login/admin/request/LoginDto.java
-
21src/main/java/com/qniao/zsh/api/command/login/admin/response/LoginInfoVo.java
-
20src/main/java/com/qniao/zsh/api/command/login/admin/response/LoginVo.java
-
75src/main/java/com/qniao/zsh/application/service/login/LoginApplicationService.java
-
14src/main/java/com/qniao/zsh/domain/aggregate/user/entity/User.java
-
28src/main/java/com/qniao/zsh/infrastructure/config/LoginRequestConfig.java
-
37src/main/java/com/qniao/zsh/infrastructure/exception/AuthExceptionCode.java
-
13src/main/java/com/qniao/zsh/infrastructure/exception/LoginFailedException.java
-
9src/main/resources/application-local.yml
-
2src/main/resources/application.yml
@ -0,0 +1,22 @@ |
|||||
|
package com.qniao.zsh.api.command.login.admin.request; |
||||
|
|
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.validation.constraints.NotNull; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @author wh |
||||
|
* @date 2023/4/6 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class LoginDto { |
||||
|
@NotNull(message = "用户名不能为空") |
||||
|
private String username; |
||||
|
@NotNull(message = "密码不能为空") |
||||
|
private String password; |
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
@ -0,0 +1,21 @@ |
|||||
|
package com.qniao.zsh.api.command.login.admin.response; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
/** |
||||
|
* @author wh |
||||
|
* @date 2023/4/8 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class LoginInfoVo { |
||||
|
|
||||
|
private String userId; |
||||
|
|
||||
|
private String realName; |
||||
|
|
||||
|
private String avatar; |
||||
|
|
||||
|
private String desc; |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
package com.qniao.zsh.api.command.login.admin.response; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
/** |
||||
|
* @author wh |
||||
|
* @date 2023/4/7 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class LoginVo { |
||||
|
private String userId; |
||||
|
|
||||
|
private String token; |
||||
|
|
||||
|
private String role; |
||||
|
|
||||
|
private Integer code; |
||||
|
|
||||
|
private String message; |
||||
|
} |
||||
@ -0,0 +1,75 @@ |
|||||
|
package com.qniao.zsh.application.service.login; |
||||
|
|
||||
|
import cn.hutool.core.util.StrUtil; |
||||
|
import cn.hutool.http.Header; |
||||
|
import cn.hutool.http.HttpRequest; |
||||
|
import cn.hutool.http.HttpResponse; |
||||
|
import cn.hutool.json.JSONObject; |
||||
|
import cn.hutool.json.JSONUtil; |
||||
|
import com.qniao.zsh.api.command.login.admin.response.LoginInfoVo; |
||||
|
import com.qniao.zsh.api.command.login.admin.response.LoginVo; |
||||
|
import com.qniao.zsh.infrastructure.config.LoginRequestConfig; |
||||
|
import com.qniao.zsh.infrastructure.exception.LoginFailedException; |
||||
|
import org.springframework.scheduling.annotation.EnableAsync; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* @author wh |
||||
|
* @date 2023/4/6 |
||||
|
*/ |
||||
|
@Service |
||||
|
@EnableAsync |
||||
|
public class LoginApplicationService { |
||||
|
public LoginVo requestOps(String username, String password) { |
||||
|
//链式构建请求 |
||||
|
Map<String, Object> paramMap = new HashMap<>(2); |
||||
|
paramMap.put("username", username); |
||||
|
paramMap.put("password", password); |
||||
|
HttpResponse response = HttpRequest.post(LoginRequestConfig.LOGINURL) |
||||
|
.form(paramMap) |
||||
|
.timeout(20000) |
||||
|
.execute(); |
||||
|
LoginVo loginVo = new LoginVo(); |
||||
|
loginVo.setToken(response.header(Header.AUTHORIZATION)); |
||||
|
if (loginVo.getToken() == null){ |
||||
|
throw new LoginFailedException(); |
||||
|
}else { |
||||
|
String loginInfo = HttpRequest.get(LoginRequestConfig.LOGININFOURL) |
||||
|
.header(Header.AUTHORIZATION, loginVo.getToken()) |
||||
|
.timeout(20000) |
||||
|
.execute().body(); |
||||
|
JSONObject jsonInfo = JSONUtil.parseObj(loginInfo); |
||||
|
loginVo.setCode(Integer.valueOf(jsonInfo.getStr("code"))); |
||||
|
loginVo.setMessage(jsonInfo.getStr("message")); |
||||
|
//重新从返回的data赋值 |
||||
|
String jsonData = jsonInfo.getStr("data"); |
||||
|
jsonInfo = JSONUtil.parseObj(jsonData); |
||||
|
loginVo.setUserId(jsonInfo.getStr("adminId")); |
||||
|
loginVo.setRole(jsonInfo.getStr("position")); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
return loginVo; |
||||
|
} |
||||
|
public LoginInfoVo loginInfo(String token){ |
||||
|
LoginInfoVo loginInfoVo = new LoginInfoVo(); |
||||
|
String loginInfo = HttpRequest.get(LoginRequestConfig.LOGININFOURL) |
||||
|
.header(Header.AUTHORIZATION, token) |
||||
|
.timeout(20000) |
||||
|
.execute().body(); |
||||
|
JSONObject jsonInfo = JSONUtil.parseObj(loginInfo); |
||||
|
String jsonData = jsonInfo.getStr("data"); |
||||
|
jsonInfo = JSONUtil.parseObj(jsonData); |
||||
|
loginInfoVo.setUserId(jsonInfo.getStr("adminId")); |
||||
|
loginInfoVo.setRealName(jsonInfo.getStr("name")); |
||||
|
loginInfoVo.setAvatar(jsonInfo.getStr("avatar")); |
||||
|
loginInfoVo.setDesc(jsonInfo.getStr("position")); |
||||
|
|
||||
|
return loginInfoVo; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
package com.qniao.zsh.domain.aggregate.user.entity; |
||||
|
|
||||
|
import org.mc.ddd.infrastructure.persistent.Entity; |
||||
|
|
||||
|
/** |
||||
|
* @author wh |
||||
|
* @date 2023/4/6 |
||||
|
*/ |
||||
|
public class User extends Entity<User> { |
||||
|
/** |
||||
|
* 头像地址 |
||||
|
*/ |
||||
|
private String avatarUrl; |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
package com.qniao.zsh.infrastructure.config; |
||||
|
|
||||
|
import org.springframework.beans.factory.InitializingBean; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
|
||||
|
/** |
||||
|
* @author wh |
||||
|
* @date |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class LoginRequestConfig implements InitializingBean { |
||||
|
@Value("${login-request}") |
||||
|
private String loginUrl; |
||||
|
|
||||
|
@Value("${loginInfo-request}") |
||||
|
private String loginInfo; |
||||
|
|
||||
|
public static String LOGINURL; |
||||
|
public static String LOGININFOURL; |
||||
|
|
||||
|
@Override |
||||
|
public void afterPropertiesSet() { |
||||
|
LOGINURL = loginUrl; |
||||
|
LOGININFOURL = loginInfo; |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
package com.qniao.zsh.infrastructure.exception; |
||||
|
|
||||
|
public interface AuthExceptionCode { |
||||
|
int AuthMethodKeyDuplication = 51001; |
||||
|
|
||||
|
int AuthMethodProcessorNull = 51002; |
||||
|
|
||||
|
int ExistedChildrenItem = 51003; |
||||
|
|
||||
|
int SendingIntervalTooShort = 51004; |
||||
|
|
||||
|
int CaptchaInvalid = 51005; |
||||
|
|
||||
|
int AuthSessionNotExisted = 51006; |
||||
|
|
||||
|
int AccountFieldNotExisted = 51007; |
||||
|
|
||||
|
int PasswordFieldNotExisted = 51008; |
||||
|
|
||||
|
int AccountInvalid = 51009; |
||||
|
|
||||
|
int PasswordInvalid = 51010; |
||||
|
|
||||
|
int PasswordReInputError = 51011; |
||||
|
|
||||
|
int NotMatchItem = 51012; |
||||
|
|
||||
|
int AccountPatternError = 51013; |
||||
|
|
||||
|
int PasswordPatternError = 51014; |
||||
|
|
||||
|
int PasswordEncodingError = 51015; |
||||
|
|
||||
|
int PasswordNotMatchUser = 51016; |
||||
|
|
||||
|
int AccountExisted = 51017; |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
package com.qniao.zsh.infrastructure.exception; |
||||
|
|
||||
|
import org.mc.ddd.infrastructure.exception.BizException; |
||||
|
|
||||
|
/** |
||||
|
* @author wh |
||||
|
* @date 2023/4/7 |
||||
|
*/ |
||||
|
public class LoginFailedException extends BizException { |
||||
|
public LoginFailedException() { |
||||
|
super(AuthExceptionCode.PasswordNotMatchUser,"账号或密码错误"); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save