Java SecurityManager checkPermission()方法與示例

Syntax:

句法:

    public void checkPermission(Permission  perm);
public void checkPermission(Permission  perm, Object cntxt);

SecurityManager類的checkPermission()方法 (SecurityManager Class checkPermission() method)

  • checkPermission() method is available in java.lang package.

    checkPermission()方法在java.lang包中可用。

  • checkPermission(Permission perm) method invokes checkPermission of AccesController for the requested access, indicated by the specified permissions.

    checkPermission(Permission perm)方法為請求的訪問調用AccesController的checkPermission,由指定的權限指示。

  • checkPermission(Permission perm, Object cntxt) method invokes checkPermission of AccesControlContext for the given security context is the access granted to the resource, indicated by the specified permissions when cntxt is an instance of AccessControlContext.

    當給定的安全上下文是授予資源的訪問權限時,checkPermission(Permission perm,Object cntxt)方法調用AccesControlContext的checkPermission,當cntxt是AccessControlContext的實例時,由指定的權限表示。

  • checkPermission(Permission perm), checkPermission(Permission perm, Object cntxt) methods may throw an exception at the time of granting permission.

    checkPermission(Permission perm)checkPermission(Permission perm,Object cntxt)方法在授予權限時可能會引發異常。

  • checkPermission(Permission perm):

    checkPermission(權限燙發):

    • SecurityException – This exception may throw when the access is denied on the security policy held currently.
    • NullPointerException – This exception may throw when the given parameter is null.
  • checkPermission(Permission perm, Object cntxt):

    checkPermission(權限權限,對象cntxt):

    • SecurityException – This exception may throw when the calling thread is not allowed to access the resource by the given permission or when the security cntxt(context) is not an object of AccessControlContext.
    • NullPointerException – This exception may throw when the given first parameter is null.
  • These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.

    這些是非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問這些方法,則會收到錯誤消息。

Parameter(s):

參數:

  • In the first case, Permission perm - This parameter represents the requested permission.

    在第一種情況下, 權限權限 -此參數表示請求的權限。

  • In the second case, Permission perm, Object cntxt

    在第二種情況下, 權限權限,對象cntxt

    • Permission perm – Similar as defined in the first case.
    • 許可權限 –與第一種情況中定義的相似。
    • Object cntxt – This parameter represents the system-specific security context.
    • 對象cntxt –此參數表示系統特定的安全上下文。

Return value:

返回值:

The return type of this method is void, it returns nothing.

此方法的返回類型為void ,不返回任何內容。

Example:

例:

// Java program to demonstrate the example 
// of checkPermission() method of SecurityManager class
import java.security.*;
import java.io.*;
public class CheckPermission extends SecurityManager {
public static void main(String[] args) {
Permission perm = new FilePermission("getProperties().doc", "read,write");
AccessControlContext acc = AccessController.getContext();
// By using setProperty() method is to set the policy property 
// with security manager
System.setProperty("java.security.policy", "file:/C:/java.policy");
// Instantiating a CheckPermission object
CheckPermission cp = new CheckPermission();
// By using setSecurityManager() method is to set the
// security manager
System.setSecurityManager(cp);
// By using checkPermission(Permission) method is to
// check that restricted permission
cp.checkPermission(perm);
// By using checkPermission(Permission,Object) method is to
// check that restricted permission when cntxt is an instance
// of AccessControlContext
cp.checkPermission(perm, acc);
// Display the message 
System.out.println("Accepted..");
}
}

Output

輸出量

Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "getProperties().doc" "read,write")at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)at java.base/java.security.AccessController.checkPermission(AccessController.java:897)at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)at CheckPermission.main(CheckPermission.java:25)

翻譯自: https://www.includehelp.com/java/securitymanager-checkpermission-method-with-example.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/380010.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/380010.shtml
英文地址,請注明出處:http://en.pswp.cn/news/380010.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

匯編test指令

功能:將兩個操作數進行邏輯與運算,并根據運算結果設置相關的標志位,并不改變操作數1和操作數2的值 test 操作數1,操作數2我們經常用test來判斷一個值是否為0,用法: test 操作數1,操作數1比如我…

CSS兼容IE/Firefox要點

首先我們說說firefox和IE對CSS的寬度顯示有什么不同: 其實CSS ’width’ 指的是標準CSS中所指的width的寬度,在firefox中的寬度就是這個寬度。它只包含容器中內容的寬度。而Internet Explorer ’width’則是指整個容器的寬度,包括內容&#x…

Java GregorianCalendar computeFields()方法與示例

GregorianCalendar類computeFields()方法 (GregorianCalendar Class computeFields() method) computeFields() method is available in java.util package. 在java.util包中提供了validateFields()方法 。 computeFields() method is used to compute the calendar fields and…

JS、JNS、JP(JPE)、JNP(JPO)指令詳解、從原理上解釋

JS 格式: js 地址當執行到JS指令時,如果標志位SF1,則跳轉到指定的地址,如果SF0,不跳轉 比如: cmp eax,ecx js 0040100c此時eax0,ecx1,執行完cmp命令,符號標…

zz如何保持專心

