forked from rongchao/epmet-cloud-rizhao
				
			
				 99 changed files with 784 additions and 260 deletions
			
			
		| @ -1,2 +1,2 @@ | |||
| /*ALTER TABLE dim_agency ADD COLUMN AGENCY_DIM_TYPE VARCHAR(10) NOT NULL COMMENT '机关维度类型。self:机关本身自己,all:机关自己+下级+网格+部门等';*/ | |||
| #ALTER TABLE dim_agency ADD COLUMN AGENCY_DIM_TYPE VARCHAR(10) NOT NULL COMMENT '机关维度类型。self:机关本身自己,all:机关自己+下级+网格+部门等'; | |||
| select 1; | |||
| @ -0,0 +1,46 @@ | |||
| package com.epmet.controller; | |||
| 
 | |||
| import com.epmet.commons.tools.utils.Result; | |||
| import com.epmet.dto.result.UploadImgResultDTO; | |||
| import com.epmet.service.OssService; | |||
| import org.apache.commons.io.FileUtils; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.mock.web.MockMultipartFile; | |||
| import org.springframework.web.bind.annotation.*; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| 
 | |||
| import java.io.*; | |||
| 
 | |||
| @RestController | |||
| @RequestMapping("test") | |||
| public class TestController { | |||
| 
 | |||
|     @Autowired | |||
|     private OssService ossService; | |||
| 
 | |||
|     @PostMapping("local-upload") | |||
|     public Result localUpload(@RequestPart("file") MultipartFile file, @RequestParam("fileName") String fileName) { | |||
| 
 | |||
|         final File tempFile = new File("/opt/upload_files/" + fileName); | |||
| 
 | |||
|         try(InputStream inputStream = file.getInputStream()) { | |||
|             FileUtils.copyInputStreamToFile(inputStream, tempFile); | |||
|         } catch (IOException e) { | |||
|             e.printStackTrace(); | |||
|         } | |||
|         return new Result(); | |||
|     } | |||
| 
 | |||
|     @PostMapping("upload2aliyun") | |||
|     public Result upload2aliyun(@RequestParam("fileName") String fileName) { | |||
|         try (final FileInputStream fis = new FileInputStream("/opt/upload_files/" + fileName)) { | |||
|             final MockMultipartFile mockMultipartFile = new MockMultipartFile(fileName, fis); | |||
|             return ossService.uploadImg(mockMultipartFile, null); | |||
|         } catch (FileNotFoundException e) { | |||
|             e.printStackTrace(); | |||
|         } catch (IOException e) { | |||
|             e.printStackTrace(); | |||
|         } | |||
|         return new Result(); | |||
|     } | |||
| } | |||
| @ -0,0 +1,23 @@ | |||
| package com.epmet.controller; | |||
| 
 | |||
| import com.epmet.commons.tools.utils.Result; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.cloud.commons.util.InetUtils; | |||
| import org.springframework.web.bind.annotation.PostMapping; | |||
| import org.springframework.web.bind.annotation.RequestMapping; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| 
 | |||
| @RestController | |||
| @RequestMapping("lb") | |||
| public class LbController { | |||
| 
 | |||
|     @Autowired | |||
|     private InetUtils inetUtils; | |||
| 
 | |||
|     @PostMapping("get-host") | |||
|     public Result getHost() { | |||
|         String ipAddress = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); | |||
|         return new Result().ok(ipAddress); | |||
|     } | |||
| 
 | |||
| } | |||
| @ -0,0 +1,152 @@ | |||
| package com.epmet.openapi.scan.common.util; | |||
| 
 | |||
| import org.apache.commons.codec.binary.Base64; | |||
| import org.bouncycastle.util.encoders.UrlBase64; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| 
 | |||
| import javax.crypto.Cipher; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.InputStream; | |||
| import java.security.KeyFactory; | |||
| import java.security.PrivateKey; | |||
| import java.security.PublicKey; | |||
| import java.security.Signature; | |||
| import java.security.cert.Certificate; | |||
| import java.security.cert.CertificateFactory; | |||
| import java.security.spec.PKCS8EncodedKeySpec; | |||
| import java.security.spec.X509EncodedKeySpec; | |||
| 
 | |||
| /** | |||
|  * @author jianjun liu | |||
|  * @date 2020-06-05 16:48 | |||
|  **/ | |||
| 
 | |||
