⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 passwordsecuritymanager.java

📁 jdbc书
💻 JAVA
字号:
import java.io.*;
import java.security.AccessController;

public class PasswordSecurityManager extends SecurityManager {

    private String password;
    private BufferedReader buffy;

    public PasswordSecurityManager(String p, BufferedReader b) {
        super();
        this.password = p;
        this.buffy = b;
    }

    private boolean accessOK() {
        int c;
        String response;

        System.out.println("What's the secret password?");
        try {
            response = buffy.readLine();
            if (response.equals(password))
                return true;
            else
                return false;
        } catch (IOException e) {
            return false;
        }
    }

    public void checkRead(String filename) {
//Mention file by name so don't get prompted for password 
//for everything the application loads to create itself

        if((filename.equals(File.separatorChar + 
			"home" + File.separatorChar + 
			"monicap" + File.separatorChar + "text2.txt"))){
         if(!accessOK()){
            super.checkRead(filename);
            throw new SecurityException("No Way!");
         } else {
            FilePermission perm = new FilePermission(File.separatorChar + 
				"home" + File.separatorChar + 
				"monicap" + File.separatorChar + 
				"text2.txt", "read");
            checkPermission(perm);
         }
        }
    }

    public void checkWrite(String filename) {
//Mention file by name so don't get prompted for password 
//for everything the application loads to create itself
        if((filename.equals(File.separatorChar + "home" + 
				File.separatorChar + "monicap"
+ File.separatorChar + "text.txt"))){
         if(!accessOK()){
            super.checkWrite(filename);
            throw new SecurityException("No Way!");
	 } else {
            FilePermission perm = new FilePermission(File.separatorChar + 
				"home" + File.separatorChar + "monicap" + 
				File.separatorChar + "text.txt" , "write");
	    checkPermission(perm);
         }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -