无法使用SpringBoot以编程方式传递Keycloak登录页面

时间:2019-10-15 14:22:20

标签: authentication keycloak

我无法避免进入keycloak登录页面/无法以编程方式对Keycloak进行身份验证

我尝试获取访问令牌和刷新令牌,然后传递访问令牌以获取如下所示的主要详细信息,但是无法通过密钥斗篷登录页面/

Springboot代码

@GetMapping("/authenticatekeyclok")
 public HttpEntity keyclock() 
{
    String res=""; HttpEntity<String> response=null;
    System.out.println("enter into keycloak====");
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.set("Accept", "application/json");
    String url = "http://10.209.22.23:8080/auth/realms/master/protocol/openid-connect/token";
    String dataJSON = "grant_type=password&username=admin&password=admin&client_id=security-admin-console&client_secret=135c0e89-c5f5-4499-a64e-c5219f59f31d";
    HttpEntity formEntity = new HttpEntity<>(dataJSON, headers);
    System.out.println("print headers===="+formEntity.getBody());
    //Trying to get tokens
    response = restTemplate.exchange("http://10.209.22.23:8080/auth/realms/master/protocol/openid-connect/token", HttpMethod.POST, formEntity, String.class);
    System.out.println("response generated============= ");
    System.out.println("O/P : for first api"+response.getBody());
    StringTokenizer st= new StringTokenizer(response.getBody(),",");
    StringTokenizer st1;
    String s="";
    String refreshtoken=null;
    while(st.hasMoreTokens()) {
        s=st.nextToken();
        if(s.contains("refresh_token")) {
            st1=new StringTokenizer(s,":");
            while(st1.hasMoreTokens()) {
                refreshtoken=st1.nextToken();
                refreshtoken= refreshtoken.substring(1,refreshtoken.length()-1);
                System.out.println(refreshtoken);
            }
        } 
    }

    dataJSON = "grant_type=refresh_token&client_id=security-admin-console&client_secret=135c0e89-c5f5-4499-a64e-c5219f59f31d&redirect_uri=http://10.209.22.23:5555/auth/admin/master/console/#/realms/master&refresh_token="+refreshtoken;
    HttpEntity entity1 = new HttpEntity<>(dataJSON, headers);
    response = restTemplate.exchange(url, HttpMethod.POST, entity1, String.class);
    System.out.println("O/P for 2nd ====================== : "+response.getBody());
    st= new StringTokenizer(response.getBody(),",");
    s="";
    String accesstoken=null;
    while(st.hasMoreTokens()) {
        s=st.nextToken();
        if(s.contains("access_token")) {
            st1=new StringTokenizer(s,":");
                  while(st1.hasMoreTokens()) {
                      accesstoken=st1.nextToken();
                      accesstoken= accesstoken.substring(1,accesstoken.length()-1);
                      System.out.println(accesstoken);
                  }
        } 
    }
    dataJSON=""; //ResponseEntity<String> respo = null;
    HttpHeaders headers1 = new HttpHeaders();
    headers1.setContentType(MediaType.APPLICATION_JSON);
    headers1.set("Authorization", "Bearer "+accesstoken);
    HttpEntity entity = new HttpEntity<String>(headers1);
    //Pass the access token and trying to go to admin console of keycloak
    response = restTemplate.exchange("http://10.209.22.23:8080/auth/admin/master/console", HttpMethod.GET, entity, String.class);
    System.out.println("O/P for third======================: "+response.getBody());
    return response;
}

1 个答案:

答案 0 :(得分:0)

您正在尝试docker service update --image=...个请求。在浏览器或前端不可见的情况下,这肯定是可以实现的。

您会发现sample code有用。