对象转换小工具
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.filter.JwtAuthorizeFilter;
|
||||||
import com.example.service.AccountService;
|
import com.example.service.AccountService;
|
||||||
import com.example.utils.JwtUtils;
|
import com.example.utils.JwtUtils;
|
||||||
|
import com.fasterxml.jackson.databind.util.BeanUtil;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
@ -89,11 +91,11 @@ public class SecurityConfiguration {
|
||||||
User user = (User) authentication.getPrincipal();
|
User user = (User) authentication.getPrincipal();
|
||||||
Account account = services.findAccountByNameOrEmail(user.getUsername());
|
Account account = services.findAccountByNameOrEmail(user.getUsername());
|
||||||
String token = utils.createJwt(user , account.getId(), account.getUsername());
|
String token = utils.createJwt(user , account.getId(), account.getUsername());
|
||||||
AuthorizeV0 v0 = new AuthorizeV0();
|
AuthorizeV0 v0 = account.asViewObject(AuthorizeV0.class , v -> {
|
||||||
v0.setExpire(utils.expireTime());
|
v.setExpire(utils.expireTime());
|
||||||
v0.setRole(account.getRole());
|
v.setToken(token);
|
||||||
v0.setToken(token);
|
});
|
||||||
v0.setUsername(account.getUsername());
|
|
||||||
response.getWriter().write(RestBean.success(v0).asJsonString());
|
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.IdType;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.example.entity.BaseData;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ import java.util.Date;
|
||||||
@Data
|
@Data
|
||||||
@TableName("db_account")
|
@TableName("db_account")
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class Account {
|
public class Account implements BaseData {
|
||||||
@TableId(type = IdType.AUTO)
|
@TableId(type = IdType.AUTO)
|
||||||
Integer id;
|
Integer id;
|
||||||
String username;
|
String username;
|
||||||
|
|
Loading…
Reference in New Issue