AuthUserHelperImpl.java 4.1 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package pwc.taxtech.atms.common;

import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import pwc.taxtech.atms.dao.UserMapper;
eddie.woo's avatar
eddie.woo committed
16
import pwc.taxtech.atms.exception.ApplicationException;
eddie.woo's avatar
eddie.woo committed
17
import pwc.taxtech.atms.security.JwtUser;
eddie.woo's avatar
eddie.woo committed
18

eddie.woo's avatar
eddie.woo committed
19
import javax.servlet.http.HttpServletRequest;
20
import java.util.Optional;
eddie.woo's avatar
eddie.woo committed
21

eddie.woo's avatar
eddie.woo committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
@Component
public class AuthUserHelperImpl implements AuditorAware<String>, AuthUserHelper {
    private static Logger logger = LoggerFactory.getLogger(AuthUserHelperImpl.class);
    @Autowired(required = false)
    private HttpServletRequest request;

    @Autowired
    private UserMapper userMapper;

    /*
     * (non-Javadoc)
     * 
     * @see pwc.taxtech.atms.common.AuthUserHelper#getCurrentAuditor()
     */
    @Override
37
    public Optional<String> getCurrentAuditor() {
eddie.woo's avatar
eddie.woo committed
38 39 40 41 42 43 44 45 46 47 48 49
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            throw new ApplicationException("security context is null");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            throw new ApplicationException("authentication failed");
        }
        String result = authentication.getName();
        if (Strings.isEmpty(result)) {
            throw new ApplicationException("failed to get user name from authentication");
        }
50
        return Optional.of(result);
eddie.woo's avatar
eddie.woo committed
51 52 53 54 55
    }

    /*
     * (non-Javadoc)
     * 
56
     * @see pwc.taxtech.atms.common.AuthUserHelper#getCurrentUserId()
eddie.woo's avatar
eddie.woo committed
57 58
     */
    @Override
59
    public String getCurrentUserId() {
eddie.woo's avatar
eddie.woo committed
60 61 62 63 64 65 66 67 68 69
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            throw new ApplicationException("security context is null");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            throw new ApplicationException("authentication failed");
        }
        JwtUser jwtUser = (JwtUser) authentication.getPrincipal();
        if (jwtUser == null) {
eddie.woo's avatar
eddie.woo committed
70 71
            return "";
        }
eddie.woo's avatar
eddie.woo committed
72
        return jwtUser.getUserid();
eddie.woo's avatar
eddie.woo committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * pwc.taxtech.atms.common.AuthUserHelper#getIpaddressFromSecurityContextHolder(
     * )
     */
    @Override
    public String getIpaddressFromSecurityContextHolder() {
        // 默认ip地址
        String ipaddress = CommonConstants.DEFAULT_IP_ADDRESS;
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            throw new ApplicationException("security context is null");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            throw new ApplicationException("authentication is null");
        }
        Object authDetails = authentication.getDetails();
        if (authDetails instanceof WebAuthenticationDetails) {
            WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails) authDetails;
            if (StringUtils.hasText(webAuthenticationDetails.getRemoteAddress())) {
                logger.debug("Fetch IP address from WebAuthenticationDetails");
                ipaddress = webAuthenticationDetails.getRemoteAddress();
            }
        }
        return ipaddress;
    }

    /*
     * (non-Javadoc)
     * 
     * @see pwc.taxtech.atms.common.AuthUserHelper#getClientIp()
     */
    @Override
    public String getClientIp() {
        return Lang.getIP(request);
    }
}