authorize-requests.adoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. [[servlet-authorization-filtersecurityinterceptor]]
  2. = Authorize HttpServletRequest with FilterSecurityInterceptor
  3. :figures: servlet/authorization
  4. [NOTE]
  5. `FilterSecurityInterceptor` is in the process of being replaced by xref:servlet/authorization/authorize-http-requests.adoc[`AuthorizationFilter`].
  6. Consider using that instead.
  7. This section builds on xref:servlet/architecture.adoc#servlet-architecture[Servlet Architecture and Implementation] by digging deeper into how xref:servlet/authorization/index.adoc#servlet-authorization[authorization] works within Servlet based applications.
  8. The {security-api-url}org/springframework/security/web/access/intercept/FilterSecurityInterceptor.html[`FilterSecurityInterceptor`] provides xref:servlet/authorization/index.adoc#servlet-authorization[authorization] for ``HttpServletRequest``s.
  9. It is inserted into the xref:servlet/architecture.adoc#servlet-filterchainproxy[FilterChainProxy] as one of the xref:servlet/architecture.adoc#servlet-security-filters[Security Filters].
  10. .Authorize HttpServletRequest
  11. image::{figures}/filtersecurityinterceptor.png[]
  12. * image:{icondir}/number_1.png[] First, the `FilterSecurityInterceptor` obtains an xref:servlet/authentication/architecture.adoc#servlet-authentication-authentication[Authentication] from the xref:servlet/authentication/architecture.adoc#servlet-authentication-securitycontextholder[SecurityContextHolder].
  13. * image:{icondir}/number_2.png[] Second, `FilterSecurityInterceptor` creates a {security-api-url}org/springframework/security/web/FilterInvocation.html[`FilterInvocation`] from the `HttpServletRequest`, `HttpServletResponse`, and `FilterChain` that are passed into the `FilterSecurityInterceptor`.
  14. // FIXME: link to FilterInvocation
  15. * image:{icondir}/number_3.png[] Next, it passes the `FilterInvocation` to `SecurityMetadataSource` to get the ``ConfigAttribute``s.
  16. * image:{icondir}/number_4.png[] Finally, it passes the `Authentication`, `FilterInvocation`, and ``ConfigAttribute``s to the xref:servlet/authorization.adoc#authz-access-decision-manager`AccessDecisionManager`.
  17. ** image:{icondir}/number_5.png[] If authorization is denied, an `AccessDeniedException` is thrown.
  18. In this case the xref:servlet/architecture.adoc#servlet-exceptiontranslationfilter[`ExceptionTranslationFilter`] handles the `AccessDeniedException`.
  19. ** image:{icondir}/number_6.png[] If access is granted, `FilterSecurityInterceptor` continues with the xref:servlet/architecture.adoc#servlet-filters-review[FilterChain] which allows the application to process normally.
  20. // configuration (xml/java)
  21. By default, Spring Security's authorization will require all requests to be authenticated.
  22. The explicit configuration looks like:
  23. [[servlet-authorize-requests-defaults]]
  24. .Every Request Must be Authenticated
  25. [tabs]
  26. ======
  27. Java::
  28. +
  29. [source,java,role="primary"]
  30. ----
  31. @Bean
  32. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  33. http
  34. // ...
  35. .authorizeRequests(authorize -> authorize
  36. .anyRequest().authenticated()
  37. );
  38. return http.build();
  39. }
  40. ----
  41. XML::
  42. +
  43. [source,xml,role="secondary"]
  44. ----
  45. <http>
  46. <!-- ... -->
  47. <intercept-url pattern="/**" access="authenticated"/>
  48. </http>
  49. ----
  50. Kotlin::
  51. +
  52. [source,kotlin,role="secondary"]
  53. ----
  54. @Bean
  55. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  56. http {
  57. // ...
  58. authorizeRequests {
  59. authorize(anyRequest, authenticated)
  60. }
  61. }
  62. return http.build()
  63. }
  64. ----
  65. ======
  66. We can configure Spring Security to have different rules by adding more rules in order of precedence.
  67. .Authorize Requests
  68. [tabs]
  69. ======
  70. Java::
  71. +
  72. [source,java,role="primary"]
  73. ----
  74. @Bean
  75. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  76. http
  77. // ...
  78. .authorizeRequests(authorize -> authorize // <1>
  79. .mvcMatchers("/resources/**", "/signup", "/about").permitAll() // <2>
  80. .mvcMatchers("/admin/**").hasRole("ADMIN") // <3>
  81. .mvcMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") // <4>
  82. .anyRequest().denyAll() // <5>
  83. );
  84. return http.build();
  85. }
  86. ----
  87. XML::
  88. +
  89. [source,xml,role="secondary"]
  90. ----
  91. <http> <!--1-->
  92. <!-- ... -->
  93. <!--2-->
  94. <intercept-url pattern="/resources/**" access="permitAll"/>
  95. <intercept-url pattern="/signup" access="permitAll"/>
  96. <intercept-url pattern="/about" access="permitAll"/>
  97. <intercept-url pattern="/admin/**" access="hasRole('ADMIN')"/> <!--3-->
  98. <intercept-url pattern="/db/**" access="hasRole('ADMIN') and hasRole('DBA')"/> <!--4-->
  99. <intercept-url pattern="/**" access="denyAll"/> <!--5-->
  100. </http>
  101. ----
  102. Kotlin::
  103. +
  104. [source,kotlin,role="secondary"]
  105. ----
  106. @Bean
  107. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  108. http {
  109. authorizeRequests { // <1>
  110. authorize("/resources/**", permitAll) // <2>
  111. authorize("/signup", permitAll)
  112. authorize("/about", permitAll)
  113. authorize("/admin/**", hasRole("ADMIN")) // <3>
  114. authorize("/db/**", "hasRole('ADMIN') and hasRole('DBA')") // <4>
  115. authorize(anyRequest, denyAll) // <5>
  116. }
  117. }
  118. return http.build()
  119. }
  120. ----
  121. ======
  122. <1> There are multiple authorization rules specified.
  123. Each rule is considered in the order they were declared.
  124. <2> We specified multiple URL patterns that any user can access.
  125. Specifically, any user can access a request if the URL starts with "/resources/", equals "/signup", or equals "/about".
  126. <3> Any URL that starts with "/admin/" will be restricted to users who have the role "ROLE_ADMIN".
  127. You will notice that since we are invoking the `hasRole` method we do not need to specify the "ROLE_" prefix.
  128. <4> Any URL that starts with "/db/" requires the user to have both "ROLE_ADMIN" and "ROLE_DBA".
  129. You will notice that since we are using the `hasRole` expression we do not need to specify the "ROLE_" prefix.
  130. <5> Any URL that has not already been matched on is denied access.
  131. This is a good strategy if you do not want to accidentally forget to update your authorization rules.