Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动审核主机名和主机IP_Java_Hibernate_Jpa_Mapping_Audit Logging - Fatal编程技术网

Java Spring启动审核主机名和主机IP

Java Spring启动审核主机名和主机IP,java,hibernate,jpa,mapping,audit-logging,Java,Hibernate,Jpa,Mapping,Audit Logging,我有一个审计实体,其定义如下: import com.fasterxml.jackson.annotation.JsonIgnore; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedBy; import org.sp

我有一个审计实体,其定义如下:

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.io.Serializable;
import java.time.Instant;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;

/**
 * Base abstract class for entities which will hold definitions for created, last modified, created by,
 * last modified by attributes.
 */
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @CreatedBy
    @Column(name = "created_by", nullable = false, length = 50, updatable = false)
    @JsonIgnore
    private String createdBy;

    @CreatedDate
    @Column(name = "created_date", updatable = false)
    @JsonIgnore
    private Instant createdDate = Instant.now();

    @LastModifiedBy
    @Column(name = "last_modified_by", length = 50)
    @JsonIgnore
    private String lastModifiedBy;

    @LastModifiedDate
    @Column(name = "last_modified_date")
    @JsonIgnore
    private Instant lastModifiedDate = Instant.now();

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Instant getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Instant createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public Instant getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(Instant lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
 }
这就是实现:

import com.app.derin.uaa.config.Constants;

import java.util.Optional;

import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

/**
 * Implementation of {@link AuditorAware} based on Spring Security.
 */
@Component
public class SpringSecurityAuditorAware implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {
        return Optional.of(SecurityUtils.getCurrentUserName().orElse(Constants.SYSTEM_ACCOUNT));
    }    
}

有什么办法可以做到这一点吗?

我找到了解决方案,下面是我是如何做到的:

经过大量的谷歌搜索,我发现我的问题在于hibernate注释。我正在尝试发送hibernate不知道的数据类型。因此,我将类更改为hibernate的自定义类型

更多信息,您可以查看链接。它帮助了我

我的AuditorDetails类:

import java.io.Serializable;
import java.util.Objects;

public final class AuditorDetails implements Serializable {
    private final String loggedUser;

    private final String hostName;

    private final String hostIp;

    public AuditorDetails(String loggedUser, String hostName, String hostIp) {
        this.loggedUser = loggedUser;
        this.hostName = hostName;
        this.hostIp = hostIp;
    }

    public String getLoggedUser() {
        return loggedUser;
    }

    public String getHostName() {
        return hostName;
    }

    public String getHostIp() {
        return hostIp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AuditorDetails that = (AuditorDetails) o;
        return Objects.equals(loggedUser, that.loggedUser) &&
            Objects.equals(hostName, that.hostName) &&
            Objects.equals(hostIp, that.hostIp);
    }

    @Override
    public int hashCode() {
        return Objects.hash(loggedUser, hostName, hostIp);
    }
}
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;

public class AuditorDetailsType implements UserType {

    @Override
    public int[] sqlTypes() {
        return new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
    }

    @Override
    public Class returnedClass() {
        return AuditorDetails.class;
    }

    @Override
    public boolean equals(Object o, Object o1) throws HibernateException {
        if(o == o1)
            return true;
        if (Objects.isNull(o) || Objects.isNull(o1))
            return false;

        return o.equals(o1);
    }

    @Override
    public int hashCode(Object o) throws HibernateException {
        return o.hashCode();
    }

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException, SQLException {
        String loggedUser = resultSet.getString(strings[0]);

        if(resultSet.wasNull())
            return null;

        String hostName = resultSet.getString(strings[1]);

        String hostIp = resultSet.getString(strings[2]);

        AuditorDetails currentAuditor = new AuditorDetails(loggedUser, hostName, hostIp);
        return currentAuditor;
    }

