欢迎使用CSDN-markdown编辑器

以前没有关注java的里面的SecurityManager这个类,最近看动态代理顺便记录下此类的使用。

其实很多时候都用到SecurityManager,只是平时没太关注而已,FileInputStream的构造方法:

public FileInputStream(File file) throws FileNotFoundException {
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException(“Invalid file path”);
}
fd = new FileDescriptor();
fd.incrementAndGetUseCount();
this.path = name;
open(name);
}

就有SecurityManager的用武之地。借用这个例子,有以下测试类:

import java.io.*;

/**
* Created by
*/
public class SecurityManageTest {

public static void main(String args[]) {
    try {
        System.setSecurityManager(new PasswordSecurityManager("a"));
        System.setSecurityManager(new PasswordSecurityManager("a"));
    } catch (SecurityException se) {
        System.err.println("Exception already set!");
    }
    try {
        System.out.println("start1");
        BufferedReader fis = new BufferedReader(new FileReader("test.txt"));
        System.out.println("start2");
        BufferedWriter fos = new BufferedWriter(new FileWriter("test.txt"));
        System.out.println("start3");
        String inputString;
        while ((inputString = fis.readLine()) != null) {
            fos.write(inputString);
            fos.write('\n');
        }
        fis.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private static class PasswordSecurityManager extends SecurityManager {
    private String password;

    PasswordSecurityManager(String password) {
        super();
        this.password = password;
    }

    private boolean accessOK() {
        int c;
        BufferedReader dis = new BufferedReader(new InputStreamReader(System.in));
        String response;
        System.out.println("What's the secret password?");
        try {
            response = dis.readLine();
            if (response.equals(password))
                return true;
            else
                return false;
        } catch (IOException e) {
            return false;
        }
    }

    public void checkRead(FileDescriptor filedescriptor) {
        if (!accessOK())
            throw new SecurityException("no read permission!");
    }

    public void checkRead(String filename) {
        if (!accessOK())
            throw new SecurityException("no read permission!");
    }

    public void checkRead(String filename, Object executionContext) {
        if (!accessOK())
            throw new SecurityException("no read permission!");
    }

    public void checkWrite(FileDescriptor filedescriptor) {
        if (!accessOK())
            throw new SecurityException("no write permission!");
    }

    public void checkWrite(String filename) {
        if (!accessOK())
            throw new SecurityException("has no write permission!");
    }
}

}

运行结果如下:
start1
What’s the secret password?
a
start2
What’s the secret password?
a
start3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值