authorize-requests.adoc 5.7 KB

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