web-to-spring.xsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. | XSL Sheet used by the web.xml to acegi-security beans converter
  4. | $Id$
  5. -->
  6. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  7. <xsl:output doctype-public="-//SPRING//DTD BEAN//EN"
  8. doctype-system="http://www.springframework.org/dtd/spring-beans.dtd"
  9. indent="yes"/>
  10. <!-- Variables for case conversions -->
  11. <xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
  12. <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
  13. <xsl:variable name="welcome-files" select="web-app/welcome-file-list/welcome-file"/>
  14. <!-- Convert the auth-method content to upper case -->
  15. <xsl:variable name="auth-method" select="translate(string(web-app/login-config/auth-method), $lowercase, $uppercase)"/>
  16. <!--
  17. | Find the security-role elements in the file and uses them to build a list of
  18. | all defined roles.
  19. -->
  20. <xsl:variable name="all-roles">
  21. <xsl:for-each select="web-app/security-role/role-name">
  22. <xsl:text>ROLE_</xsl:text>
  23. <xsl:value-of select="translate(string(), $lowercase, $uppercase)"/>
  24. <xsl:if test="position() != last()">,</xsl:if>
  25. </xsl:for-each>
  26. </xsl:variable>
  27. <!--
  28. | The list of filters for use in filterToBeanProxy
  29. -->
  30. <xsl:variable name="filter-list">
  31. <xsl:text>/**=httpSessionContextIntegrationFilter</xsl:text>
  32. <xsl:choose>
  33. <xsl:when test="$auth-method = 'FORM'">
  34. <xsl:text>,authenticationProcessingFilter</xsl:text>
  35. </xsl:when>
  36. <xsl:when test="$auth-method = 'BASIC'">
  37. <xsl:text>,basicProcessingFilter</xsl:text>
  38. </xsl:when>
  39. <xsl:otherwise>
  40. <xsl:message terminate="yes">Unsupported auth-method in web.xml, must be FORM or BASIC</xsl:message>
  41. </xsl:otherwise>
  42. </xsl:choose>
  43. <xsl:text>,rememberMeProcessingFilter,anonymousProcessingFilter,securityEnforcementFilter</xsl:text>
  44. </xsl:variable>
  45. <!--
  46. | The main template (where the processing work starts)
  47. -->
  48. <xsl:template match = "web-app">
  49. <beans>
  50. <xsl:call-template name="filter-to-bean-proxy"/>
  51. <xsl:call-template name="authentication-beans"/>
  52. <xsl:apply-templates select="./login-config"/>
  53. <xsl:call-template name="filter-invocation-interceptor"/>
  54. </beans>
  55. </xsl:template>
  56. <!--
  57. | Mainly static set of beans. The InMemoryDaoImpl instance is created with a single user
  58. | called "superuser" who has all the defined roles in the web.xml file.
  59. -->
  60. <xsl:template name="authentication-beans">
  61. <xsl:comment>======================== AUTHENTICATION =======================</xsl:comment>
  62. <bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager">
  63. <property name="providers">
  64. <list>
  65. <ref local="daoAuthenticationProvider"/>
  66. <ref local="anonymousAuthenticationProvider"/>
  67. <ref local="rememberMeAuthenticationProvider"/>
  68. </list>
  69. </property>
  70. </bean>
  71. <bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
  72. <property name="authenticationDao"><ref local="inMemoryDaoImpl"/></property>
  73. <!-- property name="userCache"><ref local="userCache"/></property-->
  74. </bean>
  75. <bean id="inMemoryDaoImpl" class="net.sf.acegisecurity.providers.dao.memory.InMemoryDaoImpl">
  76. <property name="userMap">
  77. <value>
  78. superuser=password,<xsl:value-of select="$all-roles"/>
  79. <xsl:text>&#xA;</xsl:text>
  80. </value>
  81. </property>
  82. </bean>
  83. <bean id="anonymousProcessingFilter" class="net.sf.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
  84. <property name="key"><value>foobar</value></property>
  85. <property name="userAttribute"><value>anonymousUser,ROLE_ANONYMOUS</value></property>
  86. </bean>
  87. <bean id="anonymousAuthenticationProvider" class="net.sf.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
  88. <property name="key"><value>foobar</value></property>
  89. </bean>
  90. <bean id="httpSessionContextIntegrationFilter" class="net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter">
  91. </bean>
  92. <bean id="rememberMeProcessingFilter" class="net.sf.acegisecurity.ui.rememberme.RememberMeProcessingFilter">
  93. <property name="rememberMeServices"><ref local="rememberMeServices"/></property>
  94. </bean>
  95. <bean id="rememberMeServices" class="net.sf.acegisecurity.ui.rememberme.TokenBasedRememberMeServices">
  96. <property name="authenticationDao"><ref local="inMemoryDaoImpl"/></property>
  97. <property name="key"><value>springRocks</value></property>
  98. </bean>
  99. <bean id="rememberMeAuthenticationProvider" class="net.sf.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
  100. <property name="key"><value>springRocks</value></property>
  101. </bean>
  102. </xsl:template>
  103. <!--
  104. | Processes the login-config definition and inserts the SecurityEnforcementFilter with
  105. | the appropriate beans for either form or basic authentication.
  106. -->
  107. <xsl:template match="login-config">
  108. <bean id="securityEnforcementFilter" class="net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter">
  109. <property name="filterSecurityInterceptor"><ref local="filterInvocationInterceptor"/></property>
  110. <property name="authenticationEntryPoint">
  111. <xsl:choose>
  112. <xsl:when test="$auth-method = 'FORM'">
  113. <ref local="authenticationProcessingFilterEntryPoint"/>
  114. </xsl:when>
  115. <xsl:when test="$auth-method = 'BASIC'">
  116. <ref local="basicProcessingFilterEntryPoint"/>
  117. </xsl:when>
  118. </xsl:choose>
  119. </property>
  120. </bean>
  121. <xsl:choose>
  122. <xsl:when test="$auth-method = 'FORM'">
  123. <xsl:call-template name="form-login"/>
  124. </xsl:when>
  125. <xsl:when test="$auth-method = 'BASIC'">
  126. <bean id="basicProcessingFilter" class="net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter">
  127. <property name="authenticationManager"><ref local="authenticationManager"/></property>
  128. <property name="authenticationEntryPoint"><ref local="basicProcessingFilterEntryPoint"/></property>
  129. </bean>
  130. <bean id="basicProcessingFilterEntryPoint" class="net.sf.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint">
  131. <property name="realmName"><value>Your Realm</value></property>
  132. </bean>
  133. </xsl:when>
  134. </xsl:choose>
  135. </xsl:template>
  136. <!--
  137. | Converts a form login configuration to an Acegi AuthenticationProcessingFilter and its entry point.
  138. | The content of the form-login-page element is used for the loginFormUrl property of the entry point
  139. | and the form-error-page is used for the authenticationFailureUrl property of the filter.
  140. |
  141. | The user must manually change the form Url to "j_acegi_security_check" in their login page.
  142. -->
  143. <xsl:template name="form-login">
  144. <xsl:message>Processing form login configuration</xsl:message>
  145. <xsl:message>Remember to switch your login form action from "j_security_check" to "j_acegi_security_check"</xsl:message>
  146. <bean id="authenticationProcessingFilter" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
  147. <property name="authenticationManager"><ref bean="authenticationManager"/></property>
  148. <property name="authenticationFailureUrl"><value><xsl:value-of select="form-login-config/form-error-page"/></value></property>
  149. <property name="defaultTargetUrl"><value></value></property>
  150. <property name="filterProcessesUrl"><value>/j_acegi_security_check</value></property>
  151. <property name="rememberMeServices"><ref local="rememberMeServices"/></property>
  152. </bean>
  153. <bean id="authenticationProcessingFilterEntryPoint" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
  154. <property name="loginFormUrl"><value><xsl:value-of select="form-login-config/form-login-page"/></value></property>
  155. <property name="forceHttps"><value>false</value></property>
  156. </bean>
  157. </xsl:template>
  158. <!--
  159. | Outputs a standard filterToBeanProxy bean.
  160. -->
  161. <xsl:template name="filter-to-bean-proxy">
  162. <xsl:comment>======================== FILTER CHAIN =======================</xsl:comment>
  163. <xsl:comment>if you wish to use channel security, add "channelProcessingFilter," in front
  164. of "httpSessionContextIntegrationFilter" in the list below</xsl:comment>
  165. <bean id="filterChainProxy" class="net.sf.acegisecurity.util.FilterChainProxy">
  166. <property name="filterInvocationDefinitionSource">
  167. <value>
  168. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  169. PATTERN_TYPE_APACHE_ANT
  170. <xsl:value-of select="$filter-list"/>
  171. </value>
  172. </property>
  173. </bean>
  174. </xsl:template>
  175. <xsl:template name="filter-invocation-interceptor">
  176. <bean id="httpRequestAccessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased">
  177. <property name="allowIfAllAbstainDecisions"><value>false</value></property>
  178. <property name="decisionVoters">
  179. <list>
  180. <ref bean="roleVoter"/>
  181. </list>
  182. </property>
  183. </bean>
  184. <xsl:comment>An access decision voter that reads ROLE_* configuration settings</xsl:comment>
  185. <bean id="roleVoter" class="net.sf.acegisecurity.vote.RoleVoter"/>
  186. <xsl:text>&#xA;</xsl:text>
  187. <xsl:comment>
  188. Note the order that entries are placed against the objectDefinitionSource is critical.
  189. The FilterSecurityInterceptor will work from the top of the list down to the FIRST pattern that matches the request URL.
  190. Accordingly, you should place MOST SPECIFIC (ie a/b/c/d.*) expressions first, with LEAST SPECIFIC (ie a/.*) expressions last
  191. </xsl:comment>
  192. <bean id="filterInvocationInterceptor" class="net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor">
  193. <property name="authenticationManager"><ref bean="authenticationManager"/></property>
  194. <property name="accessDecisionManager"><ref local="httpRequestAccessDecisionManager"/></property>
  195. <property name="objectDefinitionSource">
  196. <value>
  197. <xsl:text>&#xA;CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON&#xA;</xsl:text>
  198. <xsl:text>PATTERN_TYPE_APACHE_ANT&#xA;</xsl:text>
  199. <xsl:apply-templates select="security-constraint"/>
  200. </value>
  201. </property>
  202. </bean>
  203. </xsl:template>
  204. <!--
  205. | Converts a security-constraint (a url-pattern and the associated role-name elements)
  206. | to the form
  207. | antUrlPattern=list of allowed roles
  208. | Roles are converted to upper case and have the "ROLE_" prefix appended.
  209. |
  210. | In the case of role-name='*', signifying "any authenticated role", the complete list of roles
  211. | defined in the web.xml file is used.
  212. -->
  213. <xsl:template match="security-constraint">
  214. <xsl:value-of select="web-resource-collection/url-pattern"/>
  215. <xsl:text>=</xsl:text>
  216. <xsl:for-each select="./auth-constraint/role-name">
  217. <xsl:choose>
  218. <xsl:when test="string() = '*'">
  219. <xsl:value-of select="$all-roles"/>
  220. </xsl:when>
  221. <xsl:otherwise>
  222. <xsl:text>ROLE_</xsl:text>
  223. <xsl:value-of select="translate(string(), $lowercase, $uppercase)"/>
  224. </xsl:otherwise>
  225. </xsl:choose>
  226. <xsl:if test="position() != last()">,</xsl:if>
  227. </xsl:for-each>
  228. <xsl:text>&#xA;</xsl:text>
  229. </xsl:template>
  230. </xsl:stylesheet>