From 465237a3cd7433e042e78696a24ec2a1bdca234a Mon Sep 17 00:00:00 2001 From: Amadeus Date: Sun, 28 Apr 2024 23:51:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E8=BD=AC=E6=8D=A2=E5=B0=8F?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/config/SecurityConfiguration.java | 12 ++++--- .../java/com/example/entity/BaseData.java | 36 +++++++++++++++++++ .../java/com/example/entity/dto/Account.java | 3 +- 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/example/entity/BaseData.java diff --git a/src/main/java/com/example/config/SecurityConfiguration.java b/src/main/java/com/example/config/SecurityConfiguration.java index b842c08..cc8cfb8 100644 --- a/src/main/java/com/example/config/SecurityConfiguration.java +++ b/src/main/java/com/example/config/SecurityConfiguration.java @@ -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()); } diff --git a/src/main/java/com/example/entity/BaseData.java b/src/main/java/com/example/entity/BaseData.java new file mode 100644 index 0000000..37961ba --- /dev/null +++ b/src/main/java/com/example/entity/BaseData.java @@ -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 asViewObject(Class clazz , Consumer consumer){ + V v = this.asViewObject(clazz); + consumer.accept(v); + return v; + } + + + default V asViewObject(Class clazz){ + try { + Field[] declaredFirlds = clazz.getDeclaredFields(); + Constructor 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) { + } + } +} diff --git a/src/main/java/com/example/entity/dto/Account.java b/src/main/java/com/example/entity/dto/Account.java index 4550843..41b18d1 100644 --- a/src/main/java/com/example/entity/dto/Account.java +++ b/src/main/java/com/example/entity/dto/Account.java @@ -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;