養成好習慣 養成在固定時間、固定地點專心學習工作的好習慣。 如果可能,在進入學習或者工作狀態前做一些小儀式,比如擺個姿勢,戴上學習帽什么的。就好像在運動前做準備活動一樣,給身體一個提示。讓頭腦做好準備 避免在學習前做什么…

Java——File類

一,File類的概述和構造方法 A:file類的概述 file類可以理解成一個路徑 文件夾或者是文件夾路徑 路徑分為絕對路徑和相對路徑 絕對路徑是一個固定的路徑,從盤符開始 這里的G:\TIM 就是一個絕對路徑,是一個固定的路…

Linux進程環境

一 main函數 當內核使用一個exec函數執行C程序時,在調用main函數之前先調用一個特殊的啟動例程,可執行程序將此例程指定為程序的起始地址。啟動例程從內核獲取命令行參數和環境變量,然后為調用main函數做好準備。 二 進程終止 進程終止的方式…

JO、JNO、JB、JNB命令詳解(從原理上)

JO 當執行到jo命令時,如果ZF標志位為1,則跳轉,反之不跳轉 add eax,ecx jo 00401000c此時eax7fff ffff ,ecx0000 0001,執行完add命令,OF1,原因是eax存儲的最大值是7fffffff,再加1&a…

java 根據類名示例化類_Java類類getProtectionDomain()方法及示例

java 根據類名示例化類類class getProtectionDomain()方法 (Class class getProtectionDomain() method) getProtectionDomain() method is available in java.lang package. getProtectionDomain()方法在java.lang包中可用。 getProtectionDomain() method is used to return …

snagit 9.0注冊碼

8.0的注冊碼 A5CCU-RYNM4-C9ECC-5CWW9-B5R7B 5HCC5-4CCC9-NGXCM-XYDZ5-H6ER6 HLHAD-2CZLC-8XYDC-CC5CB-P289A D5DSC-WZCBM-JRHSC-QVTEV-TR7R8 snagit 9.0: name:Team Z.W.T sn:XMYU5-9CMBC-5SLBZ-DKML2-JE8M5 謝謝 name:Team Z.W.T sn: WDYMP-8ALRM-GVVV2-PH8VK-6MD27 Z…

vue3 配置 @符號

config,ts 配置 有 爆紅 安裝 npm install 一下 然后 配置 路徑提示功能 tsconfig.json 配置 路徑提示功能 一共這兩個路徑配置

android 頁面轉換

通過setContentView來改變布局 很簡單 ViewchangeActivity.java package idrc.change;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class ViewchangeActivity extends Activity {/** Called when the…

mts模式_MTS的完整形式是什么?

mts模式MTS:Microsoft Transaction Server /移動電話服務/多通道電視聲音 (MTS: Microsoft Transaction Server/ Mobile Telephone Service/ Multichannel Television Sound) 1)MTS:Microsoft Transaction Server (1) MTS: Microsoft Transaction Server…

Java——IO流

一,IO流常識 Ⅰ,IO流的概述 1,IO流用來處理設備之間的數據傳輸 2,Java對數據的操作都是通過流的方式 3,Java用于操作流的類都在IO包中 4,流 按流向分為: ①輸入流 ②輸出流 5&#xff0…

JBE、JNBE、JA、JL指令詳解(從原理上)

JBE 當執行到JBE命令時,如果此時的CF標志位或者ZF標志位為1,跳轉,其他不跳轉 相當于小于等于命令 cmp eax,ecx jbe 0040100c執行到cmp命令時,如果此時的eac小于等于ecx,jbe都會跳轉到0040100c 因為小于的…

數組shift方法_數組shift()方法以及JavaScript中的示例

數組shift方法JavaScript shift()方法 (JavaScript shift() method) shift() method is used to remove the first element of an array and returns the deleted element. shift()方法用于刪除數組的第一個元素,并返回刪除的元素。 It changes the array length. …

ArcMap 9使用技巧

ArcMap 9使用技巧技巧1 重疊要素的選擇切換目標:在覆蓋同一區域的多個要素中切換被選擇要素操作步驟:1.在編輯環境中選中覆蓋同一區域的多個要素中的一個要素2.按下N 鍵,被選要素就會自動地切換到另一個圖層的要素3&am…

《軟件》2011年第6期刊登出 《DB 查詢分析器》中斷SQL語句的執行

《軟件》編輯部寄來了2011年第6期樣刊,在2011年第6期,刊登出了本人的論文------“《DB 查詢分析器》中斷SQL語句的執行”。 論文刊登在第42頁,排在第13篇,還比較靠前,呵呵。 在“萬方數據”和“中國期刊全文數據庫”中…

Java——IO流(序列流)

序列化與反序列化 游戲存檔:目的是為了游戲下次上號數據的保存 package com.yy.otherio;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; im…

ret2libc過地址隨機化

程序&#xff1a; #include<stdio.h> char buf2[10] "this is buf2"; void vul() {char buf1[10];gets(buf1); } void main() {write(1,"sinxx",5);vul(); }很明顯&#xff0c;gets函數存在溢出 編譯&#xff1a; gcc -no-pie -fno-stack-protect…