当前位置:网站首页>Use the same interface to realize different login methods
Use the same interface to realize different login methods
2022-07-23 08:14:00 【zhou_ zhao_ xu】
List of articles
- Define parent class DTO
package com.zzx.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.Data;
@Data
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "productCode",
visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = UserPasswordDTO.class, name = LoginDTO.USERNAME_PASSWORD),
@JsonSubTypes.Type(value = UserPhoneDTO.class,name = LoginDTO.PHONE_SMS_PRODUCT),
})
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginDTO {
public static final String PHONE_SMS_PRODUCT = "phoneSms";
public static final String USERNAME_PASSWORD = "password";
private String productCode;
public LoginDTO() {
}
}
Define subclasses that require login types
- Account password login
package com.zzx.dto; import lombok.Data; @Data public class UserPasswordDTO extends LoginDTO { private String userName; private String password; @Override public String getProductCode() { return LoginDTO.USERNAME_PASSWORD; } } - Mobile number verification code login
package com.zzx.dto; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data public class UserPhoneDTO extends LoginDTO { /** * Location code */ private String areaCode; /** * cell-phone number */ private String phoneNum; /** * Verification Code */ private String verifyCode; @Override public String getProductCode() { return LoginDTO.PHONE_SMS_PRODUCT; } }
- Account password login
test ( Be careful , Incoming productCode Must be with @JsonSubTypes.Type Defined in name The value is consistent. )
package com.zzx;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzx.dto.LoginDTO;
import com.zzx.dto.UserPasswordDTO;
import com.zzx.dto.UserPhoneDTO;
public class LoginTest {
public static void main(String[] args) throws JsonProcessingException {
Map<String, String> params = new HashMap<>();
params.put("productCode", "password");
params.put("userName", "userName");
params.put("password", "password");
params.put("phoneNum", null);
params.put("verifyCode", null);
ObjectMapper mapper = new ObjectMapper();
String res = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(params);
LoginDTO loginDTO = mapper.readValue(res, LoginDTO.class);
System.out.println("loginDTO : "+JSON.toJSONString(loginDTO));
if (loginDTO instanceof UserPasswordDTO){
System.out.println("userPassword : "+JSON.toJSONString(loginDTO));
}
if (loginDTO instanceof UserPhoneDTO){
System.out.println("userPhone : "+JSON.toJSONString(loginDTO));
}
}
}
边栏推荐
- 使用同一个接口实现不同登录的方式
- PostgreSQL database master-slave deployment master database suspended restore master database
- 直播预告 | 开源安全治理模型和工具直播研讨会
- RestClient操作索引库-初始化RestClient
- Three things programmers want to do most | comics
- Experiment III LZW
- 2022年中国软件产品全国巡回展即将启航
- 敏捷测试团队组织构成
- Introduction to JVM monitoring tools jstack, jconsole, Jinfo, jmap, JDB, jstat
- matlab simulink 水能和同步电机发电
猜你喜欢
随机推荐
Worthington对肝细胞分离系统的测试及相关优化方案
c语言十进制数转二进制数
Typora设置标题自动添加序号
C language function (1)
Wechat applet project practice
matlab simulink 磷酸铁锂电池仿真
Experiment 5 JPEG
matlab ode45求解微分方程
使用路由协议配置 IPv6 over IP手动隧道
论文阅读:The Perfect Match: 3D Point Cloud Matching with Smoothed Densities
将childNodes返回的伪数组转化为真数组
networkx 对图进行可视化
Web资源共享
SQL报错盲注实例分析
Organizational structure of agile testing team
How to use selenium.chrome to realize the extended function of intercepting or forwarding requests
matlab simulink 水能和同步电机发电
Text align: center centered
Three things programmers want to do most | comics
C language decimal number to binary number









