All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.digitalchina.platform.security.auth.CustomUserDetailService Maven / Gradle / Ivy

The newest version!
package com.digitalchina.platform.security.auth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.digitalchina.platform.security.constants.SecurityConstants;
import com.digitalchina.platform.security.constants.UserInfoParamConstants;
import com.digitalchina.platform.security.properties.SecureProperties;
import com.digitalchina.platform.security.utils.HttpSender;
import com.digitalchina.platform.security.utils.SimpleHttpResponse;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.DigestUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CustomUserDetailService implements UserDetailsService {
    private static final Logger logger = LoggerFactory.getLogger(CustomMetadataSource.class);
    private SecureProperties secureProperties;

    public UserDetails loadUserByUsername(String account)
            throws UsernameNotFoundException {
        if (account == null) {
            throw new UsernameNotFoundException(account);
        }

        String password = null;
        boolean enabled = true;
        Map extendInfo = Maps.newHashMap();
        //获取用户信息
        List params = Lists.newArrayList();
        params.add(new BasicNameValuePair("account", account));
        params.add(new BasicNameValuePair("apiKey", secureProperties.getApiKey()));

        SimpleHttpResponse httpResponse = HttpSender.postResponse(secureProperties.getUserInfoUrl(),
                secureProperties.getHttpConnectionTimeout() * 1000, params, null, "utf-8");
        String responseMgr = httpResponse.getHttpResponseEntity();
        logger.debug(responseMgr);
        if (responseMgr != null && responseMgr.trim().length() > 0) {
            try {
                JSONObject responseMsg = (JSONObject) JSONObject.parse(responseMgr);
                Integer code = responseMsg.getInteger("code");
                JSONObject result = responseMsg.getJSONObject("result");
                if (SecurityConstants.RESPONSE_CODE_OK == code) {//调用成功
                    password = String.valueOf(result.get("password"));
                    extendInfo.put(UserInfoParamConstants.USER_ID, result.get(UserInfoParamConstants.USER_ID));
                    extendInfo.put(UserInfoParamConstants.ACCOUNT, result.get(UserInfoParamConstants.ACCOUNT));
                    extendInfo.put(UserInfoParamConstants.DESC, result.get(UserInfoParamConstants.DESC));
                    extendInfo.put(UserInfoParamConstants.DREG, result.get(UserInfoParamConstants.DREG));
                    extendInfo.put(UserInfoParamConstants.NAME, result.get(UserInfoParamConstants.NAME));
                    List roleIdList = (List) result.get(UserInfoParamConstants.ROLEID_LIST);
                    if (roleIdList == null && roleIdList.size() == 0) {
                        extendInfo.put(UserInfoParamConstants.ROLEID_LIST, null);
                    } else {
                        extendInfo.put(UserInfoParamConstants.ROLEID_LIST, Joiner.on(",").join(roleIdList));
                    }
                    extendInfo.put(UserInfoParamConstants.MOBILE_PHONE, result.get(UserInfoParamConstants.MOBILE_PHONE));
                    extendInfo.put(UserInfoParamConstants.EMAIL, result.get(UserInfoParamConstants.EMAIL));
                    extendInfo.put(UserInfoParamConstants.QQ, result.get(UserInfoParamConstants.QQ));
                    extendInfo.put(UserInfoParamConstants.WECHAT, result.get(UserInfoParamConstants.WECHAT));

                    extendInfo.put(UserInfoParamConstants.EXT1, result.get(UserInfoParamConstants.EXT1));
                    extendInfo.put(UserInfoParamConstants.EXT2, result.get(UserInfoParamConstants.EXT2));
                    extendInfo.put(UserInfoParamConstants.EXT3, result.get(UserInfoParamConstants.EXT3));
                    extendInfo.put(UserInfoParamConstants.EXT4, result.get(UserInfoParamConstants.EXT4));
                    extendInfo.put(UserInfoParamConstants.EXT5, result.get(UserInfoParamConstants.EXT5));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            throw new UsernameNotFoundException(account);
        }


        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;
        Set authorities = new HashSet();
        //获取用户当前项目下可访问的所有url
        params.add(new BasicNameValuePair("pCode", secureProperties.getpCode()));
        params.add(new BasicNameValuePair("apiKey", secureProperties.getApiKey()));

        httpResponse = HttpSender.postResponse(secureProperties.getAuthApplicationUrl(),
                secureProperties.getHttpConnectionTimeout() * 1000, params, null, "utf-8");
        responseMgr = httpResponse.getHttpResponseEntity();
        if (responseMgr != null && responseMgr.trim().length() > 0) {
            try {
                JSONObject responseMsg = (JSONObject) JSONObject.parse(responseMgr);
                Integer code = responseMsg.getInteger("code");
                JSONArray result = responseMsg.getJSONArray("result");
                if (SecurityConstants.RESPONSE_CODE_OK == code) {//调用成功
                    Object[] array = result.toArray();
                    for (Object o : array) {
                        SimpleGrantedAuthority authority = new SimpleGrantedAuthority(String.valueOf(o));
                        authorities.add(authority);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        CustomUser customUser = new CustomUser(account, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        customUser.setExtendInfo(extendInfo);
        return customUser;
    }

    private String saltSecurityConfig(String url) {
        String config = DigestUtils.md5DigestAsHex((url + "{" + secureProperties.getpCode() + "}").getBytes());
        return config;
    }

    public void setSecureProperties(SecureProperties secureProperties) {
        this.secureProperties = secureProperties;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy