authorize-requests.adoc 5.1 KB

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