    @Override
    public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException {
        if (Objects.isNull(o)){
            preparedStatement.setNull(i,Types.VARCHAR);
        }
        else {
            AuditorDetails currentAuditor = (AuditorDetails) o;
            preparedStatement.setString(i,currentAuditor.getLoggedUser());
            preparedStatement.setString(i+1,currentAuditor.getHostName());
            preparedStatement.setString(i+2,currentAuditor.getHostIp());
        }
    }

    @Override
    public Object deepCopy(Object o) throws HibernateException {
        if (Objects.isNull(o))
            return null;

        AuditorDetails currentAuditor = (AuditorDetails) o;

        return new AuditorDetails(currentAuditor.getLoggedUser(), currentAuditor.getHostName(), currentAuditor.getHostIp());
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Serializable disassemble(Object o) throws HibernateException {
        return (Serializable) o;
    }

    @Override
    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return serializable;
    }

    @Override
    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return o;
    }
}
import com.app.derin.uaa.ext.AuditorDetails;
import com.app.derin.uaa.ext.AuditorDetailsType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.TypeDef;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.Instant;

@TypeDef(name = "AuditorDetails",
    typeClass = AuditorDetailsType.class,
    defaultForType = AuditorDetails.class)
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity{

    @CreatedDate
    @JsonIgnore
    @Column(name = "created_date", updatable = false)
    private Instant createdDate = Instant.now();

    @CreatedBy
    @Columns(columns = {@Column(name = "created_by", updatable = false),
    @Column(name = "created_host_name", updatable = false),
    @Column(name = "created_host_ip", updatable = false)})
    private AuditorDetails createdBy;

    @LastModifiedDate
    @Column(name = "modified_date")
    @JsonIgnore
    private Instant modifiedDate = Instant.now();

    @LastModifiedBy
    @Columns(columns = {@Column(name = "modified_by"),
        @Column(name = "modified_host_name"),
        @Column(name = "modified_host_ip")})
    private AuditorDetails modifiedBy;

    @Column(name = "row_status")
    @JsonIgnore
    @ColumnDefault("1")
    private Integer rowStatus = 1;

    protected AbstractAuditingEntity() {
    }
    public AuditorDetails getModifiedBy() {
        return modifiedBy;
    }

    public Instant getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Instant createdDate) {
        this.createdDate = createdDate;
    }

    public AuditorDetails getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(AuditorDetails createdBy) {
        this.createdBy = createdBy;
    }

    public Instant getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Instant modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public Integer getRowStatus() {
        return rowStatus;
    }

    public void setRowStatus(Integer rowStatus) {
        this.rowStatus = rowStatus;
    }
}
AuditorDetailsType类:

import java.io.Serializable;
import java.util.Objects;

public final class AuditorDetails implements Serializable {
    private final String loggedUser;

    private final String hostName;

    private final String hostIp;

    public AuditorDetails(String loggedUser, String hostName, String hostIp) {
        this.loggedUser = loggedUser;
        this.hostName = hostName;
        this.hostIp = hostIp;
    }

    public String getLoggedUser() {
        return loggedUser;
    }

    public String getHostName() {
        return hostName;
    }

    public String getHostIp() {
        return hostIp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AuditorDetails that = (AuditorDetails) o;
        return Objects.equals(loggedUser, that.loggedUser) &&
            Objects.equals(hostName, that.hostName) &&
            Objects.equals(hostIp, that.hostIp);
    }

    @Override
    public int hashCode() {
        return Objects.hash(loggedUser, hostName, hostIp);
    }
}
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;

public class AuditorDetailsType implements UserType {

    @Override
    public int[] sqlTypes() {
        return new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
    }

    @Override
    public Class returnedClass() {
        return AuditorDetails.class;
    }

    @Override
    public boolean equals(Object o, Object o1) throws HibernateException {
        if(o == o1)
            return true;
        if (Objects.isNull(o) || Objects.isNull(o1))
            return false;

        return o.equals(o1);
    }