| public class RSASignature { | |||
|     private static final Logger LOGGER = LoggerFactory.getLogger(RSASignature.class); | |||
|     public static final String KEY_ALGORITHM = "RSA"; | |||
|     public static final String SIGNATURE_ALGORITHM = "SHA1WithRSA"; | |||
|     public static final String ENCODING = "utf-8"; | |||
|     public static final String X509 = "X.509"; | |||
| 
 | |||
|     /** | |||
|      * 获取私钥 | |||
|      * | |||
|      * @param key | |||
|      * @return | |||
|      * @throws Exception | |||
|      */ | |||
|     public static PrivateKey getPrivateKey(String key) throws Exception { | |||
|         byte[] keyBytes = Base64.decodeBase64(key.getBytes(ENCODING)); | |||
|         PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); | |||
|         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); | |||
|         PrivateKey privateKey = keyFactory.generatePrivate(keySpec); | |||
|         return privateKey; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 获取公钥 | |||
|      * | |||
|      * @param key | |||
|      * @return | |||
|      * @throws Exception | |||
|      */ | |||
|     public static PublicKey getPublicKey(String key) throws Exception { | |||
|         byte[] keyBytes = Base64.decodeBase64(key.getBytes(ENCODING)); | |||
|         CertificateFactory certificateFactory = CertificateFactory.getInstance(X509); | |||
|         InputStream in = new ByteArrayInputStream(keyBytes); | |||
|         Certificate certificate = certificateFactory.generateCertificate(in); | |||
|         PublicKey publicKey = certificate.getPublicKey(); | |||
|         return publicKey; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 使用公钥对明文进行加密,返回BASE64编码的字符串 | |||
|      * | |||
|      * @param publicKey | |||
|      * @param plainText | |||
|      * @return | |||
|      */ | |||
|     public static String encrypt(String publicKey, String plainText) { | |||
|         try { | |||
|             KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); | |||
|             byte[] encodedKey = Base64.decodeBase64(publicKey.getBytes(ENCODING)); | |||
|             PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); | |||
|             Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); | |||
|             cipher.init(Cipher.ENCRYPT_MODE, pubKey); | |||
|             byte[] enBytes = cipher.doFinal(plainText.getBytes()); | |||
|             return new String(Base64.encodeBase64(enBytes)); | |||
|         } catch (Exception e) { | |||
|             LOGGER.error("rsa encrypt exception: {}", e.getMessage(), e); | |||
|         } | |||
|         return null; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 使用私钥对明文密文进行解密 | |||
|      * | |||
|      * @param privateKey | |||
|      * @param enStr | |||
|      * @return | |||
|      */ | |||
|     public static String decrypt(String privateKey, String enStr) { | |||
|         try { | |||
|             PrivateKey priKey = getPrivateKey(privateKey); | |||
|             Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); | |||
|             cipher.init(Cipher.DECRYPT_MODE, priKey); | |||
|             byte[] deBytes = cipher.doFinal(Base64.decodeBase64(enStr)); | |||
|             return new String(deBytes); | |||
|         } catch (Exception e) { | |||
|             LOGGER.error("rsa decrypt exception: {}", e.getMessage(), e); | |||
|         } | |||
|         return null; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * RSA私钥签名 | |||
|      * | |||
|      * @param content    待签名数据 | |||
|      * @param privateKey 私钥 | |||
|      * @return 签名值 | |||
|      */ | |||
|     public static String signByPrivateKey(String content, String privateKey) { | |||
|         try { | |||
|             PrivateKey priKey = getPrivateKey(privateKey); | |||
|             Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); | |||
|             signature.initSign(priKey); | |||
|             signature.update(content.getBytes(ENCODING)); | |||
|             byte[] signed = signature.sign(); | |||
|             return new String(UrlBase64.encode(signed), ENCODING); | |||
|         } catch (Exception e) { | |||
|             LOGGER.error("sign error, content: {}", content, e); | |||
|         } | |||
|         return null; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 公钥验签 | |||
|      * | |||
|      * @param content | |||
|      * @param sign | |||
|      * @param publicKey | |||
|      * @return | |||
|      */ | |||
|     public static boolean verifySignByPublicKey(String content, String sign, String publicKey) { | |||
|         try { | |||
|             KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); | |||
|             byte[] encodedKey = Base64.decodeBase64(publicKey.getBytes(ENCODING)); | |||
|             PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); | |||
| 
 | |||
|             Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); | |||
|             signature.initVerify(pubKey); | |||
|             signature.update(content.getBytes(ENCODING)); | |||
| 
 | |||
|             return signature.verify(UrlBase64.decode(sign.getBytes(ENCODING))); | |||
| 
 | |||
|         } catch (Exception e) { | |||
|             LOGGER.error("verify sign error, content: {}, sign: {}", content, sign, e); | |||
|         } | |||
|         return false; | |||
|     } | |||
| 
 | |||
| } | |||
| @ -0,0 +1,24 @@ | |||
| package com.epmet.controller; | |||
| 
 | |||
| import com.epmet.commons.tools.utils.Result; | |||
| import com.epmet.feign.LbFeignClient; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.web.bind.annotation.PostMapping; | |||
| import org.springframework.web.bind.annotation.RequestMapping; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| 
 | |||
| import javax.servlet.http.HttpServletRequest; | |||
| 
 | |||
| @RestController | |||
| @RequestMapping("lbtest") | |||
| public class LbTestController { | |||
| 
 | |||
|     @Autowired | |||
|     private LbFeignClient lbFeignClient; | |||
| 
 | |||
|     @PostMapping("get-host") | |||
|     public Result getHost() { | |||
|         Result host = lbFeignClient.getHost(); | |||
|         return host; | |||
|     } | |||
| } | |||
| @ -0,0 +1,12 @@ | |||
| package com.epmet.feign; | |||
| 
 | |||
| import com.epmet.commons.tools.utils.Result; | |||
| import com.epmet.feign.fallback.LbFeignClientFallback; | |||
| import org.springframework.cloud.openfeign.FeignClient; | |||
| import org.springframework.web.bind.annotation.PostMapping; | |||
| 
 | |||
| @FeignClient(name = "gov-voice-server", fallback = LbFeignClientFallback.class) | |||
| public interface LbFeignClient { | |||
|     @PostMapping("/gov/voice/lb/get-host") | |||
|     Result getHost(); | |||
| } | |||
| @ -0,0 +1,13 @@ | |||
| package com.epmet.feign.fallback; | |||
| 
 | |||
| import com.epmet.commons.tools.utils.Result; | |||
| import com.epmet.feign.LbFeignClient; | |||
| import org.springframework.stereotype.Component; | |||
| 
 | |||
| @Component | |||
| public class LbFeignClientFallback implements LbFeignClient { | |||
|     @Override | |||
|     public Result getHost() { | |||
|         return new Result().error(0, "请求失败"); | |||
|     } | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue