Die Dateiklasse kann eine Datei zum Lesen, Schreiben und Ausführen festlegen. Um es von Benutzer zu erhalten, müssen Sie NIO verwenden.
Die Files-Klasse und NIO verwendet PosixFilePermissions, die Berechtigungen nach Datei und nach drei Gruppen festlegt: Benutzer, Besitzer und Gruppe. Unter Windows befinden sich Administratoren in einer Gruppe sowie System.
Um es zu bewegen, brauchen wir unseren eigenen SecurityManager. Beim Verschieben von Dateien verwendet NIO die Schreibberechtigung. Daher muss unser SecurityManager die Schreibberechtigung ändern. Siehe meinen Code unten als ein Beispiel.
p.s. Obwohl der FileSystemsProvider WindowsFileSystemProvider ist, wird dies von FileSystemProviders.getProvider (oder der Methode ähnlich) zurückgegeben. Es ist möglich, dass rt.jar für jedes heruntergeladene Betriebssystem unterschiedlich ist, aber wenn Sie Windows verwenden, können Sie davon ausgehen, dass dies korrekt ist.
PathRestrictor.java
package Testers;
import java.io.IOException;
import java.net.URI;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.AccessMode;
import java.nio.file.CopyOption;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.spi.FileSystemProvider;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import sun.nio.fs.WindowsFileSystemProvider;
public class PathRestrictor extends FileSystemProvider{
boolean canRead;
boolean canWrite;
boolean canMove; //This is the tricky one
boolean canOpen;
private Path path;
WindowsFileSystemProvider provider = new WindowsFileSystemProvider();
public PathRestrictor(Path p){
path = p;
canRead = true;
canWrite = true;
canOpen = true;
}
public void setExecuteable(boolean executable){
canOpen = executable;
try {
Files.setPosixFilePermissions(path, getPerms());
} catch (IOException e) {
e.printStackTrace();
}
}
public void setReadable(boolean readable){
canRead = readable;
try {
Files.setPosixFilePermissions(path, getPerms());
} catch (IOException e) {
e.printStackTrace();
}
}
public void setWriteable(boolean writeable){
canWrite = writeable;
try {
Files.setPosixFilePermissions(path, getPerms());
} catch (IOException e) {
e.printStackTrace();
}
}
public void setMoveable(boolean moveable){
canMove = moveable;
MovementSecurityManager manager = new MovementSecurityManager();
if(!moveable)manager.unMoveablePaths.add(path.toString());
else manager.unMoveablePaths.remove(path.toString());
System.setSecurityManager(manager);
}
private Set<PosixFilePermission> getPerms() {
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_WRITE);
if(canRead){
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OWNER_READ);
}
if(canWrite){
perms.add(PosixFilePermission.OTHERS_WRITE);
perms.add(PosixFilePermission.OWNER_WRITE);
}
if(canOpen){
perms.add(PosixFilePermission.OTHERS_EXECUTE);
perms.add(PosixFilePermission.OWNER_EXECUTE);
}
return perms;
}
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException {
provider.checkAccess(path, modes);
}
@Override
public void copy(Path source, Path target, CopyOption... options)
throws IOException {
// TODO Auto-generated method stub
provider.copy(source, target, options);
}
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs)
throws IOException {
provider.createDirectory(dir, attrs);
}
@Override
public void delete(Path path) throws IOException {
provider.delete(path);
}
@Override
public <V extends FileAttributeView> V getFileAttributeView(Path path,
java.lang.Class<V> type, LinkOption... options) {
return provider.getFileAttributeView(path, type, options);
}
@Override
public FileStore getFileStore(Path path) throws IOException {
return provider.getFileStore(path);
}
@Override
public FileSystem getFileSystem(URI uri) {
return provider.getFileSystem(uri);
}
@Override
public Path getPath(URI uri) {
return provider.getPath(uri);
}
@Override
public String getScheme() {
return provider.getScheme();
}
@Override
public boolean isHidden(Path path) throws IOException {
return provider.isHidden(path);
}
@Override
public boolean isSameFile(Path path, Path path2) throws IOException {
return path.toString().equals(path2.toString());
}
@Override
public void move(Path source, Path target, CopyOption... options)
throws IOException {
MovementSecurityManager manager = new MovementSecurityManager();
manager.isMoving = true;
System.setSecurityManager(manager);
provider.move(source, target, options);
}
@Override
public SeekableByteChannel newByteChannel(Path path,
Set<? extends OpenOption> options, FileAttribute<?>... attrs)
throws IOException {
return provider.newByteChannel(path, options, attrs);
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
Filter<? super Path> filter) throws IOException {
return provider.newDirectoryStream(dir, filter);
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException {
return provider.newFileSystem(uri, env);
}
@Override
public <A extends BasicFileAttributes> A readAttributes(Path path,
java.lang.Class<A> type, LinkOption... options) throws IOException {
return provider.readAttributes(path, type, options);
}
@Override
public Map<String, Object> readAttributes(Path path, String attributes,
LinkOption... options) throws IOException {
return provider.readAttributes(path, attributes, options);
}
@Override
public void setAttribute(Path path, String attribute, Object value,
LinkOption... options) throws IOException {
provider.setAttribute(path, attribute, value, options);
}
}
MovementSecurityManager.java
package Testers;
import java.util.HashSet;
import java.util.Set;
public class MovementSecurityManager extends SecurityManager {
public Set<String> unMoveablePaths = new HashSet<String>();
public boolean isMoving = true;
public void checkWrite(String path){
if(unMoveablePaths.contains(path) && isMoving) throw new SecurityException("Cannot move file!");
else super.checkWrite(path);
}
}
Können sie es kopieren? – JD9999
Ich habe tatsächlich herausgefunden, wie ich das Ergebnis bekommen könnte, das ich mehr oder weniger wollte. Die einzige Sache, die ich noch unsicher bin, ist, wenn es wirklich möglich ist, die Erlaubnis eines Benutzers zu entfernen, um eine Datei zu verschieben, und wenn es dann möglich ist, wie. – JereTheJuggler
Es ist ziemlich kompliziert, lassen Sie mich Ihnen zeigen, wie in einer Antwort. Zumindest denke ich, das funktioniert, ich bin der einzige Benutzer und ein Administrator, also kann ich es nicht testen – JD9999