    @Override
    public int hashCode(Object o) throws HibernateException {
        return o.hashCode();
    }

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException, SQLException {
        String loggedUser = resultSet.getString(strings[0]);

        if(resultSet.wasNull())
            return null;

        String hostName = resultSet.getString(strings[1]);

        String hostIp = resultSet.getString(strings[2]);

        AuditorDetails currentAuditor = new AuditorDetails(loggedUser, hostName, hostIp);
        return currentAuditor;
    }

    @Override
    public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException {
        if (Objects.isNull(o)){
            preparedStatement.setNull(i,Types.VARCHAR);
        }
        else {
            AuditorDetails currentAuditor = (AuditorDetails) o;
            preparedStatement.setString(i,currentAuditor.getLoggedUser());
            preparedStatement.setString(i+1,currentAuditor.getHostName());
            preparedStatement.setString(i+2,currentAuditor.getHostIp());
        }
    }

    @Override
    public Object deepCopy(Object o) throws HibernateException {
        if (Objects.isNull(o))
            return null;

        AuditorDetails currentAuditor = (AuditorDetails) o;

        return new AuditorDetails(currentAuditor.getLoggedUser(), currentAuditor.getHostName(), currentAuditor.getHostIp());
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Serializable disassemble(Object o) throws HibernateException {
        return (Serializable) o;
    }

    @Override
    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return serializable;
    }

    @Override
    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return o;
    }
}
import com.app.derin.uaa.ext.AuditorDetails;
import com.app.derin.uaa.ext.AuditorDetailsType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.TypeDef;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.Instant;

@TypeDef(name = "AuditorDetails",
    typeClass = AuditorDetailsType.class,
    defaultForType = AuditorDetails.class)
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity{

    @CreatedDate
    @JsonIgnore
    @Column(name = "created_date", updatable = false)
    private Instant createdDate = Instant.now();

    @CreatedBy
    @Columns(columns = {@Column(name = "created_by", updatable = false),
    @Column(name = "created_host_name", updatable = false),
    @Column(name = "created_host_ip", updatable = false)})
    private AuditorDetails createdBy;

    @LastModifiedDate
    @Column(name = "modified_date")
    @JsonIgnore
    private Instant modifiedDate = Instant.now();

    @LastModifiedBy
    @Columns(columns = {@Column(name = "modified_by"),
        @Column(name = "modified_host_name"),
        @Column(name = "modified_host_ip")})
    private AuditorDetails modifiedBy;

    @Column(name = "row_status")
    @JsonIgnore
    @ColumnDefault("1")
    private Integer rowStatus = 1;

    protected AbstractAuditingEntity() {
    }
    public AuditorDetails getModifiedBy() {
        return modifiedBy;
    }

    public Instant getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Instant createdDate) {
        this.createdDate = createdDate;
    }

    public AuditorDetails getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(AuditorDetails createdBy) {
        this.createdBy = createdBy;
    }

    public Instant getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Instant modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public Integer getRowStatus() {
        return rowStatus;
    }

    public void setRowStatus(Integer rowStatus) {
        this.rowStatus = rowStatus;
    }
}
抽象审计实体:

import java.io.Serializable;
import java.util.Objects;

public final class AuditorDetails implements Serializable {
    private final String loggedUser;

    private final String hostName;

    private final String hostIp;

    public AuditorDetails(String loggedUser, String hostName, String hostIp) {
        this.loggedUser = loggedUser;
        this.hostName = hostName;
        this.hostIp = hostIp;
    }

    public String getLoggedUser() {
        return loggedUser;
    }

    public String getHostName() {
        return hostName;
    }

    public String getHostIp() {
        return hostIp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AuditorDetails that = (AuditorDetails) o;
        return Objects.equals(loggedUser, that.loggedUser) &&
            Objects.equals(hostName, that.hostName) &&
            Objects.equals(hostIp, that.hostIp);
    }

