runas-auth-provider.xml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <chapter xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="runas"><info><title>Run-As Authentication Replacement</title></info>
  2. <section xml:id="runas-overview"><info><title>Overview</title></info>
  3. <para>The <literal>AbstractSecurityInterceptor</literal> is able to
  4. temporarily replace the <literal>Authentication</literal> object in
  5. the <literal>SecurityContext</literal> and
  6. <literal>SecurityContextHolder</literal> during the secure object
  7. callback phase. This only occurs if the original
  8. <literal>Authentication</literal> object was successfully processed by
  9. the <literal>AuthenticationManager</literal> and
  10. <literal>AccessDecisionManager</literal>. The
  11. <literal>RunAsManager</literal> will indicate the replacement
  12. <literal>Authentication</literal> object, if any, that should be used
  13. during the <literal>SecurityInterceptorCallback</literal>.</para>
  14. <para>By temporarily replacing the <literal>Authentication</literal>
  15. object during the secure object callback phase, the secured invocation
  16. will be able to call other objects which require different
  17. authentication and authorization credentials. It will also be able to
  18. perform any internal security checks for specific
  19. <literal>GrantedAuthority</literal> objects. Because Spring Security
  20. provides a number of helper classes that automatically configure
  21. remoting protocols based on the contents of the
  22. <literal>SecurityContextHolder</literal>, these run-as replacements
  23. are particularly useful when calling remote web services</para>
  24. </section>
  25. <section xml:id="runas-config"><info><title>Configuration</title></info>
  26. <para>A <literal>RunAsManager</literal> interface is provided by Acegi
  27. Security:</para>
  28. <para><programlisting>public Authentication buildRunAs(Authentication authentication, Object object, ConfigAttributeDefinition config);
  29. public boolean supports(ConfigAttribute attribute);
  30. public boolean supports(Class clazz);</programlisting></para>
  31. <para>The first method returns the <literal>Authentication</literal>
  32. object that should replace the existing
  33. <literal>Authentication</literal> object for the duration of the
  34. method invocation. If the method returns <literal>null</literal>, it
  35. indicates no replacement should be made. The second method is used by
  36. the <literal>AbstractSecurityInterceptor</literal> as part of its
  37. startup validation of configuration attributes. The
  38. <literal>supports(Class)</literal> method is called by a security
  39. interceptor implementation to ensure the configured
  40. <literal>RunAsManager</literal> supports the type of secure object
  41. that the security interceptor will present.</para>
  42. <para>One concrete implementation of a <literal>RunAsManager</literal>
  43. is provided with Spring Security. The
  44. <literal>RunAsManagerImpl</literal> class returns a replacement
  45. <literal>RunAsUserToken</literal> if any
  46. <literal>ConfigAttribute</literal> starts with
  47. <literal>RUN_AS_</literal>. If any such
  48. <literal>ConfigAttribute</literal> is found, the replacement
  49. <literal>RunAsUserToken</literal> will contain the same principal,
  50. credentials and granted authorities as the original
  51. <literal>Authentication</literal> object, along with a new
  52. <literal>GrantedAuthorityImpl</literal> for each
  53. <literal>RUN_AS_</literal> <literal>ConfigAttribute</literal>. Each
  54. new <literal>GrantedAuthorityImpl</literal> will be prefixed with
  55. <literal>ROLE_</literal>, followed by the <literal>RUN_AS</literal>
  56. <literal>ConfigAttribute</literal>. For example, a
  57. <literal>RUN_AS_SERVER</literal> will result in the replacement
  58. <literal>RunAsUserToken</literal> containing a
  59. <literal>ROLE_RUN_AS_SERVER</literal> granted authority.</para>
  60. <para>The replacement <literal>RunAsUserToken</literal> is just like
  61. any other <literal>Authentication</literal> object. It needs to be
  62. authenticated by the <literal>AuthenticationManager</literal>,
  63. probably via delegation to a suitable
  64. <literal>AuthenticationProvider</literal>. The
  65. <literal>RunAsImplAuthenticationProvider</literal> performs such
  66. authentication. It simply accepts as valid any
  67. <literal>RunAsUserToken</literal> presented.</para>
  68. <para>To ensure malicious code does not create a
  69. <literal>RunAsUserToken</literal> and present it for guaranteed
  70. acceptance by the <literal>RunAsImplAuthenticationProvider</literal>,
  71. the hash of a key is stored in all generated tokens. The
  72. <literal>RunAsManagerImpl</literal> and
  73. <literal>RunAsImplAuthenticationProvider</literal> is created in the
  74. bean context with the same key:</para>
  75. <para><programlisting>
  76. &lt;bean id="runAsManager" class="org.springframework.security.runas.RunAsManagerImpl"&gt;
  77. &lt;property name="key"&gt;&lt;value&gt;my_run_as_password&lt;/value&gt;&lt;/property&gt;
  78. &lt;/bean&gt;
  79. &lt;bean id="runAsAuthenticationProvider"
  80. class="org.springframework.security.runas.RunAsImplAuthenticationProvider"&gt;
  81. &lt;property name="key"&gt;&lt;value&gt;my_run_as_password&lt;/value&gt;&lt;/property&gt;
  82. &lt;/bean&gt;
  83. </programlisting></para>
  84. <para>By using the same key, each <literal>RunAsUserToken</literal>
  85. can be validated it was created by an approved
  86. <literal>RunAsManagerImpl</literal>. The
  87. <literal>RunAsUserToken</literal> is immutable after creation for
  88. security reasons</para>
  89. </section>
  90. </chapter>