runas-auth-provider.xml 5.9 KB

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