Bladeren bron

Merge branch 'dev' of trick9191/wlyy2.0 into dev

trick9191 5 jaren geleden
bovenliggende
commit
d8b400218a

+ 19 - 9
business/base-service/src/main/java/com/yihu/jw/hospital/prescription/service/PrescriptionService.java

@ -2248,16 +2248,26 @@ public class PrescriptionService extends BaseJpaService<WlyyPrescriptionDO, Pres
        return rs;
    }
    @Autowired
    private HttpClientUtil httpClientUtil;
//    @Autowired
//    private HttpClientUtil httpClientUtil;
    public String test() {
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("clientId", "ihealth_pa8DIRJasL"));
        params.add(new BasicNameValuePair("clientSecret", "jkzl1h7aj39dnasyi23fnkv92"));
        params.add(new BasicNameValuePair("code", "b60bab0617c04b23b7fa0a059cc83eb6"));
        params.add(new BasicNameValuePair("openid","oULM4xAj9sOsHJ95ttKYd_Fjh6Hc"));
        String res = httpClientUtil.post("http://www.xmtyw.cn/wlyy/iHealth/userInfo", params, "UTF-8");
        return res;
//        List<NameValuePair> params = new ArrayList<>();
////        params.add(new BasicNameValuePair("clientId", "ihealth_pa8DIRJasL"));
////        params.add(new BasicNameValuePair("clientSecret", "jkzl1h7aj39dnasyi23fnkv92"));
////        params.add(new BasicNameValuePair("code", "b60bab0617c04b23b7fa0a059cc83eb6"));
//        params.add(new BasicNameValuePair("openid","o0hubt3byGmNYLyC-UvmSVyUuZkI"));
//        //String res = httpClientUtil.post("http://www.xmtyw.cn/wlyy/iHealth/userInfo", params, "UTF-8");
//        //https://wx.xmzsh.com
//        String res = httpClientUtil.post("https://wx.xmzsh.com/interface/getUserInfoByOpenId.htm", params, "UTF-8");
//        return res;
//
//        com.alibaba.fastjson.JSONObject jsonObject=new com.alibaba.fastjson.JSONObject();
//        jsonObject.put("openId","o0hubt3byGmNYLyC-UvmSVyUuZkI");
//        String responseMsg =httpClientUtil.sendPost("https://wx.xmzsh.com/interface/getUserInfoByOpenId.htm",jsonObject.toString());
//        com.alibaba.fastjson.JSONObject object1 = com.alibaba.fastjson.JSONObject.parseObject(responseMsg);
//        return object1.toString();
        return null;
    }
    
    

+ 193 - 93
server/svr-authentication/src/main/java/com/yihu/jw/security/core/userdetails/jdbc/WlyyUserDetailsService.java

