AccountSecurityAuditor.java 1.1 KB
package com.glsx.usedcar.security;

import com.glsx.usedcar.module.account.entity.Account;
import org.springframework.data.domain.AuditorAware;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;
import java.util.Optional;

import static com.glsx.usedcar.web.WebConstants.LOGIN_ACCOUNT;

public class AccountSecurityAuditor implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.isNull(requestAttributes)) {
            return Optional.of("系统默认");
        }
        Account account = (Account) (((ServletRequestAttributes) requestAttributes).getRequest()).getAttribute(LOGIN_ACCOUNT);
        if (Objects.isNull(account)) {
            return Optional.of("系统默认");
        }
        String principal = account.getAccountName();
        return Optional.of(principal);
    }
}