Secure Service : Service « Security « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Security » Service 




Secure Service


/*
 * 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 java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import com.sun.corba.se.spi.activation.Server;

/**
 * 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 othrows 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();
  }
}

/*

//Server.policy

//These lines grant permissions to any code loaded from the directory shown.
//Edit the directory to match the installation on your system.
//On Windows systems, change the forward slashes to double backslashes: "\\".
grant codeBase "file:/home/david/Books/JavaExamples2/Examples" {
   // Allow the server to listen for and accept network connections
   // from any host on any port > 1024
   permission java.net.SocketPermission "*:1024-", "listen,accept";
};

*/
          
       














Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.