无法从WebLogic配置检索密码

无法从WebLogic配置检索密码,weblogic,weblogic12c,Weblogic,Weblogic12c,我在WebLogic 12.2.1上部署的web服务中有以下代码。它将从WebLogic配置中检索密钥库文件名和密码 InitialContext ic = new InitialContext(); MBeanServer server = (MBeanServer) ic.lookup("java:comp/env/jmx/runtime"); ObjectName runtime = new ObjectName("com.bea:Name=MLMAppSrv01,

我在WebLogic 12.2.1上部署的web服务中有以下代码。它将从WebLogic配置中检索密钥库文件名和密码

    InitialContext ic = new InitialContext();
    MBeanServer server = (MBeanServer) ic.lookup("java:comp/env/jmx/runtime");
    ObjectName runtime = new ObjectName("com.bea:Name=MLMAppSrv01,Type=Server");
    Object keyStoreFileName = server.getAttribute(runtime, "CustomIdentityKeyStoreFileName");
    Object keyStorePassPhrase = server.getAttribute(runtime, "CustomIdentityKeyStorePassPhrase");
它能够检索密钥库文件名,但当它尝试检索密码时,会引发以下异常

[Management:141302]Access not allowed for Subject: principals=[], on resource Server, action: read, target CustomIdentityKeyStorePassPhrase.
在域的安全性下,我已经启用了“已启用明文凭据访问”

还有什么不对劲


提前感谢。

您的代码中没有传递任何用户名。对于weblogic用户或其他管理员用户,您应该检索密码。否则,它将不允许您访问密码


如果要使用weblogic以外的其他用户,请确保将该用户添加到管理员组。

您可以传递以下示例代码中给出的凭据

Hashtable properties = new Hashtable();
   properties.put(Context.INITIAL_CONTEXT_FACTORY,
                  "weblogic.jndi.WLInitialContextFactory");
   // NOTE: The port number of the server is provided in the next line,
   //       followed by the userid and password on the next two lines.
   properties.put(Context.PROVIDER_URL, "t3://localhost:9001");
   properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
   properties.put(Context.SECURITY_CREDENTIALS, "welcome1");
   try {
       ctx = new InitialContext(properties);
   } catch (NamingException ne) {
       ne.printStackTrace(System.err);
       System.exit(0);
   }

我认为您没有经过身份验证,因此无法访问受限资源。将注释@RunAs(“WEBLOGIC”)添加到类中,并在WEB-INF/WEBLOGIC-ejb-jar.xml中对其进行配置

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-ejb-jar>
   <run-as-role-assignment>
     <role-name>WEBLOGIC</role-name>
     <run-as-principal-name>weblogic</run-as-principal-name>
   </run-as-role-assignment>
</weblogic-ejb-jar>
(其中WEBLOGIC是在WEBLOGIC控制台中具有管理员权限的用户的名称,默认管理员帐户甚至命名为WEBLOGIC)

您的类应该如下所示:

@Stateless
@RunAs("WEBLOGIC")
public class SomeService {
// ...
}
weblogic-ejb-jar.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-ejb-jar>
   <run-as-role-assignment>
     <role-name>WEBLOGIC</role-name>
     <run-as-principal-name>weblogic</run-as-principal-name>
   </run-as-role-assignment>
</weblogic-ejb-jar>

网络逻辑
应用服务器

我有一个类似的例子,我的jersey代码需要在WLS中以编程方式从密钥库获取密钥。不需要EJB设置。您只需要正确定义web.xml和weblogic.xml

web.xml:

<servlet>
    <servlet-name>{jersey webservice class}</servlet-name>
    <run-as>
        <role-name>admRole</role-name>
    </run-as>        
</servlet>

<security-role>
    <role-name>admRole</role-name>
</security-role>

{jersey webservice类}
广告角色
广告角色
weblogic.xml:

<run-as-role-assignment>
    <role-name>admRole</role-name>
    <run-as-principal-name>wlsadm</run-as-principal-name>
</run-as-role-assignment> 

广告角色
wlsadm

WebLogic服务器作为一项服务启动,使用具有管理员权限的Windows登录ID(我们将此ID称为“X”)。为了登录WebLogic控制台,我使用了一个ID(我们称这个ID为“Y”)。我需要在代码“Y”中传递的用户名是否与我可以登录WebLogic控制台的ID相同?如何在我的代码中这样做?我需要访问代码中的密钥库,因此我需要获取密码。我没有在代码中硬编码密码,而是考虑从Weblogic配置中检索密码。现在您的建议是在代码中硬编码weblogic密码。这似乎并没有解决我不硬编码密码的问题。您是否正在尝试检索密钥库或Weblogic域的密码?我正在尝试使用代码检索密钥库密码。但要这样做,我现在需要检索weblogic密码。密码中不允许硬编码密码。您正在代码中对weblogic密码进行硬编码。在执行或从用户处读取密码时,请将其作为参数传递,而不是硬编码密码。谢谢您的时间。让我解释一下。我的代码必须在web服务的生产环境中工作。所有密码都是安全存储的(例如密钥库),它们对程序来说都是未知的。没有用户的干预,也不可能,因为我的代码在web服务中工作。如果我能够输入密码,我首先会输入密钥库密码。如果我的weblogic用户名是“wlsadm”,那么我应该输入什么?我应该为@RunAs指定“wlsadm”还是WEBLOGIC?那么WebLogicEJBjar.xml呢?只需将“weblogic”替换为wlsadm即可(所有这些都是为了便于阅读)。weblogic-ejb-jar.xml应该在您的WEB-INF文件夹中,如果不创建它的话。应用程序只需要知道要使用哪个角色。因为您使用的是WebService,所以您有一个Web应用程序,对吗?我今天终于尝试了您的解决方案,但仍然不起作用。我的weblogic管理ID是“wlsysad”。我按照您的建议将@RunAs(“wlsysad”)放在web服务类的顶部。然后,我创建了WEB-INF\weblogic-ejb-jar.xml,内容如您所建议的。对于和,我将两个值都设置为“wlsysad”。实际上有两个@RunAs注释。一个来自javax.annotation.security.RunAs。另一个来自weblogic.jws.security.RunAs,已弃用。我使用的javax.annotation.security.RunAs.javax.annotation.security.RunAs是正确的。你还收到同样的异常吗?如果是,您的bean必须是一个ejbbean。这就是为什么我的示例有@Stateless(来自javax.ejb.Stateless)注释。