Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- System.setSecurityManager()
- @Override
- public void checkPermission(java.security.Permission perm) {
- if (perm.getName().equals("shutdownHooks")) {
- if (threadContextClassLoaderIsPluginClassLoader()) {
- throw new SecurityException("Installing shutdown hooks is not allowed.");
- }
- }
- }
- class SandboxPolicy extends Policy {
- @Override
- public PermissionCollection getPermissions(ProtectionDomain domain) {
- // Decide if the plugin permissions are needed or full access can be granted using all permissions.
- if (isPlugin(domain)) {
- return pluginPermissions();
- } else {
- return applicationPermissions();
- }
- }
- private boolean isPlugin(ProtectionDomain domain) {
- return domain.getClassLoader() instanceof PluginClassLoader;
- }
- private PermissionCollection pluginPermissions() {
- // Empty permissions = No permissions
- // This is not the point to add plugin permissions
- return new Permissions();
- }
- private PermissionCollection applicationPermissions() {
- // Grant full access to the application
- Permissions permissions = new Permissions();
- permissions.add(new AllPermission());
- return permissions;
- }
- }
- @Override
- protected PermissionCollection getPermissions(CodeSource codeSource)
- {
- PermissionCollection pc;
- // The SecureClassloader per default grants access to read resources from the source JAR.
- // This is useful. Call super to get those permissions:
- pc = super.getPermissions(codeSource);
- // At this point you can extend permissions.
- // For example grant read access to a file.
- pc.add(new FilePermission("path\file", "read"));
- return (pc);
- }
Advertisement
Add Comment
Please, Sign In to add comment