正在从Azure Java SDK列出我的Azure权限

正在从Azure Java SDK列出我的Azure权限,java,azure,azure-java-sdk,Java,Azure,Azure Java Sdk,有Azure API用于列出我自己的权限。部分记录在中(虽然他们在文档中遗漏了每个订阅案例) 我正在努力找到一种通过调用此API的方法-有一个可通过.accessManagement()方法访问的接口,但它包含用于列出角色和角色分配的方法,而不是用于列出实际权限的方法 SDK中是否缺少此功能,或者我只是搜索得不好?有时Azure SDK缺少一些功能。我还检查了JavaSDK源代码,似乎没有这样的接口可以直接调用这个API 因此,这里有两个选项: 1。获取角色分配,以便您可以获取实际角色ID,使用

有Azure API用于列出我自己的权限。部分记录在中(虽然他们在文档中遗漏了每个订阅案例)

我正在努力找到一种通过调用此API的方法-有一个可通过
.accessManagement()
方法访问的接口,但它包含用于列出角色和角色分配的方法,而不是用于列出实际权限的方法


SDK中是否缺少此功能,或者我只是搜索得不好?

有时Azure SDK缺少一些功能。我还检查了JavaSDK源代码,似乎没有这样的接口可以直接调用这个API

因此,这里有两个选项:

1。获取角色分配,以便您可以获取实际角色ID,使用此角色ID您可以通过以下代码获取角色的实际权限:

        Set<Permission> permissions = azureResourceManager.accessManagement().roleDefinitions().getById(
                        "{role id}")
                        .permissions();
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.stream.Collectors;

import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.google.gson.Gson;

public class testAzureAPI {

    public static void main(String[] args) {

        AzureProfile azureProfile = new AzureProfile(AzureEnvironment.AZURE);
        //I use ClientSecretCredential just for demo here, you can change it your self
        TokenCredential tokenCredential = new ClientSecretCredentialBuilder()
                .clientId("").clientSecret("")
                .tenantId("")  
   .authorityHost(azureProfile.getEnvironment().getActiveDirectoryEndpoint()).build();

        String accessToken = tokenCredential
                .getToken(new TokenRequestContext().addScopes("https://management.azure.com/.default")).block()
                .getToken();

        String reqURL = "https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Authorization/permissions?api-version=2015-07-01";

        try {

            URL url = new URL(reqURL);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("Authorization", "Bearer " + accessToken);
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine = in.lines().collect(Collectors.joining());
            in.close();

            Permissions perms = new Gson().fromJson(inputLine, Permissions.class);

            System.out.println(perms.getValue().get(2).getActions());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class Value {
        public List<String> actions;

        public List<Object> notActions;

        public List<String> getActions() {
            return actions;
        }

        public void setActions(List<String> actions) {
            this.actions = actions;
        }

        public List<Object> getNotActions() {
            return notActions;
        }

        public void setNotActions(List<Object> notActions) {
            this.notActions = notActions;
        }
    }

    public class Permissions {
        public List<Value> value;

        public List<Value> getValue() {
            return value;
        }

        public void setValue(List<Value> value) {
            this.value = value;
        }
    }
}
Set permissions=azureResourceManager.accessManagement().roleDefinitions().getById(
“{角色id}”)
.permissions();
2.直接调用REST API,只需尝试以下代码:

        Set<Permission> permissions = azureResourceManager.accessManagement().roleDefinitions().getById(
                        "{role id}")
                        .permissions();
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.stream.Collectors;

import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.google.gson.Gson;

public class testAzureAPI {

    public static void main(String[] args) {

        AzureProfile azureProfile = new AzureProfile(AzureEnvironment.AZURE);
        //I use ClientSecretCredential just for demo here, you can change it your self
        TokenCredential tokenCredential = new ClientSecretCredentialBuilder()
                .clientId("").clientSecret("")
                .tenantId("")  
   .authorityHost(azureProfile.getEnvironment().getActiveDirectoryEndpoint()).build();

        String accessToken = tokenCredential
                .getToken(new TokenRequestContext().addScopes("https://management.azure.com/.default")).block()
                .getToken();

        String reqURL = "https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Authorization/permissions?api-version=2015-07-01";

        try {

            URL url = new URL(reqURL);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("Authorization", "Bearer " + accessToken);
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine = in.lines().collect(Collectors.joining());
            in.close();

            Permissions perms = new Gson().fromJson(inputLine, Permissions.class);

            System.out.println(perms.getValue().get(2).getActions());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class Value {
        public List<String> actions;

        public List<Object> notActions;

        public List<String> getActions() {
            return actions;
        }

        public void setActions(List<String> actions) {
            this.actions = actions;
        }

        public List<Object> getNotActions() {
            return notActions;
        }

        public void setNotActions(List<Object> notActions) {
            this.notActions = notActions;
        }
    }

    public class Permissions {
        public List<Value> value;

        public List<Value> getValue() {
            return value;
        }

        public void setValue(List<Value> value) {
            this.value = value;
        }
    }
}
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.net.URL;
导入java.net.URLConnection;
导入java.util.List;
导入java.util.stream.collector;
导入com.azure.core.credential.TokenCredential;
导入com.azure.core.credential.TokenRequestContext;
导入com.azure.core.management.AzureEnvironment;
导入com.azure.core.management.profile.AzureProfile;
导入com.azure.identity.ClientSecretCredentialBuilder;
导入com.google.gson.gson;
公共类testAzureAPI{
公共静态void main(字符串[]args){
AzureProfile-AzureProfile=新的AzureProfile(AzureEnvironment.AZURE);
//我在这里使用ClientSecretCredential只是为了演示,您可以自己更改它
TokenCredential TokenCredential=新客户端secretCredentialBuilder()
.clientId(“”).clientSecret(“”)
.tenantId(“”)
.authorityHost(azureProfile.getEnvironment().getActiveDirectoryEndpoint()).build();
字符串accessToken=tokenCredential
.getToken(新TokenRequestContext().addScopes(“https://management.azure.com/.default))块()
.getToken();
字符串请求URL=”https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Authorization/permissions?api version=2015-07-01”;
试一试{
URL=新URL(请求URL);
URLConnection conn=url.openConnection();
conn.setRequestProperty(“授权”、“承载人”+accessToken);
BufferedReader in=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
字符串inputLine=in.lines().collect(collector.joining());
in.close();
Permissions perms=new Gson().fromJson(inputLine,Permissions.class);
System.out.println(perms.getValue().get(2.getActions());
}捕获(例外e){
e、 printStackTrace();
}
}
公共阶级价值{
公开名单行动;
公开列表注释;
公共列表getActions(){
返回动作;
}
公共无效设置操作(列出操作){
这个动作=动作;
}
公共列表getnotations(){
返回符号;
}
公共无效设置注释(列表注释){
this.notActions=notActions;
}
}
公共类权限{
公共列表值;
公共列表getValue(){
返回值;
}
公共无效设置值(列表值){
这个值=值;
}
}
}
我已经在我这边进行了测试,它非常适合我:

结果: 按API分类:

按代码:

感谢@Stanley Gong提供了详尽的答案和详细的备选方案,这比我要求的还要多。我会等几天,以防有人在SDK中神奇地发现这个,然后我会接受你的回答。@Michal没问题,伙计:)