    @Override
    public int hashCode() {
        return Objects.hash(loggedUser, hostName, hostIp);
    }
}
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;

public class AuditorDetailsType implements UserType {

    @Override
    public int[] sqlTypes() {
        return new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
    }

    @Override
    public Class returnedClass() {
        return AuditorDetails.class;
    }

    @Override
    public boolean equals(Object o, Object o1) throws HibernateException {
        if(o == o1)
            return true;
        if (Objects.isNull(o) || Objects.isNull(o1))
            return false;

        return o.equals(o1);
    }

    @Override
    public int hashCode(Object o) throws HibernateException {
        return o.hashCode();
    }

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException, SQLException {
        String loggedUser = resultSet.getString(strings[0]);

        if(resultSet.wasNull())
            return null;

        String hostName = resultSet.getString(strings[1]);

        String hostIp = resultSet.getString(strings[2]);

        AuditorDetails currentAuditor = new AuditorDetails(loggedUser, hostName, hostIp);
        return currentAuditor;
    }

    @Override
    public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException {
        if (Objects.isNull(o)){
            preparedStatement.setNull(i,Types.VARCHAR);
        }
        else {
            AuditorDetails currentAuditor = (AuditorDetails) o;
            preparedStatement.setString(i,currentAuditor.getLoggedUser());
            preparedStatement.setString(i+1,currentAuditor.getHostName());
            preparedStatement.setString(i+2,currentAuditor.getHostIp());
        }
    }

    @Override
    public Object deepCopy(Object o) throws HibernateException {
        if (Objects.isNull(o))
            return null;

        AuditorDetails currentAuditor = (AuditorDetails) o;

        return new AuditorDetails(currentAuditor.getLoggedUser(), currentAuditor.getHostName(), currentAuditor.getHostIp());
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Serializable disassemble(Object o) throws HibernateException {
        return (Serializable) o;
    }

    @Override
    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return serializable;
    }

    @Override
    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return o;
    }
}
import com.app.derin.uaa.ext.AuditorDetails;
import com.app.derin.uaa.ext.AuditorDetailsType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.TypeDef;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.Instant;

@TypeDef(name = "AuditorDetails",
    typeClass = AuditorDetailsType.class,
    defaultForType = AuditorDetails.class)
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity{

    @CreatedDate
    @JsonIgnore
    @Column(name = "created_date", updatable = false)
    private Instant createdDate = Instant.now();

    @CreatedBy
    @Columns(columns = {@Column(name = "created_by", updatable = false),
    @Column(name = "created_host_name", updatable = false),
    @Column(name = "created_host_ip", updatable = false)})
    private AuditorDetails createdBy;

    @LastModifiedDate
    @Column(name = "modified_date")
    @JsonIgnore
    private Instant modifiedDate = Instant.now();

    @LastModifiedBy
    @Columns(columns = {@Column(name = "modified_by"),
        @Column(name = "modified_host_name"),
        @Column(name = "modified_host_ip")})
    private AuditorDetails modifiedBy;

    @Column(name = "row_status")
    @JsonIgnore
    @ColumnDefault("1")
    private Integer rowStatus = 1;

    protected AbstractAuditingEntity() {
    }
    public AuditorDetails getModifiedBy() {
        return modifiedBy;
    }

    public Instant getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Instant createdDate) {
        this.createdDate = createdDate;
    }

    public AuditorDetails getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(AuditorDetails createdBy) {
        this.createdBy = createdBy;
    }

    public Instant getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Instant modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public Integer getRowStatus() {
        return rowStatus;
    }

    public void setRowStatus(Integer rowStatus) {
        this.rowStatus = rowStatus;
    }
}
SpringSecurityAuditorAware:

import com.app.derin.uaa.config.Constants;

import java.util.Optional;

import com.app.derin.uaa.ext.AuditorDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * Implementation of {@link AuditorAware} based on Spring Security.
 */
