对象转换小工具
This commit is contained in:
parent
cafb198c4a
commit
465237a3cd
|
@ -6,10 +6,12 @@ import com.example.entity.vo.response.AuthorizeV0;
|
|||
import com.example.filter.JwtAuthorizeFilter;
|
||||
import com.example.service.AccountService;
|
||||
import com.example.utils.JwtUtils;
|
||||
import com.fasterxml.jackson.databind.util.BeanUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
|
@ -89,11 +91,11 @@ public class SecurityConfiguration {
|
|||
User user = (User) authentication.getPrincipal();
|
||||
Account account = services.findAccountByNameOrEmail(user.getUsername());
|
||||
String token = utils.createJwt(user , account.getId(), account.getUsername());
|
||||
AuthorizeV0 v0 = new AuthorizeV0();
|
||||
v0.setExpire(utils.expireTime());
|
||||
v0.setRole(account.getRole());
|
||||
v0.setToken(token);
|
||||
v0.setUsername(account.getUsername());
|
||||
AuthorizeV0 v0 = account.asViewObject(AuthorizeV0.class , v -> {
|
||||
v.setExpire(utils.expireTime());
|
||||
v.setToken(token);
|
||||
});
|
||||
|
||||
response.getWriter().write(RestBean.success(v0).asJsonString());
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package com.example.entity;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface BaseData {
|
||||
default <V> V asViewObject(Class<V> clazz , Consumer<V> consumer){
|
||||
V v = this.asViewObject(clazz);
|
||||
consumer.accept(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
default <V> V asViewObject(Class<V> clazz){
|
||||
try {
|
||||
Field[] declaredFirlds = clazz.getDeclaredFields();
|
||||
Constructor<V> constructor = clazz.getConstructor();
|
||||
V v = constructor.newInstance();
|
||||
for (Field declaredField : declaredFirlds) convert(declaredField , v);
|
||||
return v;
|
||||
} catch (ReflectiveOperationException exception) {
|
||||
throw new RuntimeException(exception.getMessage());
|
||||
}
|
||||
}
|
||||
private void convert(Field field , Object vo){
|
||||
try {
|
||||
Field source = this.getClass().getDeclaredField(field.getName());
|
||||
field.setAccessible(true);
|
||||
source.setAccessible(true);
|
||||
field.set(vo , source.get(this));
|
||||
|
||||
} catch (IllegalAccessException |NoSuchFieldException e) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package com.example.entity.dto;
|
|||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.example.entity.BaseData;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
|
@ -11,7 +12,7 @@ import java.util.Date;
|
|||
@Data
|
||||
@TableName("db_account")
|
||||
@AllArgsConstructor
|
||||
public class Account {
|
||||
public class Account implements BaseData {
|
||||
@TableId(type = IdType.AUTO)
|
||||
Integer id;
|
||||
String username;
|
||||
|
|
Loading…
Reference in New Issue