客户端请求限制套接字编程(本地)

时间:2018-11-11 16:31:43

标签: java sockets acl access-control accesscontrolservice

我在这里遇到了许多有关套接字编程的问题,但是它们都连接到HTTP / URL。我只有两个Java类,用于通过端口连接服务器和客户端。我面临的问题是我必须让client1读取其中不允许读取client2的文件。我正在寻找任何可能的实现方法,以便能够基于策略文件允许或删除请求。我尝试使用安全管理器方法,但未能正确实现。任何建议都会有很大帮助

我将在下面发布服务器类和策略详细信息

政策文件

grant {

permission java.io.FilePermission "D:\\Uni\\System security\\Client Server 
Example\\ClientServerExample\\src\\javaapplication2\\abc.txt", "read", 
signedBy "client1";

permission java.io.FilePermission "D:\\Uni\\System security\\Client Server 
Example\\ClientServerExample\\src\\javaapplication2\\abc.txt", "write", 
signedBy "client2";

permission java.security.AllPermission, signedBy "client1";
permission javax.security.auth.kerberos.ServicePermission "285", "initiate, 
accept", signedBy "client1";

};

服务器类

package javaapplication2;

import java.io.*;
import java.net.*;
import java.security.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

class Multi extends Thread
{
    private Socket s = null;
    DataInputStream infromClient;

    Multi() throws IOException
    {
    }

    Multi( Socket s ) throws IOException
    {
        this.s = s;
        infromClient = new DataInputStream( s.getInputStream() );
    }

    public void run()
    {
        try
        {
            System.out.println( "Socket Closing" );
            s.close();
        }
        catch ( IOException ex )
        {
            Logger.getLogger( Multi.class.getName() ).log( Level.SEVERE, null, ex );
        }
    }
}

public class Server
{
    @SuppressWarnings("resource")
    public static void main( String args[] ) throws IOException, InterruptedException
    {
        System.setProperty( "java.security.policy", "file:/C:/Users/Ali/Desktop/java.policy" );

        SecurityManager client1 = new SecurityManager();
        System.setSecurityManager( client1 );

        ServerSocket socket = null;
        DataInputStream in;
        PrintStream out;
        Socket clientSocket1 = null;

        //try {

        //System.out.println("Allowed!");
        //} 

        while ( true )
        {
            socket = new ServerSocket( 200 );
            System.out.println( "Server is Awaiting" );
            clientSocket1 = socket.accept();

            in = new DataInputStream( ( clientSocket1 ).getInputStream() );//read from client1
            BufferedReader fileRead = new BufferedReader( new InputStreamReader( in ) );
            String fname = fileRead.readLine();

            FileInputStream propFile =
                    new FileInputStream( fname );
            Properties p = new Properties( System.getProperties() );
            p.load( propFile );
            System.setProperties( p );
            // display new properties
            System.getProperties().list( System.out );

            try
            {

                AccessController.checkPermission( new FilePermission( fname, "read" ) );

                BufferedReader contentRead = new BufferedReader( new FileReader( fname ) );
                out = new PrintStream( clientSocket1.getOutputStream() );//write to client1
                PrintWriter pwrite = new PrintWriter( out, true );

                String str;
                while ( ( str = contentRead.readLine() ) != null )
                {
                    pwrite.println( str );         // sending each line to client
                }

            }
            catch ( SecurityException e )
            {
            }
            Multi t = new Multi( clientSocket1 );
            t.start();
            Thread.sleep( 2000 );
            socket.close();
        }
    }
}

0 个答案:

没有答案