java AccessController.doPrivileged

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/852453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:03:58  来源:igfitidea点击:

AccessController.doPrivileged

javasecurityapijakarta-ee

提问by Peter Kofler

I am trying to figure out what some legacy code is doing. What exactly is this line doing, and why would I need it this way?

我想弄清楚一些遗留代码在做什么。这条线到底在做什么,为什么我需要这种方式?

String lineSeparator = (String) java.security.AccessController.doPrivileged(
       new sun.security.action.GetPropertyAction("line.separator")); 

I found it in the logger implementation of the web/ejb application running on Weblogic 8. There are no special security policies enabled as far as I know. (I do not like imports from sun.* packages, so I want to get rid of this line ;-)

我在 Weblogic 8 上运行的 web/ejb 应用程序的记录器实现中发现了它。据我所知,没有启用特殊的安全策略。(我不喜欢从 sun.* 包中导入,所以我想去掉这一行;-)

回答by Tom Hawtin - tackline

It is just getting a system property. Retrieving system properties requires permissions which the calling code may not have. The doPrivilegedasserts the privileges of the calling class irrespective of how it was called. Clearly, doPrivilegedis something you need to be careful with.

它只是获取系统属性。检索系统属性需要调用代码可能没有的权限。在doPrivileged不考虑声称它是如何被称为调用类的特权。显然,这doPrivileged是您需要小心的事情。

The code quoted is the equivalent of:

引用的代码相当于:

String lineSeparator = java.security.AccessController.doPrivileged(
    new java.security.PrivilegedAction<String>() {
        public String run() {
            return System.getProperty("line.separator");
        }
    }
 );

(Don't you just love the conciseness of Java's syntax?)

(你不就是喜欢 Java 语法的简洁吗?)

Without asserting privileges, this can be rewritten as:

在不声明特权的情况下,这可以重写为:

String lineSeparator = System.getProperty("line.separator");