周壕 3 månader sedan
förälder
incheckning
b676ff55f4

+ 4 - 0
src/main/java/com/bowintek/practice/config/ElasticsearchConfig.java

@@ -101,6 +101,10 @@ public class ElasticsearchConfig {
         builder.authenticate(restClient);
 
         Request request = new Request("GET", "/test4/_search");
+        request.getOptions().getHeaders()
+                .add(new BasicHeader("Authorization", "Negotiate " + builder.getNewToken()));
+        log.info("header size:" + request.getOptions().getHeaders().size());
+
         Cancellable cancellable = restClient.performRequestAsync(
                 request,
                 new ResponseListener() {

+ 16 - 0
src/main/java/com/bowintek/practice/config/SmRestClientBuilder.java

@@ -500,6 +500,22 @@ public final class SmRestClientBuilder {
         return token;
     }
 
+    public String getNewToken() {
+        byte[] tokenN = initiateSecurityContext(getSubj(), getServerRealm());
+
+        for(int times = 0; null == tokenN && times < 3; ++times) {
+            SmRestClientBuilder.LOG.error("InitiateSecurityContext again.");
+            tokenN = initiateSecurityContext(getSubj(), getServerRealm());
+        }
+
+        if (null == tokenN) {
+            throw new IllegalArgumentException("Get security token failed.");
+        } else {
+            //this.createSTTime = System.currentTimeMillis();
+            return new String(Base64.getEncoder().encode(tokenN), StandardCharsets.UTF_8);
+        }
+    }
+
     private static GSSContext getGssContext(String servicePrincipalName) throws GSSException {
         GSSManager manager = GSSManager.getInstance();
         GSSName serverName = manager.createName(servicePrincipalName, new Oid("1.2.840.113554.1.2.2.1"));