Skip to content

Commit

Permalink
优化 records 类的编码方式
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Oct 27, 2023
1 parent 5921431 commit 5d49e11
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public static <T> Mono<T> userAuditorSerializable(T obejct) {
if (ObjectUtils.isEmpty(userAuditor)) {
return Mono.just(obejct);
}
return USERS_SERVICE.loadByCode(userAuditor.getCode()).flatMap(user -> {
return USERS_SERVICE.loadByCode(userAuditor.code()).flatMap(user -> {
try {
propertyDescriptor.getWriteMethod().invoke(obejct, userAuditor.withUser(user));
propertyDescriptor.getWriteMethod().invoke(obejct, UserAuditor.withUser(user));
return Mono.just(obejct);
} catch (IllegalAccessException | InvocationTargetException e) {
return Mono.error(RestServerException.withMsg(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class UserAuditorWriteConverter implements Converter<UserAuditor,
*/
@Override
public String convert(@NonNull UserAuditor source) {
return source.getCode();
return source.code();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
* @author Alex bob
*/
public record AuthenticationToken(String token, Long expires, Long lastAccessTime) implements Serializable {

public static AuthenticationToken of(String token, String expires, Long lastAccessTime) {
return new AuthenticationToken(token, Long.parseLong(expires), lastAccessTime);
}

/**
* Builds an authentication token from a web session
*
* @param session The web session
* @return The authentication token
*/
public static AuthenticationToken build(WebSession session) {
return new AuthenticationToken(session.getId(),
session.getMaxIdleTime().getSeconds(),
return new AuthenticationToken(session.getId(), session.getMaxIdleTime().getSeconds(),
session.getLastAccessTime().getEpochSecond());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.platform.boot.security;

import com.platform.boot.security.user.User;
import lombok.Data;

import java.io.Serializable;

Expand All @@ -22,31 +21,22 @@
*
* @author Alex bob (<a href="https://github.com/vnobo">Alex bob</a>)
*/
@Data
public class UserAuditor implements Serializable {
public record UserAuditor(String code, String username, String name) implements Serializable {

private String code;
private String username;
private String name;

public static UserAuditor withDetails(SecurityDetails securityDetails) {
UserAuditor userAuditor = UserAuditor.withCode(securityDetails.getCode());
userAuditor.setName(securityDetails.getName());
userAuditor.setUsername(securityDetails.getUsername());
return userAuditor;
public static UserAuditor of(String code, String username, String name) {
return new UserAuditor(code, username, name);
}

public static UserAuditor withCode(String code) {
UserAuditor userAuditor = new UserAuditor();
userAuditor.setCode(code);
return userAuditor;
return of(code, null, null);
}

public static UserAuditor withDetails(SecurityDetails securityDetails) {
return of(securityDetails.getCode(), securityDetails.getUsername(), securityDetails.getName());
}

public UserAuditor withUser(User user) {
this.code = user.getCode();
this.username = user.getUsername();
this.name = user.getName();
return this;
public static UserAuditor withUser(User user) {
return of(user.getCode(), user.getUsername(), user.getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class UserResponse extends User {
/**
* UserResponse.java
* <p>
* This file contains the code for the UserResponse class.
* It is written in the Java programming language.
* The code overrides the getPassword() method from the superclass
* and uses the @JsonIgnore annotation to indicate that the password
* should be ignored during serialization and deserialization.
*/
@JsonIgnore
@Override
public String getPassword() {
return super.getPassword();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.platform.boot.security.user;


import com.platform.boot.commons.utils.ContextUtils;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand Down
5 changes: 3 additions & 2 deletions boot/platform/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ server.port: 8080
spring:
application.name: platform
r2dbc:
url: r2dbc:postgres://localhost:5432/plate
url: r2dbc:postgres://192.168.1.2:5432/plate?fetchSize=5000
username: farmer
password: q1w2e3..
pool:
Expand All @@ -26,6 +26,7 @@ spring:
sql.init:
mode: always
platform: postgres
encoding: utf-8
data.redis:
host: localhost
host: 192.168.1.2
repositories.enabled: false
5 changes: 1 addition & 4 deletions boot/platform/src/main/resources/data-postgres.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
insert into se_users(code, username, password, name, extend)
values ('U1000', 'admin', '{pbkdf2}7d8a68bc5d507bd19bc153ff10bcdef66f5a5f3d0c1ab2438630e50b5c65894bccc2c7e4404c5afa',
'系统超级管理员',
'{
"avatar": "https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png"
}');
'系统超级管理员', '{}');
insert into se_authorities(code, user_code, authority)
values ('A1000', 'U1000', 'ROLE_ADMINISTRATORS');

Expand Down

0 comments on commit 5d49e11

Please sign in to comment.