@ -118,10 +118,12 @@ public class WlyyUserDetailsService extends JdbcDaoSupport implements UserDetail
        } else if ("2".equals(loginType)) {//2.为医生账号
            //解除锁定
            this.getJdbcTemplate().update("update base_doctor d set d.login_failure_count = 0, d.locked = 0 where d.mobile = ? or d.idcard = ?", username, username);
        } else if ("3".equals(loginType) || "4".equals(loginType)) { //3.患者账号
        } else if ("3".equals(loginType)) { //3.患者账号
            //解除锁定
            this.getJdbcTemplate().update("update base_patient p set p.login_failure_count = 5, p.locked = 0 where p.mobile = ? or p.idcard = ?", username, username);
        } //...
        } else if("4".equals(loginType)){
            this.getJdbcTemplate().update("update base_patient p set p.login_failure_count = 5, p.locked = 0 where p.mobile = ? or p.idcard = ?", username, getIdcard());
        }//...
    }
    /**
@ -155,98 +157,175 @@ public class WlyyUserDetailsService extends JdbcDaoSupport implements UserDetail
                redisTemplate.delete(BespeakRegist + username);
            }
            users = this.getJdbcTemplate().query(DEFAULT_PATIENT_DETAILS_STATEMENT, new BeanPropertyRowMapper(WlyyUserDetails.class), username, username);
            // 去i健康查询用户
            // 第三方同步居民,端查询用户
        }else if("4".equals(loginType)){
            users = this.getJdbcTemplate().query(DEFAULT_PATIENT_DETAILS_IDCARD_STATEMENT, new BeanPropertyRowMapper(WlyyUserDetails.class), getIdcard());
            if ((null == users || users.size() == 0) && "4".equals(loginType)) {
                List<NameValuePair> params = new ArrayList<>();
                params.add(new BasicNameValuePair("clientId", getClientId()));
                ClientDetails clientDetails = clientDetailsService.loadClientByClientId(getClientId());
                params.add(new BasicNameValuePair("clientSecret", clientDetails.getClientSecret()));
                params.add(new BasicNameValuePair("code", username));
                params.add(new BasicNameValuePair("openid", getOpenid()));
                String res = null;
                try{
                    res = httpClientUtil.post(iHealthUserInfoUrl, params, "UTF-8");
                }catch (Exception e){
                    logger.error("远程请求i健康用户信息异常:" + e.getMessage());
                    return users;
                }
                if(null == res){
                    return users;
                }
                JSONObject patient = JSONObject.parseObject(res);
                if(patient.getInteger("status") == -1){
                    logger.error("去i健康查询用户信息失败:" + patient.getString("error"));
                    return users;
                }
                try {
                    String salt = randomString(5);
                    String idcard = patient.getString("idcard");
                    String pw = idcard.substring(idcard.length()-6);
                    this.getJdbcTemplate().update(DEFAULT_PATIENT_INSERT_STATEMENT,
                            new Object[]{patient.getString("code"),
                                    patient.getString("photo"),
                                    patient.getString("idcard"),
                                    MD5.md5Hex(pw + "{" + salt + "}"),
                                    patient.getString("salt"),
                                    patient.getString("name"),
                                    patient.getDate("birthday"),
                                    patient.getInteger("sex"),
                                    patient.getString("mobile"),
                                    patient.getString("province"),
                                    patient.getString("provinceName"),
                                    patient.getString("city"),
                                    patient.getString("cityName"),
                                    patient.getString("town"),
                                    patient.getString("townName"),
                                    patient.getString("street"),
                                    patient.getString("streetName"),
                                    patient.getString("address"),
                                    1,
                                    0,
                                    1,
                                    0,
                                    new Date()
                            }
                    );
                }catch (Exception e){
                    logger.error("将i健康居民数据同步到互联网医院居民表失败:" + e.getMessage());
                    return users;
                }
                try {
                    this.getJdbcTemplate().update(PATIENT_INSERT_WEHCAT,
                            new Object[]{
                                    UUID.randomUUID().toString(),
                                    getWechatId(),
                                    patient.getString("code"),
                                    getOpenid(),
                                    new Date()
                            }
                    );
                }catch (Exception e){
                    logger.error("将i健康居民微信openid数据同步到互联网医院居民微信关联表失败:" + e.getMessage());
                    return users;
                }
            users = this.getJdbcTemplate().query(DEFAULT_PATIENT_DETAILS_STATEMENT, new BeanPropertyRowMapper(WlyyUserDetails.class),username,getIdcard());
            if (null == users || users.size() == 0) {
                //厦门I健康账号同步
                if("xm_ihealth_wx".equals(getWechatId())){
                    List<NameValuePair> params = new ArrayList<>();
                    params.add(new BasicNameValuePair("clientId", getClientId()));
                    ClientDetails clientDetails = clientDetailsService.loadClientByClientId(getClientId());
                    params.add(new BasicNameValuePair("clientSecret", clientDetails.getClientSecret()));
                    params.add(new BasicNameValuePair("code", username));
                    params.add(new BasicNameValuePair("openid", getOpenid()));
                    String res = null;
                    try{
                        res = httpClientUtil.post(getSynPath(getWechatId()), params, "UTF-8");
                    }catch (Exception e){
                        logger.error("远程请求i健康用户信息异常:" + e.getMessage());
                        return users;
                    }
                    if(null == res){
                        return users;
                    }
                    JSONObject patient = JSONObject.parseObject(res);
                    if(patient.getInteger("status") == -1){
                        logger.error("去i健康查询用户信息失败:" + patient.getString("error"));
                        return users;
                    }
                    try {
                        String salt = randomString(5);
                        String idcard = patient.getString("idcard");
                        String pw = idcard.substring(idcard.length()-6);
                        this.getJdbcTemplate().update(DEFAULT_PATIENT_INSERT_STATEMENT,
                                new Object[]{patient.getString("code"),
                                        patient.getString("photo"),
                                        patient.getString("idcard"),
                                        MD5.md5Hex(pw + "{" + salt + "}"),
                                        salt,
                                        patient.getString("name"),
                                        patient.getDate("birthday"),
                                        patient.getInteger("sex"),
                                        patient.getString("mobile"),
                                        patient.getString("province"),
                                        patient.getString("provinceName"),
                                        patient.getString("city"),
                                        patient.getString("cityName"),
                                        patient.getString("town"),
                                        patient.getString("townName"),
                                        patient.getString("street"),
                                        patient.getString("streetName"),
                                        patient.getString("address"),
                                        1,
                                        0,
                                        1,
                                        0,
                                        new Date()
                                }
                        );
                    }catch (Exception e){
                        logger.error("将i健康居民数据同步到互联网医院居民表失败:" + e.getMessage());
                        return users;
                    }
                    try {
                        this.getJdbcTemplate().update(PATIENT_INSERT_WEHCAT,
                                new Object[]{
                                        UUID.randomUUID().toString(),
                                        getWechatId(),
                                        patient.getString("code"),
                                        getOpenid(),
                                        new Date()
                                }
                        );
                    }catch (Exception e){
                        logger.error("将i健康居民微信openid数据同步到互联网医院居民微信关联表失败:" + e.getMessage());
                        return users;
                    }
                    WlyyUserDetails user = new WlyyUserDetails();
                    user.setName(patient.getString("name"));
                    user.setPassword(patient.getString("password"));
                    if(patient.getInteger("sex") == 1){
                        user.setGender(WlyyUserDetails.Gender.male);
                    }else if(patient.getInteger("sex") == 2){
                        user.setGender(WlyyUserDetails.Gender.female);
                    }
                    user.setId(username);
                    user.setIdcard(patient.getString("idcard"));
                    user.setMobile(patient.getString("mobile"));
                    user.setEnabled(true);
                    user.setLocked(false);
                    user.setLockedDate(null);
                    users.add(user);
                }else if("xm_zsyy_wx".equals(getWechatId())){
                    JSONObject jsonObject=new JSONObject();
                    jsonObject.put("openId",getOpenid());
                    String res = null;
                    try{
                        res = httpClientUtil.sendPost(getSynPath("xm_zsyy_wx"), jsonObject.toString());
                    }catch (Exception e){
                        logger.error("远程请求中山医院用户信息异常:" + e.getMessage());
                        return users;
                    }
                    if(null == res){
                        return users;
                    }
                    JSONObject object1 = JSONObject.parseObject(res);
                    if (null != object1&&null!=object1.get("status") && "200".equals(object1.get("status").toString())) {
                        JSONObject patient = object1.getJSONObject("userinfo");
                        String salt = randomString(5);
                        String mobile = patient.getString("mobile");
                        String pw = mobile.substring(mobile.length()-6);
                        try {
                            this.getJdbcTemplate().update(DEFAULT_PATIENT_INSERT_STATEMENT,
                                    new Object[]{patient.getString("id"),
                                            null,
                                            patient.getString("idcard"),
                                            MD5.md5Hex(pw + "{" + salt + "}"),
                                            salt,
                                            patient.getString("name"),
                                            patient.getDate("birthday"),
                                            patient.getInteger("sex"),
                                            mobile,
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            1,
                                            0,
                                            1,
                                            0,
                                            new Date()
                                    }
                            );
                        }catch (Exception e){
                            logger.error("将中山医院居民数据同步到互联网医院居民表失败:" + e.getMessage());
                            return users;
                        }
                        WlyyUserDetails user = new WlyyUserDetails();
                        user.setName(patient.getString("name"));
                        user.setPassword(MD5.md5Hex(pw + "{" + salt + "}"));
                        if(patient.getInteger("sex") == 1){
                            user.setGender(WlyyUserDetails.Gender.male);
                        }else if(patient.getInteger("sex") == 2){
                            user.setGender(WlyyUserDetails.Gender.female);
                        }
                        user.setId(patient.getString("id"));
                        user.setIdcard(patient.getString("idcard"));
                        user.setMobile(patient.getString("mobile"));
                        user.setEnabled(true);
                        user.setLocked(false);
                        user.setLockedDate(null);
                        users.add(user);
                    }else{
                        logger.error("远程请求中山医院用户信息异常:" + res.toString());
                        return users;
                    }
                WlyyUserDetails user = new WlyyUserDetails();
                user.setName(patient.getString("name"));
                user.setPassword(patient.getString("password"));
                if(patient.getInteger("sex") == 1){
                    user.setGender(WlyyUserDetails.Gender.male);
                }else if(patient.getInteger("sex") == 2){
                    user.setGender(WlyyUserDetails.Gender.female);
                }
                user.setId(username);
                user.setIdcard(patient.getString("idcard"));
                user.setMobile(patient.getString("mobile"));
                user.setEnabled(true);
                user.setLocked(false);
                user.setLockedDate(null);
                users.add(user);
            }
        }
        return users;
@ -313,6 +392,8 @@ public class WlyyUserDetailsService extends JdbcDaoSupport implements UserDetail
            result = this.getJdbcTemplate().queryForMap("select d.login_failure_count,d.last_login_failure_time from base_doctor d where d.mobile = ? or d.idcard = ?", username, username);
        } else if ("3".equals(loginType)) { //3.患者账号
            result = this.getJdbcTemplate().queryForMap("select p.login_failure_count,p.last_login_failure_time from base_patient p where p.mobile = ? or p.idcard = ?", username, username);
        } else if ("4".equals(loginType)) { //4.第三方患者账号
            result = this.getJdbcTemplate().queryForMap("select p.login_failure_count,p.last_login_failure_time from base_patient p where p.mobile = ? or p.idcard = ?", username, getIdcard());
        }
        return result;
    }
@ -333,7 +414,10 @@ public class WlyyUserDetailsService extends JdbcDaoSupport implements UserDetail
        } else if ("3".equals(loginType)) { //3.患者账号
            //账号锁定
            this.getJdbcTemplate().update("update base_patient p set p.login_failure_count = 5, p.locked = 1, p.locked_date = ?  where p.mobile = ? or p.idcard = ?", new Date(), username, username);
        } //...
        }else if ("4".equals(loginType)) { //3.同步的患者账号
            //账号锁定
            this.getJdbcTemplate().update("update base_patient p set p.login_failure_count = 5, p.locked = 1, p.locked_date = ?  where p.mobile = ? or p.idcard = ?", new Date(), username, getIdcard());
        }  //...
    }
    /**
@ -353,6 +437,9 @@ public class WlyyUserDetailsService extends JdbcDaoSupport implements UserDetail
        } else if ("3".equals(loginType)) { //3.患者账号
            //更新失败次数
            this.getJdbcTemplate().update("update base_patient p set p.login_failure_count = ? ,p.last_login_failure_time = ? where p.mobile = ? or p.idcard = ?", loginFailureCount, new Date(), username, username);
        } else if ("4".equals(loginType)) { //4.第三方同步患者账号
            //更新失败次数
            this.getJdbcTemplate().update("update base_patient p set p.login_failure_count = ? ,p.last_login_failure_time = ? where p.mobile = ? or p.idcard = ?", loginFailureCount, new Date(), username, getIdcard());
        } //...
    }
@ -392,7 +479,7 @@ public class WlyyUserDetailsService extends JdbcDaoSupport implements UserDetail
            users = this.getJdbcTemplate().query(DEFAULT_PATIENT_DETAILS_STATEMENT, new BeanPropertyRowMapper(WlyyUserSimple.class), username, username);
        } else if("4".equals(loginType)){
            //更新登录时间
            this.getJdbcTemplate().update("update base_patient p set p.login_failure_count = 0, p.login_date = ? where p.mobile = ? or p.idcard = ?", new Date(), username, username);
            this.getJdbcTemplate().update("update base_patient p set p.login_failure_count = 0, p.login_date = ? where p.mobile = ? or p.idcard = ?", new Date(), username, getIdcard());
            users = this.getJdbcTemplate().query(DEFAULT_PATIENT_DETAILS_IDCARD_STATEMENT, new BeanPropertyRowMapper(WlyyUserSimple.class), getIdcard());
        }
@ -485,6 +572,19 @@ public class WlyyUserDetailsService extends JdbcDaoSupport implements UserDetail
        this.getJdbcTemplate().update("update base_patient p set p.openid = ? where p.id= ?", openid, userId);
    }
    public String getSynPath(String wxId){
        String sql = "SELECT w.syn_path AS sysPath FROM wx_wechat w WHERE w.id ='"+wxId+"'";
        List<Map<String,Object>> list = this.getJdbcTemplate().queryForList(sql);
        if(list!=null&&list.size()>0){
            return (String)list.get(0).get("sysPath");
        }else{
            logger.error("getSynPath error");
        }
        return "";
    }
    public String randomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuffer buffer = new StringBuffer();

+ 13 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/dao/OauthKeypairDao.java

@ -0,0 +1,13 @@
package com.yihu.jw.security.dao;
import com.yihu.jw.security.model.OauthKeypairDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
 * Created by Trick on 2019/7/23.
 */
