# 前言

简单了解 SecurityManager。具体查阅 API。

# What

它是 Java 沙盒模型控制安全的重要一个环节。它是 Java 的一个类。下面一段话源于SecurityManager API:

The security manager is a class that allows applications to implement a security policy. It allows an application to determine, before performing a possibly unsafe or sensitive operation, what the operation is and whether it is being attempted in a security context that allows the operation to be performed. The application can allow or disallow the operation.

安全管理器是一个类,允许应用实现一个安全策略。在执行一项可能不安全或者敏感操作之前,它允许应用去确定这个操作的内容以及这个操作是否允许去修改安全上下文。这个应用可也允许或者阻止该行为。

简单的说,他规定了权限,类访问权限,文件访问权限等等,范围很广,你能想到的,它都有。只要在命令行运行时附加参数,或者在程序运行前设置该类即可。

# Why

至于使用这个类作为安全机制的原因:Java 原生安全机制,对于简单应用,为什么不用?

至于使用场合,比如恶意的反射:

假设有个安全控制类:

public class Security {

    boolean security = true;

// 我是 private 方法,我不想被外人访问!
private void changeSecurity() {
security = false;
} public boolean getSecurity() {
return security;
} }

然后黑客想要修改安全:

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random; public class Main { public static void main(String[] args) {
Class securityClass = Security.class;
try {
Security security = (Security) securityClass.newInstance();
Method method = securityClass.getDeclaredMethod("changeSecurity");
method.setAccessible(true); // 让我访问 private 方法
System.out.println("Security level: " + security.getSecurity());
method.invoke(security);
System.out.println("Security level: " + security.getSecurity());
} catch (Exception e) {
e.printStackTrace();
}
} }

这是很邪恶的,一下子就把安全选项给关闭了。

# How

然而阻止黑客这么做很简单,附加 SecurityManager 禁用反射即可:

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random; public class Main { public static void main(String[] args) {
System.setSecurityManager(new SecurityManager()); // 设置了 SecurityManager
Class securityClass = Security.class;
try {
Security security = (Security) securityClass.newInstance();
Method method = securityClass.getDeclaredMethod("changeSecurity");
method.setAccessible(true);
System.out.println("Security level: " + security.getSecurity());
method.invoke(security);
System.out.println("Security level: " + security.getSecurity());
} catch (Exception e) {
e.printStackTrace();
}
}
}

# Reference

http://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html SecurityManager API

Java Class SecurityManager的更多相关文章

  1. java.lang.SecurityManager、java.security包

    学习java大概3年多了,一直没有好好研究过java安全相关的问题,总是会看到 SecurityManger sm = System.getSecurityManager(); if(sm!=null ...

  2. java安全管理器SecurityManager入门

    table { margin-left: 30px; width: 95%; border: 1px; border-collapse: collapse } img { border: 1px so ...

  3. Using Java SecurityManager to grant/deny access to system functions

    In Java it is possible to restrict access to specific functions like reading/writing files and syste ...

  4. java基础系列--SecurityManager入门(转)

    转载作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/yiwangzhibujian/p/6207212.html 一.文章的目的 这是一篇对Java安全管理器入门的文 ...

  5. Java安全管理器——SecurityManager

    总的来说,Java安全应该包括两方面的内容,一是Java平台(即是Java运行环境)的安全性:二是Java语言开发的应用程序的安全性.由于我们不是Java本身语言的制定开发者,所以第一个安全性不需要我 ...

  6. java线程基础知识----SecurityManager类详解

    在查看java Thread源码的时候发现一个类----securityManager,虽然很早就知道存在这样一个类但是都没有深究,今天查看了它的api和源码,发现这个类功能强大,可以做很多权限控制策 ...

  7. java安全管理器SecurityManager

    本文转载自java安全管理器SecurityManager 导语 这是一篇对Java安全管理器入门的文章,目的是简单了解什么是SecurityManager,对管理器进行简单配置,解决简单问题. 比如 ...

  8. Java基础知识【上】(转载)

    http://blog.csdn.net/silentbalanceyh/article/details/4608272 (最终还是决定重新写一份Java基础相关的内容,原来因为在写这一个章节的时候没 ...

  9. JAVA安全模型

    作为一种诞生于互联网兴起时代的语言,Java 从一开始就带有安全上的考虑,如何保证通过互联网下载到本地的 Java 程序是安全的,如何对 Java 程序访问本地资源权限进行有限授权,这些安全角度的考虑 ...

随机推荐

  1. [Linux] Use find to search for filename patterns

    Learn how to use find to identify filenames matching specified patterns. We'll use find to identify ...

  2. 检测dll是32/64位?(直接读dll文件包含的某几个字节进行判断)

    检查dll是32位还是64位? #include "stdafx.h" #include <Windows.h> int _tmain(int argc, _TCHAR ...

  3. C/C++ 笔试、面试题目大汇总2

    http://www.cnblogs.com/fangyukuan/archive/2010/09/18/1830493.html 一.找错题 试题1: void test1() { charstri ...

  4. 【hdu 3389】Game

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  5. 【SAE 部署 JavaWeb 项目报 404 错误】

    个人学习整理,如有不足之处,请不吝不吝赐教.转载请注明:@CSU-Max 今天写了一个小的 JavaWeb 项目传到 SAE 上.訪问的时候出错. 本地測试是正常的,并且曾经做微信平台开发的时候上传的 ...

  6. Procedural graphics architectures and techniques

    BACKGROUND The evolution of graphics rendering technology has led to the development of procedural t ...

  7. GANs(生成对抗网络)初步

    Image Completion with Deep Learning in TensorFlow 1. 基本思路 首先定义一个简单的.常见的概率分布,将其表示为 pz,不妨将其作为 [-1, 1] ...

  8. Methods for Using Message Queuing Telemetry Transport for Sensor Networks to Support Sleeping Devices

    Methods support a sleep mode for an embedded device. Embedded devices like sensors and actuators use ...

  9. POJ3280 Cheapest Palindrome 【DP】

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6013   Accepted: 29 ...

  10. Android 光标位置设置

    EditText edit =(EditText) findViewById(R.id.etTest); 1.设置光标在EditText中的指定位置 edit.setSelection(1); 需要注 ...