AuthUserHelperImpl.java 4.86 KB
Newer Older
frank.xa.zhang's avatar
frank.xa.zhang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
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;
import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.security.DDUserInfo;
import pwc.taxtech.atms.security.JwtUser;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@Component
public class AuthUserHelperImpl implements AuditorAware<String>, AuthUserHelper {
    private static Logger logger = LoggerFactory.getLogger(AuthUserHelperImpl.class);
    @Autowired(required = false)
    private HttpServletRequest request;

    @Resource
    private UserMapper userMapper;

    /*
     * (non-Javadoc)
     * 
     * @see pwc.taxtech.atms.common.AuthUserHelper#getCurrentAuditor()
     */
    @Override
    public Optional<String> getCurrentAuditor() {
        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");
        }
        return Optional.of(result);
    }

    /*
     * (non-Javadoc)
     * 
     * @see pwc.taxtech.atms.common.AuthUserHelper#getCurrentUserId()
     */
    @Override
    public String getCurrentUserId() {
        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) {
            return "";
        }
        return jwtUser.getUserid();
    }

    /*
     * (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);
    }

    @Override
    public DDUserInfo getDDUserInfo(){
        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) {
            return null;
        }
        return (DDUserInfo)jwtUser.getDefaultClaims().get("dduserInfo");
    }
}