public interface OauthKeypairDao extends PagingAndSortingRepository<OauthKeypairDO, String>, JpaSpecificationExecutor<OauthKeypairDO> {
    OauthKeypairDO findByCode(String code);
}

+ 47 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/model/OauthKeypairDO.java

@ -0,0 +1,47 @@
package com.yihu.jw.security.model;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
/**
 * Created by Trick on 2019/7/23.
 */
@Entity
@Table(name = "oauth_keypair")
public class OauthKeypairDO {
    private Integer id;
    private String code;
    private byte[] keyPair;
    @Id
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "identity")
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Column(name = "code")
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    @Column(name = "key_pair")
    public byte[] getKeyPair() {
        return keyPair;
    }
    public void setKeyPair(byte[] keyPair) {
        this.keyPair = keyPair;
    }
}

+ 55 - 1
server/svr-authentication/src/main/java/com/yihu/jw/security/oauth2/provider/endpoint/WlyyLoginEndpoint.java

@ -1,16 +1,20 @@
package com.yihu.jw.security.oauth2.provider.endpoint;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.entity.base.login.BaseLoginLogDO;
import com.yihu.jw.restmodel.ResultStatus;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.security.core.userdetails.jdbc.WlyyUserDetailsService;
import com.yihu.jw.security.dao.OauthKeypairDao;
import com.yihu.jw.security.login.service.BaseLoginLogService;
import com.yihu.jw.security.model.*;
import com.yihu.jw.security.oauth2.core.redis.WlyyRedisVerifyCodeService;
import com.yihu.jw.security.oauth2.provider.WlyyTokenGranter;
import com.yihu.jw.security.oauth2.provider.error.WlyyOAuth2ExceptionTranslator;
import com.yihu.jw.security.utils.SerializeUtil;
import com.yihu.utils.security.RSAUtils;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -45,6 +49,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.text.SimpleDateFormat;
@ -52,6 +57,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.alibaba.fastjson.JSON;
/**
 * <p>
@ -90,6 +96,8 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
    private StringRedisTemplate redisTemplate;
    @Autowired
    private BaseLoginLogService baseLoginLogService;
    @Autowired
    private OauthKeypairDao oauthKeypairDao;
    @PostConstruct
    private void init() {
@ -104,7 +112,7 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
     *                    captcha 验证码
     *                    password 密码
     *                    username 用户名/手机/身份证号
     *                    login_type 用户类型 1或默认为user,2:医生登录,3:患者登录
     *                    login_type 用户类型 1或默认为user,2:医生登录,3:患者登录,4:第三方同步账号登录
     * @param httpSession
     * @return
     * @throws Exception
@ -127,6 +135,7 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
                String password = com.yihu.jw.security.utils.RSAUtils.decryptBase64(parameters.get("password"), keyPair);
                parameters.put("password", password);
            } else {
                //第三方同步账号模式登录
                parameters.put("grant_type", "ihealthCode");
            }
        } else {
@ -597,4 +606,49 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
        return response;
    }
    @RequestMapping(value = "/oauth/getHwlyyPublicKey", method = RequestMethod.GET)
    public ObjEnvelop getHwlyyPublicKey(){
//        KeyPair keyPair = com.yihu.jw.security.utils.RSAUtils.getKey();
//        byte[] bytekey = SerializeUtil.ObjTOSerialize(keyPair);
//        KeyPair key = (KeyPair)SerializeUtil.unSerialize(bytekey);
//        OauthKeypairDO keypairDO = new OauthKeypairDO();
//        keypairDO.setCode("hwlyyKey");
//        keypairDO.setKeyPair(bytekey);
//        oauthKeypairDao.save(keypairDO);
        OauthKeypairDO keypairDO = oauthKeypairDao.findByCode("hwlyyKey");
        KeyPair key = (KeyPair)SerializeUtil.unSerialize(keypairDO.getKeyPair());
        PublickeyVO pk = new PublickeyVO();
        pk.setPublicKey(com.yihu.jw.security.utils.RSAUtils.generateBase64PublicKey(key));
        return ObjEnvelop.getSuccess("success", pk);
    }
    @RequestMapping(value = "/oauth/getHwlyyDecrypt", method = RequestMethod.POST)
    public ObjEnvelop getHwlyyDecrypt(String openid,String idcard,String wechatId){
        Map<String,Object> rs = new HashedMap();
        try{
            OauthKeypairDO keypairDO = oauthKeypairDao.findByCode("hwlyyKey");
            KeyPair keyPair = (KeyPair)SerializeUtil.unSerialize(keypairDO.getKeyPair());
            //解密openid
            if(org.apache.commons.lang3.StringUtils.isNotBlank(openid)){
                rs.put("openid",com.yihu.jw.security.utils.RSAUtils.decryptBase64(openid, keyPair));
            }else {
                rs.put("openid",openid);
            }
            //解密idcard
            if(org.apache.commons.lang3.StringUtils.isNotBlank(idcard)){
                rs.put("idcard",com.yihu.jw.security.utils.RSAUtils.decryptBase64(idcard, keyPair));
            }else {
                rs.put("idcard",idcard);
            }
            //解密wechatId
            if(org.apache.commons.lang3.StringUtils.isNotBlank(wechatId)){
                rs.put("wechatId",com.yihu.jw.security.utils.RSAUtils.decryptBase64(wechatId, keyPair));
            }else {
                rs.put("wechatId",wechatId);
            }
        }catch (Exception e){
            logger.error(e);
        }
        return ObjEnvelop.getSuccess("success", rs);
    }
}

+ 58 - 2
server/svr-authentication/src/main/java/com/yihu/jw/security/utils/HttpClientUtil.java

@ -19,8 +19,9 @@ import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -303,4 +304,59 @@ public class HttpClientUtil {
        }
        return null;
    }
    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url
     *            发送请求的 URL带上参数
     * @param param
     *            POST参数。
     * @return 所代表远程资源的响应结果
     */
    public  String sendPost(String url, String param) {
        StringBuffer buffer = new StringBuffer();
        PrintWriter out = null;
        BufferedReader in = null;
        HttpURLConnection conn = null;
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            osw.write(param.toString());
            osw.flush();
            // 读取返回内容
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String temp;
            while ((temp = br.readLine()) != null) {
                buffer.append(temp);
                buffer.append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return buffer.toString();
    }
}

