Guest User

Untitled

a guest
Oct 18th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. System.setSecurityManager()
  2.  
  3. @Override
  4. public void checkPermission(java.security.Permission perm) {
  5. if (perm.getName().equals("shutdownHooks")) {
  6. if (threadContextClassLoaderIsPluginClassLoader()) {
  7. throw new SecurityException("Installing shutdown hooks is not allowed.");
  8. }
  9. }
  10. }
  11.  
  12. class SandboxPolicy extends Policy {
  13. @Override
  14.  
  15. public PermissionCollection getPermissions(ProtectionDomain domain) {
  16. // Decide if the plugin permissions are needed or full access can be granted using all permissions.
  17. if (isPlugin(domain)) {
  18. return pluginPermissions();
  19. } else {
  20. return applicationPermissions();
  21. }
  22. }
  23.  
  24. private boolean isPlugin(ProtectionDomain domain) {
  25. return domain.getClassLoader() instanceof PluginClassLoader;
  26. }
  27.  
  28. private PermissionCollection pluginPermissions() {
  29. // Empty permissions = No permissions
  30. // This is not the point to add plugin permissions
  31. return new Permissions();
  32. }
  33.  
  34. private PermissionCollection applicationPermissions() {
  35. // Grant full access to the application
  36. Permissions permissions = new Permissions();
  37. permissions.add(new AllPermission());
  38. return permissions;
  39. }
  40. }
  41.  
  42. @Override
  43. protected PermissionCollection getPermissions(CodeSource codeSource)
  44. {
  45. PermissionCollection pc;
  46. // The SecureClassloader per default grants access to read resources from the source JAR.
  47. // This is useful. Call super to get those permissions:
  48. pc = super.getPermissions(codeSource);
  49. // At this point you can extend permissions.
  50. // For example grant read access to a file.
  51. pc.add(new FilePermission("path\file", "read"));
  52. return (pc);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment