Open lusains opened 2 years ago
hello~we can use cookieJar to get super administrator privileges.
public static void main(String[] args) { String url = "http://localhost:8070/signin"; FormBody loginBody = new FormBody.Builder() .add("username", "apollo") .add("password", "admin") .build(); OkHttpClient client = new OkHttpClient.Builder().cookieJar(new CookieJar() { private final HashMap<String, List<okhttp3.Cookie>> cookieStore = new HashMap<>(); @Override public void saveFromResponse(HttpUrl url, List<okhttp3.Cookie> cookies) { cookieStore.put(url.host(), cookies); } @Override public List<okhttp3.Cookie> loadForRequest(HttpUrl url) { List<okhttp3.Cookie> cookies = cookieStore.get(url.host()); return cookies != null ? cookies : new ArrayList<>(); } }).build(); //调用登录接口,刷新cookie到CookieJar Request request = new Request.Builder().url(url).post(loginBody).build(); Response response = client.newCall(request).execute(); try { //调用业务接口,通过loadForRequest请求授权等管理器接口 String url2 = "http://localhost:8070/consumers/509c8cc78e667529eaaf332c44a845b9c58857b5/assign-role?type=AppRole"; String json = "{\"appId\":\"sentinel-dashboard\"}"; RequestBody body2 = RequestBody.create(JSON, json); Response response2 = client.newCall(request2).execute(); System.out.println(response2.body().string()); } catch (IOException e) { e.printStackTrace(); } }
+1
hello~we can use cookieJar to get super administrator privileges.