+ 58 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/utils/SerializeUtil.java

@ -0,0 +1,58 @@
package com.yihu.jw.security.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 * Created by Trick on 2019/7/23.
 */
public class SerializeUtil {
    private static final Logger log = LoggerFactory.getLogger(SerializeUtil.class);
    /**
     *
     * <p>Title: ObjTOSerialize</p>
     * <p>Description: 序列化一个对象</p>
     * @param obj
     * @return
     * @author guangshuai.wang
     */
    public static byte[] ObjTOSerialize(Object obj){
        ObjectOutputStream oos = null;
        ByteArrayOutputStream byteOut = null;
        try{
            byteOut = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(byteOut);
            oos.writeObject(obj);
            byte[] bytes = byteOut.toByteArray();
            return bytes;
        }catch (Exception e) {
            log.error("对象序列化失败");
        }
        return null;
    }
    /**
     *
     * <p>Title: unSerialize</p>
     * <p>Description: 反序列化</p>
     * @param bytes
     * @return
     * @author guangshuai.wang
     */
    public static Object unSerialize(byte[] bytes){
        ByteArrayInputStream in = null;
        try{
            in = new ByteArrayInputStream(bytes);
            ObjectInputStream objIn = new ObjectInputStream(in);
            return objIn.readObject();
        }catch (Exception e) {
            log.error("反序列化失败");
        }
        return null;
    }
}