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

📄 secureservice.java

📁 java 的164个例子的源代码,包括了 很多方面,供包括了19个方面的 源代码
💻 JAVA
字号:
/* * Copyright (c) 2000 David Flanagan.  All rights reserved. * This code is from the book Java Examples in a Nutshell, 2nd Edition. * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. * You may study, use, and modify it for any non-commercial purpose. * You may distribute it non-commercially as long as you retain this notice. * For a commercial use license, or to purchase the book (recommended), * visit http://www.davidflanagan.com/javaexamples2. */import com.davidflanagan.examples.net.*;  // Note no package statement here.import java.io.*;/** * This is a demonstration service.  It attempts to do things that may * or may not be allowed by the security policy and reports the * results of its attempts to the client. **/public class SecureService implements Server.Service {    public void serve(InputStream i, OutputStream o) throws IOException {	PrintWriter out = new PrintWriter(o);	// Try to install our own security manager.  If we can do this,	// we can defeat any access control.	out.println("Trying to create and install a security manager...");	try {	    System.setSecurityManager(new SecurityManager());	    out.println("Success!");	}	catch (Exception e) { out.println("Failed: " + e); }	// Try to make the Server and the Java VM exit.	// This is a denial of service attack, and it should not succeed!        out.println();	out.println("Trying to exit...");	try { System.exit(-1); }	catch (Exception e) { out.println("Failed: " + e); }	// The default system policy allows this property to be read        out.println();	out.println("Attempting to find java version...");	try { out.println(System.getProperty("java.version")); }	catch (Exception e) { out.println("Failed: " + e); }	// The default system policy does not allow this property to be read	out.println();	out.println("Attempting to find home directory...");	try { out.println(System.getProperty("user.home")); }	catch (Exception e) { out.println("Failed: " + e); }	// Our custom policy explicitly allows this property to be read	out.println();	out.println("Attempting to read service.tmp property...");	try {	    String tmpdir = System.getProperty("service.tmp");	    out.println(tmpdir);	    File dir = new File(tmpdir);	    File f = new File(dir, "testfile");	    // Check whether we've been given permission to write files to	    // the tmpdir directory	    out.println();	    out.println("Attempting to write a file in " + tmpdir + "...");	    try {		new FileOutputStream(f);		out.println("Opened file for writing: " + f);	    }	    catch (Exception e) { out.println("Failed: " + e); }	    // Check whether we've been given permission to read files from	    // the tmpdir directory	    out.println();	    out.println("Attempting to read from " + tmpdir + "...");	    try {		FileReader in = new FileReader(f);		out.println("Opened file for reading: " + f);	    }	    catch (Exception e) { out.println("Failed: " + e); }	}	catch (Exception e) { out.println("Failed: " + e); }	// Close the Service sockets	out.close();	i.close();    }}

⌨️ 快捷键说明

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