gpt4 book ai didi

java - 如何在 Windows 7 上使用 NIO 库更改 Java 中的文件权限

转载 作者:行者123 更新时间:2023-12-01 06:09:33 28 4
gpt4 key购买 nike

我正在尝试编写以特定文件的 Path 对象开头的代码,并使该文件的所有者不再具有移动它、删除它或修改它的权限,但他们仍然可以阅读。还需要确保可以撤消此操作,并始终保持管理员不受限制的访问权限。

我的主要问题之一是我不知道如何获取系统中的用户配置文件名称和组名称。

如果有深入的解释就太好了。

最佳答案

File类可以设置文件的读取、写入和执行。如果用户要获取它,则需要使用NIO。

Files 类和 NIO 使用 PosixFilePermissions,它按文件和三个组设置权限:用户、所有者和组。在 Windows 上,管理员以及系统都位于一个组中。

要移动它,我们需要自己的 SecurityManager。当移动文件时,NIO 使用写权限。所以我们的SecurityManager得修改写权限。请参阅下面我的代码作为示例。

附:尽管 FileSystemsProvider 是 WindowsFileSystemProvider,但它是由 FileSystemProviders.getProvider (或类似的方法)返回的。对于下载的每个操作系统,rt.jar 可能会有所不同,但如果您使用的是 Windows,则可以假设这是正确的。

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);
}

}

关于java - 如何在 Windows 7 上使用 NIO 库更改 Java 中的文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37724278/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com