@Component
public class SpringSecurityAuditorAware implements AuditorAware<AuditorDetails> {


    private final Logger log = LoggerFactory.getLogger(SpringSecurityAuditorAware.class);

    @Override
    public Optional<AuditorDetails> getCurrentAuditor() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        if(request != null) {
            String hostIp = getClientIpAddress(request);
            String hostName = "";
            AuditorDetails currentAuditor = new AuditorDetails(SecurityUtils.getCurrentUserName().orElse(Constants.SYSTEM_ACCOUNT),
                hostName, hostIp);
            return Optional.of(currentAuditor);
        }

        return Optional.of(currentAuditor);
    }

    private static final String[] IP_HEADER_CANDIDATES = {
        "X-Forwarded-For",
        "Proxy-Client-IP",
        "WL-Proxy-Client-IP",
        "HTTP_X_FORWARDED_FOR",
        "HTTP_X_FORWARDED",
        "HTTP_X_CLUSTER_CLIENT_IP",
        "HTTP_CLIENT_IP",
        "HTTP_FORWARDED_FOR",
        "HTTP_FORWARDED",
        "HTTP_VIA",
        "REMOTE_ADDR" };

    public String getClientIpAddress(HttpServletRequest request) {
        for (String header : IP_HEADER_CANDIDATES) {
            String ip = request.getHeader(header);
            log.info("ip : {}", ip);
            if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
                return ip;
            }
        }
        return request.getRemoteAddr();
    }

}
import com.app.derin.uaa.config.Constants;
导入java.util.Optional;
导入com.app.derin.uaa.ext.AuditorDetails;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.data.domain.AuditorAware;
导入org.springframework.stereotype.Component;
导入org.springframework.web.context.request.RequestContextHolder;
导入org.springframework.web.context.request.ServletRequestAttributes;
导入javax.servlet.http.HttpServletRequest;
/**
*基于Spring安全性的{@link AuditorAware}的实现。
*/
@组成部分
公共类SpringSecurityAuditorAware实现AuditorAware{
私有最终记录器log=LoggerFactory.getLogger(SpringSecurityAuditorAware.class);
@凌驾
公共可选getCurrentAuditor(){
ServletRequestAttributes sra=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest=sra.getRequest();
if(请求!=null){
字符串hostIp=getClientIpAddress(请求);
字符串hostName=“”;
AuditorDetails currentAuditor=新的AuditorDetails(SecurityUtils.getCurrentUserName().orElse(Constants.SYSTEM_ACCOUNT),
主机名(hostIp);
返回可选。of(currentAuditor);
}
返回可选。of(currentAuditor);
}
私有静态最终字符串[]IP_头_候选={
“X-Forwarded-For”,
“代理客户端IP”,
“WL代理客户端IP”,
“HTTP\u X\u转发给”,
“HTTP_X_转发”,
“HTTP_X_群集_客户端_IP”,
“HTTP_客户端_IP”,
“HTTP\u转发给”,
“HTTP_转发”,
“HTTP_VIA”,
“远程地址”};
公共字符串getClientIpAddress(HttpServletRequest请求){
for(字符串头:IP\U头\U候选){
字符串ip=request.getHeader(header);
log.info(“ip:{}”,ip);
如果(ip!=null&&ip.length()!=0&&!(未知)。equalsIgnoreCase(ip)){
返回ip;
}
}
返回请求。getRemoteAddr();
}
}

T的类型是什么?它用于AuditorDetails类,我将其用作AuditorDetails或T,但它是相同的。没有变化。此类
import javax.persistence.embeddeble;导入java.io.Serializable@可嵌入的公共类AuditorDetails实现可序列化的{String loggedUser;String hostName;String hostIp;//getter setters}
,为什么使用T而不使用AuditorDetails?它不会改变任何东西。或审计细节。这不是我的问题。我问的是如何用审计添加hostip。有什么办法吗?但你的错误是T不是已知的类型