1 22 package org.jboss.proxy; 23 24 import java.security.Principal ; 25 import java.security.PrivilegedAction ; 26 import java.security.AccessController ; 27 28 import org.jboss.invocation.Invocation; 29 import org.jboss.security.SecurityAssociation; 30 31 37 public class SecurityInterceptor 38 extends Interceptor 39 { 40 41 private static final long serialVersionUID = -4206940878404525061L; 42 43 46 public SecurityInterceptor() 47 { 48 } 49 50 52 public Object invoke(Invocation invocation) 53 throws Throwable 54 { 55 SecurityActions sa = SecurityActions.UTIL.getSecurityActions(); 57 58 Principal principal = sa.getPrincipal(); 59 if (principal != null) 60 { 61 invocation.setPrincipal(principal); 62 } 63 64 Object credential = sa.getCredential(); 65 if (credential != null) 66 { 67 invocation.setCredential(credential); 68 } 69 70 return getNext().invoke(invocation); 71 } 72 73 interface SecurityActions 74 { 75 class UTIL 76 { 77 static SecurityActions getSecurityActions() 78 { 79 return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED; 80 } 81 } 82 83 SecurityActions NON_PRIVILEGED = new SecurityActions() 84 { 85 public Principal getPrincipal() 86 { 87 return SecurityAssociation.getPrincipal(); 88 } 89 90 public Object getCredential() 91 { 92 return SecurityAssociation.getCredential(); 93 } 94 }; 95 96 SecurityActions PRIVILEGED = new SecurityActions() 97 { 98 private final PrivilegedAction getPrincipalAction = new PrivilegedAction () 99 { 100 public Object run() 101 { 102 return SecurityAssociation.getPrincipal(); 103 } 104 }; 105 106 private final PrivilegedAction getCredentialAction = new PrivilegedAction () 107 { 108 public Object run() 109 { 110 return SecurityAssociation.getCredential(); 111 } 112 }; 113 114 public Principal getPrincipal() 115 { 116 return (Principal )AccessController.doPrivileged(getPrincipalAction); 117 } 118 119 public Object getCredential() 120 { 121 return AccessController.doPrivileged(getCredentialAction); 122 } 123 }; 124 125 Principal getPrincipal(); 126 127 Object getCredential(); 128 } 129 } 130 | Popular Tags |