LogInHelper.java
package com.yd.dcap.integration.helper; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.yd.dcap.integration.EnvConfig; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.commons.codec.binary.Base64; import org.glassfish.jersey.jackson.JacksonFeature; import javax.crypto.Cipher; import javax.ws.rs.ProcessingException; import javax.ws.rs.client.*; import javax.ws.rs.core.MediaType; import java.nio.charset.StandardCharsets; import java.security.KeyFactory; import java.security.interfaces.RSAPublicKey; import java.security.spec.X509EncodedKeySpec; public class LogInHelper { public static String getToken(String account, String password) { Client client = ClientBuilder.newBuilder().hostnameVerifier((s1, s2) -> true).register(new JacksonFeature()).build(); WebTarget webTarget = client.target(EnvConfig.base_url + "api/user/getPublicKey"); Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); PubKey publicKey = invocationBuilder.get(PubKey.class); byte[] decoded = Base64.decodeBase64(publicKey.data); RSAPublicKey pubKey; for (int i = 0; i < 3; i++) { try { pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); String outStr = Base64.encodeBase64String(cipher.doFinal(password.getBytes(StandardCharsets.UTF_8))); LoginData loginData = new LoginData(account, outStr); LoginResponse loginResponse = client.target(EnvConfig.base_url + "api/login") .request(MediaType.APPLICATION_JSON) .post(Entity.entity(loginData, MediaType.APPLICATION_JSON), LoginResponse.class); return loginResponse.data.token; } catch (ProcessingException ex) { ex.printStackTrace(); } catch (Exception e) { e.printStackTrace(); return null; } } return null; } @Data @AllArgsConstructor @NoArgsConstructor public static class PubKey { private int code; private String data; } @Data @AllArgsConstructor @NoArgsConstructor public static class LoginData { private String account; public String password; } @Data @AllArgsConstructor @NoArgsConstructor public static class LoginResponse { public int code; public LoginResponseData data; } @Data @AllArgsConstructor @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public static class LoginResponseData { public String token; } }
PlatformClient.java
package com.yd.dcap.integration.helper; import com.yd.dcap.integration.EnvConfig; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.jackson.JacksonFeature; import javax.ws.rs.ProcessingException; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.MediaType; import java.util.HashMap; import java.util.Map; public class PlatformClient { //private Invocation.Builder builder; String token; public PlatformClient(String userName, String password) { token = LogInHelper.getToken(userName, password); } private Invocation.Builder getBuilder(String path) { ClientConfig configuration = new ClientConfig(); configuration = configuration.property(ClientProperties.CONNECT_TIMEOUT, 30000); configuration = configuration.property(ClientProperties.READ_TIMEOUT, 30000); Client client = ClientBuilder.newBuilder().withConfig(configuration).hostnameVerifier((s1, s2) -> true).register(new JacksonFeature()).build(); return client.target(EnvConfig.base_url).path(path) .request(MediaType.APPLICATION_JSON) .header("token", token); } public <T> T doPostRequest(String url, Object payload, Class<T> klass) { Invocation.Builder invoker = this.getBuilder(url); try { return invoker.post(Entity.entity(payload, MediaType.APPLICATION_JSON), klass); } catch (ProcessingException ex) { return invoker.post(Entity.entity(payload, MediaType.APPLICATION_JSON), klass); } catch (Exception e) { e.printStackTrace(); return null; } } public <T> T doPostRequest(String url, Object payload, String mtype, Class<T> klass) { Invocation.Builder invoker = this.getBuilder(url); try { return invoker.post(Entity.entity(payload, mtype), klass); } catch (ProcessingException ex) { return invoker.post(Entity.entity(payload, mtype), klass); } catch (Exception e) { e.printStackTrace(); return null; } } public <T> T doGetRequest(String url, Class<T> klass) { Invocation.Builder invoker = this.getBuilder(url); try { return invoker.get(klass); } catch (ProcessingException ex) { return invoker.get(klass); } catch (Exception e) { e.printStackTrace(); return null; } } static Map<String, PlatformClient> clientMap = new HashMap<>(); public static PlatformClient getClient(String userName, String password) { return clientMap.computeIfAbsent(userName, k -> new PlatformClient(userName, password)); } }
标签:http,String,javax,return,client,import,login,getToken,public From: https://www.cnblogs.com/panda4671/p/16837573.html