检查客户端请求的权限并检入Java(套接字)中的策略文件

时间:2018-11-09 09:59:57

标签: java sockets access-control

我有5个客户端类和一个服务器类(mutilthread)。我已经通过setProperty方法设置了策略文件,但不确定如何实现。我尝试了支票允许,但对我不起作用。我有什么方法可以根据策略文件检查哪些客户端具有访问权限,哪些客户端没有访问权限?

政策文件

/* AUTOMATICALLY GENERATED ON Thu Nov 08 19:52:58 GST 2018*/
/* DO NOT EDIT */

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";

    };

例如:client1可以访问所有内容

client2只能写

客户端3只能读取

服务器代码

public class Server {

@SuppressWarnings("resource")
public static void main( String args[] ) throws IOException, InterruptedException
{
    ServerSocket socket = null;
    DataInputStream in;
    PrintStream out;
    Socket clientSocket1 = null;

    System.setProperty( "java.security.policy", "file:/C:/Users/Ali/Desktop/java.policy" );

    while ( true )
    {
        socket = new ServerSocket( 285 );
        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();

        try
        {

            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 个答案:

没有答案