java.lang.SecurityManager.checkLink(Stringlib)方法实例

Git ww9992019com 发表于 1 周前

<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="4858">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

<strong>java.lang.SecurityManager.checkLink(String lib) </strong>方法抛出一个SecurityException如果调用线程不允许动态链接由字符串参数文件指定的库代码。该参数可以是一个简单的库名或完整的文件名。被调用的方法,由load方法和Runtime类调用LoadLibrary当前的安全管理器。</p>

<p>

此方法调用checkPermission与RuntimePermission("loadLibrary."+lib) 权限。如果重写此方法,那么调用super.checkLink 重载方法通常会抛出一个异常。</p>

<h2>

声明</h2>

<p>

以下是<strong>java.lang.SecurityManager.checkLink()</strong>方法的声明</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> checkLink</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> lib</span><span class="pun">)</span></pre>

<h2>

参数</h2>

<ul>

<li>

    <p>

        lib --库的名称。</p>

</li>

</ul>

<h2>

返回值</h2>

<p>

此方法没有返回值。</p>

<h2>

异常</h2>

<ul>

<li>

    <p>

        SecurityException -- 如果调用线程没有权限来链接动态库。</p>

</li>

<li>

    <p>

        NullPointerException -- 如果lib参数为null。</p>

</li>

</ul>

<h2>

例子</h2>

<p>

我们的示例中,需要为每个命令的权限被阻止。一项新的规则文件设置,仅允许创建和安全管理器的设置。该文件位于C:/java.policy,包含以下文字:</p>

<pre class="prettyprint prettyprinted">

<span class="pln">grant </span><span class="pun">{</span><span class="pln">

permission java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">RuntimePermission</span><span class="pln"> </span><span class="str">"setSecurityManager"</span><span class="pun">;</span><span class="pln">

permission java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">RuntimePermission</span><span class="pln"> </span><span class="str">"createSecurityManager"</span><span class="pun">;</span><span class="pln">

permission java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">RuntimePermission</span><span class="pln"> </span><span class="str">"usePolicy"</span><span class="pun">;</span><span class="pln">

</span><span class="pun">};</span></pre>

<p>

下面的例子显示lang.SecurityManager.checkLink()方法的使用。</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">package</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">yiibai</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">SecurityManagerDemo</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

  </span><span class="com">// set the policy file as the system securuty policy</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="pln">setProperty</span><span class="pun">(</span><span class="str">"java.security.policy"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"file:/C:/java.policy"</span><span class="pun">);</span><span class="pln">



  </span><span class="com">// create a security manager</span><span class="pln">

  </span><span class="typ">SecurityManager</span><span class="pln"> sm </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">SecurityManager</span><span class="pun">();</span><span class="pln">



  </span><span class="com">// set the system security manager</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="pln">setSecurityManager</span><span class="pun">(</span><span class="pln">sm</span><span class="pun">);</span><span class="pln">



  </span><span class="com">// perform the check</span><span class="pln">

  sm</span><span class="pun">.</span><span class="pln">checkLink</span><span class="pun">(</span><span class="str">"JDBC"</span><span class="pun">);</span><span class="pln">



  </span><span class="com">// print a message if we passed the check</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Allowed!"</span><span class="pun">);</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span></pre>

<p>

让我们来编译和运行上面的程序,这将产生以下结果:</p>

<pre class="prettyprint prettyprinted">

<span class="typ">Exception</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> thread </span><span class="str">"main"</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">security</span><span class="pun">.</span><span class="typ">AccessControlException</span><span class="pun">:</span><span class="pln"> access denied </span><span class="pun">(</span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">RuntimePermission</span><span class="pln"> loadLibrary</span><span class="pun">.</span><span class="pln">JDBC</span><span class="pun">)</span></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="java.lang.SecurityManager.checkExit()方法实例"><i class="layui-icon"></i>
                            java.lang.SecurityManager.checkExit()方法实例</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:java.lang.SecurityManager.checkListen()方法实例"><i class="layui-icon"></i>
                            java.lang.SecurityManager.checkListen()方法实例</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复