代码之家  ›  专栏  ›  技术社区  ›  user840930

除了keydrope令牌之外,我还需要什么来访问由keydrope保护的服务吗?

  •  0
  • user840930  · 技术社区  · 6 年前

    我可以从key斗篷获得一个有效的令牌,但是当我尝试使用令牌访问它时,我仍然从一个由key斗篷保护的服务获得404notfound响应。我还要做什么?还需要什么来访问由keydeap保护的服务?

    我要拿回不记名代币。

    我创建了最简单的服务,并将其部署为对Wildfly的一场战争。我在wildfly中安装了keydepot适配器,并在war中添加了keydepot.json文件,同时修改了web.xml。

    <module-name>application</module-name>
    
    <security-constraint>
     <web-resource-collection>
       <web-resource-name>Resources</web-resource-name>
       <url-pattern>/resources/*</url-pattern>
     </web-resource-collection>
     <auth-constraint>
        <role-name>user</role-name>
     </auth-constraint>
     <user-data-constraint>
       <transport-guarantee>CONFIDENTIAL</transport-guarantee>
     </user-data-constraint>
    </security-constraint>
    
    <login-config>
    <auth-method>KEYCLOAK</auth-method>
    </login-config>
    
    <security-role>
      <role-name>user</role-name>
    </security-role>
    

    我将用户角色添加到keydrope

    我有一个客户端程序,它接收一个用户和密码,并从keydepot安装中获取令牌

     AuthzClient authzClient = AuthzClient.create();            
     AccessTokenResponse response = authzClient.obtainAccessToken(name, password);
    
     String tokenStr = response.getToken();
    

    然后我试着用这个令牌来对用key斗篷保护的战争进行一个REST调用:

      String urlString = "http://localhost:8080/simple-rest-0.0.1-SNAPSHOT/resources/message";
      URL url = new URL(urlString); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection();
      con.setRequestMethod("GET");
      String authString = "Bearer " + tokenStr;
      con.setRequestProperty("Authorization", authString);
      basicStatus = con.getResponseCode();
    

    basicStatus返回404。我是不是用错了keydeap?我错过什么了吗?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Daniel Chicken Man    5 年前

    我也遇到了类似的问题,得到了404(起初很奇怪,因为我的浏览器可以验证端点,但我的代码/curl/postman都没有)。

    在我的例子中,我以为我得到的是不记名代币,但实际上我得到的字符串是其他东西。承载令牌应该很长(并且可以通过其他方法进行验证/比较)。


    发布到: <host>/auth/realms/<realm>/protocol/openid-connect/token
    添加标题内容类型:application/x-www-form-urlencoded

    • 客户端id
    • 用户名

    我运行的代码正在检索会话\状态(比我认为的要短得多,而不是承载令牌)。出于某种原因,当提供不正确的承载令牌(甚至过期的令牌)时,我的所有端点都解析了404(而不是任何其他远程有用的代码)。