【转帖】jndi简单运用之weblogic_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3911 | 回复: 0   主题: 【转帖】jndi简单运用之weblogic        下一篇 
shixisheng
注册用户
等级:上尉
经验:646
发帖:52
精华:0
注册:2013-5-27
状态:离线
发送短消息息给shixisheng 加好友    发送短消息息给shixisheng 发消息
发表于: IP:您无权察看 2013-5-28 15:24:55 | [全部帖] [楼主帖] 楼主

Main.java


Java代码  
北京联动北方科技有限公司

  1. package weblogic; 
  2. import java.util.Properties; 
  3. import javax.naming.Context; 
  4. import javax.naming.InitialContext; 
  5. public class Main { 
  6.        public static void main(String[] args) throws Exception { 
  7.              bind("object","This is a object."); 
  8.             
  9.              System.out.println(getResource("object")); 
  10.        } 
  11.        // get the resource of the specified name 
  12.        private static Object getResource(String jndiName) throws Exception{ 
  13.              Properties jndiConfig=getJndiConfig(); 
  14.              Context context = new InitialContext(jndiConfig); 
  15.              Object resource=context.lookup(jndiName); 
  16.              context.close(); 
  17.              return resource; 
  18.        } 
  19.        // bind a object to the specified name 
  20.        private static void bind(String jndiName,Object object) throws Exception{ 
  21.              Properties jndiConfig=getJndiConfig(); 
  22.              Context context = new InitialContext(jndiConfig); 
  23.              context.bind(jndiName, object); 
  24.              context.close(); 
  25.        } 
  26.        // get the configuration properties 
  27.        private static Properties getJndiConfig(){ 
  28.              String initialContextFactory="weblogic.jndi.WLInitialContextFactory"; 
  29.              String providerUrl="t3://localhost:8888"; 
  30.              String securityPrincipal="weblogic"; 
  31.              String securityCredentials="password"; 
  32.              Properties jndiConfig=new Properties(); 
  33.              jndiConfig.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 
  34.              jndiConfig.put(Context.PROVIDER_URL, providerUrl); 
  35.             // jndiConfig.put(Context.SECURITY_PRINCIPAL, securityPrincipal); 
  36.             // jndiConfig.put(Context.SECURITY_CREDENTIALS, securityCredentials); 
  37.              return jndiConfig; 
  38.        } 


    getJndiConfig()方法中提供了初始化Context的property配置。

属性名属性说明
Context.INITIAL_CONTEXT_FACTORY指定InitialContext的实现类名称。这里使用weblogic作为服务器。所以相应的值为weblogic.jndi.WLInitialContextFactory。
Context.PROVIDER_URL连接jndi服务器的url。这里weblogic运行在本机,并且端口号为8888。所以值设置为t3://localhost:8888。
Context.SECURITY_PRINCIPAL通过jndi访问指定resource的凭证名称。简单理解为访问这个resource时的用户名。如果这个resource没有设定访问策略,则可以不设。
Context.SECURITY_CREDENTIALS通过jndi访问指定resource时与凭证相对应的密码。简单理解为访问这个resource时与用户名相对应的密码。如果这个resource没有设定访问策略,则可以不设。


getJndiConfig()有以下2行注释

//jndiConfig.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
//jndiConfig.put(Context.SECURITY_CREDENTIALS, securityCredentials);


这是因为没有对新注册的resource{object=This is a object}设置访问策略。

通过以下步骤可以在weblogic10.3中对某一个resource设置访问策略。

1) 登录weblogic

北京联动北方科技有限公司
 2) 切换到指定server

北京联动北方科技有限公司
 3)打开jndi配置页面
北京联动北方科技有限公司
 4)择选新注册的resource,切换到sercurity页。

1北京联动北方科技有限公司
5) 选择Policies页,单击Add Conditions

北京联动北方科技有限公司
 6)Predicate List 选择User,单击Next

北京联动北方科技有限公司
 7)User Argument Name 填写Weblogic(这是我登录weblogic的一个用户名),点击Add按钮,点击Finish

北京联动北方科技有限公司

8)最终点击Save,会出现保存成功信息

北京联动北方科技有限公司

通过以上步骤为名称为object的resource设定了访问策略。如果通过api访问这个resoucre,

必须提供Context.SECURITY_PRINCIPAL,Context.SECURITY_CREDENTIALS。

及以上示例只需要重新启用注释的那2行代码。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论