migration.adoc 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. [[migration]]
  2. = Migrating to 6.0
  3. The Spring Security team has prepared the 5.8 release to simplify upgrading to Spring Security 6.0.
  4. Use 5.8 and the steps below to minimize changes when
  5. ifdef::spring-security-version[]
  6. xref:6.0.0@migration.adoc[updating to 6.0]
  7. endif::[]
  8. ifndef::spring-security-version[]
  9. updating to 6.0
  10. endif::[]
  11. .
  12. == Servlet
  13. === Defer Loading CsrfToken
  14. In Spring Security 5, the default behavior is that the `CsrfToken` will be loaded on every request.
  15. This means that in a typical setup, the `HttpSession` must be read for every request even if it is unnecessary.
  16. In Spring Security 6, the default is that the lookup of the `CsrfToken` will be deferred until it is needed.
  17. To opt into the new Spring Security 6 default, the following configuration can be used.
  18. .Defer Loading `CsrfToken`
  19. ====
  20. .Java
  21. [source,java,role="primary"]
  22. ----
  23. @Bean
  24. DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
  25. CsrfTokenRequestAttributeHandler requestHandler = new CsrfTokenRequestAttributeHandler();
  26. // set the name of the attribute the CsrfToken will be populated on
  27. requestHandler.setCsrfRequestAttributeName("_csrf");
  28. http
  29. // ...
  30. .csrf((csrf) -> csrf
  31. .csrfTokenRequestHandler(requestHandler)
  32. );
  33. return http.build();
  34. }
  35. ----
  36. .Kotlin
  37. [source,kotlin,role="secondary"]
  38. ----
  39. @Bean
  40. open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
  41. val requestHandler = CsrfTokenRequestAttributeHandler()
  42. // set the name of the attribute the CsrfToken will be populated on
  43. requestHandler.setCsrfRequestAttributeName("_csrf")
  44. http {
  45. csrf {
  46. csrfTokenRequestHandler = requestHandler
  47. }
  48. }
  49. return http.build()
  50. }
  51. ----
  52. .XML
  53. [source,xml,role="secondary"]
  54. ----
  55. <http>
  56. <!-- ... -->
  57. <csrf request-handler-ref="requestHandler"/>
  58. </http>
  59. <b:bean id="requestHandler"
  60. class="org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler"
  61. p:csrfRequestAttributeName="_csrf"/>
  62. ----
  63. ====
  64. If this breaks your application, then you can explicitly opt into the 5.8 defaults using the following configuration:
  65. .Explicit Configure `CsrfToken` with 5.8 Defaults
  66. ====
  67. .Java
  68. [source,java,role="primary"]
  69. ----
  70. @Bean
  71. DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
  72. CsrfTokenRequestAttributeHandler requestHandler = new CsrfTokenRequestAttributeHandler();
  73. // set the name of the attribute the CsrfToken will be populated on
  74. requestHandler.setCsrfRequestAttributeName(null);
  75. http
  76. // ...
  77. .csrf((csrf) -> csrf
  78. .csrfTokenRequestHandler(requestHandler)
  79. );
  80. return http.build();
  81. }
  82. ----
  83. .Kotlin
  84. [source,kotlin,role="secondary"]
  85. ----
  86. @Bean
  87. open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
  88. val requestHandler = CsrfTokenRequestAttributeHandler()
  89. // set the name of the attribute the CsrfToken will be populated on
  90. requestHandler.setCsrfRequestAttributeName(null)
  91. http {
  92. csrf {
  93. csrfTokenRequestHandler = requestHandler
  94. }
  95. }
  96. return http.build()
  97. }
  98. ----
  99. .XML
  100. [source,xml,role="secondary"]
  101. ----
  102. <http>
  103. <!-- ... -->
  104. <csrf request-handler-ref="requestHandler"/>
  105. </http>
  106. <b:bean id="requestHandler"
  107. class="org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler">
  108. <b:property name="csrfRequestAttributeName">
  109. <b:null/>
  110. </b:property>
  111. </b:bean>
  112. ----
  113. ====
  114. === CSRF BREACH Protection
  115. If the steps for <<Defer Loading CsrfToken>> work for you, then you can also opt into Spring Security 6's default support for BREACH protection of the `CsrfToken` using the following configuration:
  116. .`CsrfToken` BREACH Protection
  117. ====
  118. .Java
  119. [source,java,role="primary"]
  120. ----
  121. @Bean
  122. DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
  123. XorCsrfTokenRequestAttributeHandler requestHandler = new XorCsrfTokenRequestAttributeHandler();
  124. // set the name of the attribute the CsrfToken will be populated on
  125. requestHandler.setCsrfRequestAttributeName("_csrf");
  126. http
  127. // ...
  128. .csrf((csrf) -> csrf
  129. .csrfTokenRequestHandler(requestHandler)
  130. );
  131. return http.build();
  132. }
  133. ----
  134. .Kotlin
  135. [source,kotlin,role="secondary"]
  136. ----
  137. @Bean
  138. open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
  139. val requestHandler = XorCsrfTokenRequestAttributeHandler()
  140. // set the name of the attribute the CsrfToken will be populated on
  141. requestHandler.setCsrfRequestAttributeName("_csrf")
  142. http {
  143. csrf {
  144. csrfTokenRequestHandler = requestHandler
  145. }
  146. }
  147. return http.build()
  148. }
  149. ----
  150. .XML
  151. [source,xml,role="secondary"]
  152. ----
  153. <http>
  154. <!-- ... -->
  155. <csrf request-handler-ref="requestHandler"/>
  156. </http>
  157. <b:bean id="requestHandler"
  158. class="org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler"
  159. p:csrfRequestAttributeName="_csrf"/>
  160. ----
  161. ====
  162. === Explicit Save SecurityContextRepository
  163. In Spring Security 5, the default behavior is for the xref:servlet/authentication/architecture.adoc#servlet-authentication-securitycontext[`SecurityContext`] to automatically be saved to the xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] using the xref:servlet/authentication/persistence.adoc#securitycontextpersistencefilter[`SecurityContextPersistenceFilter`].
  164. Saving must be done just prior to the `HttpServletResponse` being committed and just before `SecurityContextPersistenceFilter`.
  165. Unfortunately, automatic persistence of the `SecurityContext` can surprise users when it is done prior to the request completing (i.e. just prior to committing the `HttpServletResponse`).
  166. It also is complex to keep track of the state to determine if a save is necessary causing unnecessary writes to the `SecurityContextRepository` (i.e. `HttpSession`) at times.
  167. In Spring Security 6, the default behavior is that the xref:servlet/authentication/persistence.adoc#securitycontextholderfilter[`SecurityContextHolderFilter`] will only read the `SecurityContext` from `SecurityContextRepository` and populate it in the `SecurityContextHolder`.
  168. Users now must explicitly save the `SecurityContext` with the `SecurityContextRepository` if they want the `SecurityContext` to persist between requests.
  169. This removes ambiguity and improves performance by only requiring writing to the `SecurityContextRepository` (i.e. `HttpSession`) when it is necessary.
  170. To opt into the new Spring Security 6 default, the following configuration can be used.
  171. include::partial$servlet/architecture/security-context-explicit.adoc[]
  172. [[requestcache-query-optimization]]
  173. === Optimize Querying of `RequestCache`
  174. In Spring Security 5, the default behavior is to query the xref:servlet/architecture.adoc#savedrequests[saved request] on every request.
  175. This means that in a typical setup, that in order to use the xref:servlet/architecture.adoc#requestcache[`RequestCache`] the `HttpSession` is queried on every request.
  176. In Spring Security 6, the default is that `RequestCache` will only be queried for a cached request if the HTTP parameter `continue` is defined.
  177. This allows Spring Security to avoid unnecessarily reading the `HttpSession` with the `RequestCache`.
  178. In Spring Security 5 the default is to use `HttpSessionRequestCache` which will be queried for a cached request on every request.
  179. If you are not overriding the defaults (i.e. using `NullRequestCache`), then the following configuration can be used to explicitly opt into the Spring Security 6 behavior in Spring Security 5.8:
  180. include::partial$servlet/architecture/request-cache-continue.adoc[]
  181. === Use `AuthorizationManager` for Method Security
  182. xref:servlet/authorization/method-security.adoc[Method Security] has been xref:servlet/authorization/method-security.adoc#jc-enable-method-security[simplified] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
  183. Should you run into trouble with making these changes, note that `@EnableGlobalMethodSecurity`, while deprecated, will not be removed in 6.0, allowing you to opt out by sticking with the old annotation.
  184. [[servlet-replace-globalmethodsecurity-with-methodsecurity]]
  185. ==== Replace xref:servlet/authorization/method-security.adoc#jc-enable-global-method-security[global method security] with xref:servlet/authorization/method-security.adoc#jc-enable-method-security[method security]
  186. {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableGlobalMethodSecurity.html[`@EnableGlobalMethodSecurity`] and xref:servlet/appendix/namespace/method-security.adoc#nsa-global-method-security[`<global-method-security>`] are deprecated in favor of {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html[`@EnableMethodSecurity`] and xref:servlet/appendix/namespace/method-security.adoc#nsa-method-security[`<method-security>`], respectively.
  187. The new annotation and XML element activate Spring's xref:servlet/authorization/method-security.adoc#jc-enable-method-security[pre-post annotations] by default and use `AuthorizationManager` internally.
  188. This means that the following two listings are functionally equivalent:
  189. ====
  190. .Java
  191. [source,java,role="primary"]
  192. ----
  193. @EnableGlobalMethodSecurity(prePostEnabled = true)
  194. ----
  195. .Kotlin
  196. [source,kotlin,role="secondary"]
  197. ----
  198. @EnableGlobalMethodSecurity(prePostEnabled = true)
  199. ----
  200. .Xml
  201. [source,xml,role="secondary"]
  202. ----
  203. <global-method-security pre-post-enabled="true"/>
  204. ----
  205. ====
  206. and:
  207. ====
  208. .Java
  209. [source,java,role="primary"]
  210. ----
  211. @EnableMethodSecurity
  212. ----
  213. .Kotlin
  214. [source,kotlin,role="secondary"]
  215. ----
  216. @EnableMethodSecurity
  217. ----
  218. .Xml
  219. [source,xml,role="secondary"]
  220. ----
  221. <method-security/>
  222. ----
  223. ====
  224. For applications not using the pre-post annotations, make sure to turn it off to avoid activating unwanted behavior.
  225. For example, a listing like:
  226. ====
  227. .Java
  228. [source,java,role="primary"]
  229. ----
  230. @EnableGlobalMethodSecurity(securedEnabled = true)
  231. ----
  232. .Kotlin
  233. [source,kotlin,role="secondary"]
  234. ----
  235. @EnableGlobalMethodSecurity(securedEnabled = true)
  236. ----
  237. .Xml
  238. [source,xml,role="secondary"]
  239. ----
  240. <global-method-security secured-enabled="true"/>
  241. ----
  242. ====
  243. should change to:
  244. ====
  245. .Java
  246. [source,java,role="primary"]
  247. ----
  248. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  249. ----
  250. .Kotlin
  251. [source,kotlin,role="secondary"]
  252. ----
  253. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  254. ----
  255. .Xml
  256. [source,xml,role="secondary"]
  257. ----
  258. <method-security secured-enabled="true" pre-post-enabled="false"/>
  259. ----
  260. ====
  261. [[servlet-replace-permissionevaluator-bean-with-methodsecurityexpression-handler]]
  262. ==== Publish a `MethodSecurityExpressionHandler` instead of a `PermissionEvaluator`
  263. `@EnableMethodSecurity` does not pick up a `PermissionEvaluator`.
  264. This helps keep its API simple.
  265. If you have a custom {security-api-url}org/springframework/security/access/PermissionEvaluator.html[`PermissionEvaluator`] `@Bean`, please change it from:
  266. ====
  267. .Java
  268. [source,java,role="primary"]
  269. ----
  270. @Bean
  271. static PermissionEvaluator permissionEvaluator() {
  272. // ... your evaluator
  273. }
  274. ----
  275. .Kotlin
  276. [source,kotlin,role="secondary"]
  277. ----
  278. companion object {
  279. @Bean
  280. fun permissionEvaluator(): PermissionEvaluator {
  281. // ... your evaluator
  282. }
  283. }
  284. ----
  285. ====
  286. to:
  287. ====
  288. .Java
  289. [source,java,role="primary"]
  290. ----
  291. @Bean
  292. static MethodSecurityExpressionHandler expressionHandler() {
  293. var expressionHandler = new DefaultMethodSecurityExpressionHandler();
  294. expressionHandler.setPermissionEvaluator(myPermissionEvaluator);
  295. return expressionHandler;
  296. }
  297. ----
  298. .Kotlin
  299. [source,kotlin,role="secondary"]
  300. ----
  301. companion object {
  302. @Bean
  303. fun expressionHandler(): MethodSecurityExpressionHandler {
  304. val expressionHandler = DefaultMethodSecurityExpressionHandler
  305. expressionHandler.setPermissionEvaluator(myPermissionEvaluator)
  306. return expressionHandler
  307. }
  308. }
  309. ----
  310. ====
  311. ==== Replace any custom method-security ``AccessDecisionManager``s
  312. Your application may have a custom {security-api-url}org/springframework/security/access/AccessDecisionManager.html[`AccessDecisionManager`] or {security-api-url}org/springframework/security/access/AccessDecisionVoter.html[`AccessDecisionVoter`] arrangement.
  313. The preparation strategy will depend on your reason for each arrangement.
  314. Read on to find the best match for your situation.
  315. ===== I use `UnanimousBased`
  316. If your application uses {security-api-url}org/springframework/security/access/vote/UnanimousBased.html[`UnanimousBased`] with the default voters, you likely need do nothing since unanimous-based is the default behavior with {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html[`@EnableMethodSecurity`].
  317. However, if you do discover that you cannot accept the default authorization managers, you can use `AuthorizationManagers.allOf` to compose your own arrangement.
  318. Having done that, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
  319. ===== I use `AffirmativeBased`
  320. If your application uses {security-api-url}org/springframework/security/access/vote/AffirmativeBased.html[`AffirmativeBased`], then you can construct an equivalent {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`], like so:
  321. ====
  322. .Java
  323. [source,java,role="primary"]
  324. ----
  325. AuthorizationManager<MethodInvocation> authorization = AuthorizationManagers.anyOf(
  326. // ... your list of authorization managers
  327. )
  328. ----
  329. .Kotlin
  330. [source,kotlin,role="secondary"]
  331. ----
  332. val authorization = AuthorizationManagers.anyOf(
  333. // ... your list of authorization managers
  334. )
  335. ----
  336. ====
  337. Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
  338. ===== I use `ConsensusBased`
  339. There is no framework-provided equivalent for {security-api-url}org/springframework/security/access/vote/ConsensusBased.html[`ConsensusBased`].
  340. In that case, please implement a composite {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] that takes the set of delegate ``AuthorizationManager``s into account.
  341. Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
  342. ===== I use a custom `AccessDecisionVoter`
  343. You should either change the class to implement {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] or create an adapter.
  344. Without knowing what your custom voter is doing, it is impossible to recommend a general-purpose solution.
  345. By way of example, though, here is what adapting {security-api-url}org/springframework/security/access/SecurityMetadataSource.html[`SecurityMetadataSource`] and {security-api-url}org/springframework/security/access/AccessDecisionVoter.html[`AccessDecisionVoter`] for `@PreAuthorize` would look like:
  346. ====
  347. .Java
  348. [source,java,role="primary"]
  349. ----
  350. public final class PreAuthorizeAuthorizationManagerAdapter implements AuthorizationManager<MethodInvocation> {
  351. private final SecurityMetadataSource metadata;
  352. private final AccessDecisionVoter voter;
  353. public PreAuthorizeAuthorizationManagerAdapter(MethodSecurityExpressionHandler expressionHandler) {
  354. ExpressionBasedAnnotationAttributeFactory attributeFactory =
  355. new ExpressionBasedAnnotationAttributeFactory(expressionHandler);
  356. this.metadata = new PrePostAnnotationSecurityMetadataSource(attributeFactory);
  357. ExpressionBasedPreInvocationAdvice expressionAdvice = new ExpressionBasedPreInvocationAdvice();
  358. expressionAdvice.setExpressionHandler(expressionHandler);
  359. this.voter = new PreInvocationAuthorizationAdviceVoter(expressionAdvice);
  360. }
  361. public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocation invocation) {
  362. List<ConfigAttribute> attributes = this.metadata.getAttributes(invocation, AopUtils.getTargetClass(invocation.getThis()));
  363. int decision = this.voter.vote(authentication.get(), invocation, attributes);
  364. if (decision == ACCESS_GRANTED) {
  365. return new AuthorizationDecision(true);
  366. }
  367. if (decision == ACCESS_DENIED) {
  368. return new AuthorizationDecision(false);
  369. }
  370. return null; // abstain
  371. }
  372. }
  373. ----
  374. ====
  375. Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
  376. ===== I use a custom `AfterInvocationManager`
  377. {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] replaces both {security-api-url}org/springframework/security/access/AccessDecisionManager.html[`AccessDecisionManager`] and {security-api-url}org/springframework/security/access/intercept/AfterInvocationManager.html[`AfterInvocationManager`].
  378. The difference is that `AuthorizationManager<MethodInvocation>` replaces `AccessDecisionManager` and `AuthorizationManager<MethodInvocationResult>` replaces `AfterInvocationManager`.
  379. Given that, <<_i_use_a_custom_accessdecisionvoter,the same rules apply for adaptation>>, where the goal this time is to implement `AuthorizationManager<MethodInvocationResult>` instead of `AuthorizationManager<MethodInvocation>` and use `AuthorizationManagerAfterMethodInterceptor` instead of `AuthorizationManagerBeforeMethodInterceptor`.
  380. [[servlet-check-for-annotationconfigurationexceptions]]
  381. ==== Check for ``AnnotationConfigurationException``s
  382. `@EnableMethodSecurity` and `<method-security>` activate stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
  383. If after moving to either you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
  384. === Use `AuthorizationManager` for Message Security
  385. xref:servlet/integrations/websocket.adoc[Message Security] has been xref:servlet/integrations/websocket.adoc#websocket-configuration[improved] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
  386. Should you run into trouble with making these changes, you can follow the <<servlet-authorizationmanager-messages-opt-out,opt out steps>> at the end of this section.
  387. ==== Ensure all messages have defined authorization rules
  388. The now-deprecated {security-api-url}org/springframework/security/config/annotation/web/socket/AbstractSecurityWebSocketMessageBrokerConfigurer.html[message security support] permits all messages by default.
  389. xref:servlet/integrations/websocket.adoc[The new support] has the stronger default of denying all messages.
  390. To prepare for this, ensure that authorization rules exist are declared for every request.
  391. For example, an application configuration like:
  392. ====
  393. .Java
  394. [source,java,role="primary"]
  395. ----
  396. @Override
  397. protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
  398. messages
  399. .simpDestMatchers("/user/queue/errors").permitAll()
  400. .simpDestMatchers("/admin/**").hasRole("ADMIN");
  401. }
  402. ----
  403. .Kotlin
  404. [source,kotlin,role="secondary"]
  405. ----
  406. override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
  407. messages
  408. .simpDestMatchers("/user/queue/errors").permitAll()
  409. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  410. }
  411. ----
  412. .Xml
  413. [source,xml,role="secondary"]
  414. ----
  415. <websocket-message-broker>
  416. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  417. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  418. </websocket-message-broker>
  419. ----
  420. ====
  421. should change to:
  422. ====
  423. .Java
  424. [source,java,role="primary"]
  425. ----
  426. @Override
  427. protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
  428. messages
  429. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  430. .simpDestMatchers("/user/queue/errors").permitAll()
  431. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  432. .anyMessage().denyAll();
  433. }
  434. ----
  435. .Kotlin
  436. [source,kotlin,role="secondary"]
  437. ----
  438. override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
  439. messages
  440. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  441. .simpDestMatchers("/user/queue/errors").permitAll()
  442. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  443. .anyMessage().denyAll()
  444. }
  445. ----
  446. .Xml
  447. [source,xml,role="secondary"]
  448. ----
  449. <websocket-message-broker>
  450. <intercept-message type="CONNECT" access="permitAll"/>
  451. <intercept-message type="DISCONNECT" access="permitAll"/>
  452. <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
  453. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  454. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  455. <intercept-message pattern="/**" access="denyAll"/>
  456. </websocket-message-broker>
  457. ----
  458. ====
  459. ==== Add `@EnableWebSocketSecurity`
  460. [NOTE]
  461. ====
  462. If you want to have CSRF disabled and you are using Java configuration, the migration steps are slightly different.
  463. Instead of using `@EnableWebSocketSecurity`, you will override the appropriate methods in `WebSocketMessageBrokerConfigurer` yourself.
  464. Please see xref:servlet/integrations/websocket.adoc#websocket-sameorigin-disable[the reference manual] for details about this step.
  465. ====
  466. If you are using Java Configuration, add {security-api-url}org/springframework/security/config/annotation/web/socket/EnableWebSocketSecurity.html[`@EnableWebSocketSecurity`] to your application.
  467. For example, you can add it to your websocket security configuration class, like so:
  468. ====
  469. .Java
  470. [source,java,role="primary"]
  471. ----
  472. @EnableWebSocketSecurity
  473. @Configuration
  474. public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
  475. // ...
  476. }
  477. ----
  478. .Kotlin
  479. [source,kotlin,role="secondary"]
  480. ----
  481. @EnableWebSocketSecurity
  482. @Configuration
  483. class WebSocketSecurityConfig: AbstractSecurityWebSocketMessageBrokerConfigurer() {
  484. // ...
  485. }
  486. ----
  487. ====
  488. This will make a prototype instance of `MessageMatcherDelegatingAuthorizationManager.Builder` available to encourage configuration by composition instead of extension.
  489. ==== Use an `AuthorizationManager<Message<?>>` instance
  490. To start using `AuthorizationManager`, you can set the `use-authorization-manager` attribute in XML or you can publish an `AuthorizationManager<Message<?>>` `@Bean` in Java.
  491. For example, the following application configuration:
  492. ====
  493. .Java
  494. [source,java,role="primary"]
  495. ----
  496. @Override
  497. protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
  498. messages
  499. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  500. .simpDestMatchers("/user/queue/errors").permitAll()
  501. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  502. .anyMessage().denyAll();
  503. }
  504. ----
  505. .Kotlin
  506. [source,kotlin,role="secondary"]
  507. ----
  508. override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
  509. messages
  510. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  511. .simpDestMatchers("/user/queue/errors").permitAll()
  512. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  513. .anyMessage().denyAll()
  514. }
  515. ----
  516. .Xml
  517. [source,xml,role="secondary"]
  518. ----
  519. <websocket-message-broker>
  520. <intercept-message type="CONNECT" access="permitAll"/>
  521. <intercept-message type="DISCONNECT" access="permitAll"/>
  522. <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
  523. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  524. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  525. <intercept-message pattern="/**" access="denyAll"/>
  526. </websocket-message-broker>
  527. ----
  528. ====
  529. changes to:
  530. ====
  531. .Java
  532. [source,java,role="primary"]
  533. ----
  534. @Bean
  535. AuthorizationManager<Message<?>> messageSecurity(MessageMatcherDelegatingAuthorizationManager.Builder messages) {
  536. messages
  537. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  538. .simpDestMatchers("/user/queue/errors").permitAll()
  539. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  540. .anyMessage().denyAll();
  541. return messages.build();
  542. }
  543. ----
  544. .Kotlin
  545. [source,kotlin,role="secondary"]
  546. ----
  547. @Bean
  548. fun messageSecurity(val messages: MessageMatcherDelegatingAuthorizationManager.Builder): AuthorizationManager<Message<?>> {
  549. messages
  550. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  551. .simpDestMatchers("/user/queue/errors").permitAll()
  552. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  553. .anyMessage().denyAll()
  554. return messages.build()
  555. }
  556. ----
  557. .Xml
  558. [source,xml,role="secondary"]
  559. ----
  560. <websocket-message-broker use-authorization-manager="true">
  561. <intercept-message type="CONNECT" access="permitAll"/>
  562. <intercept-message type="DISCONNECT" access="permitAll"/>
  563. <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
  564. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  565. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  566. <intercept-message pattern="/**" access="denyAll"/>
  567. </websocket-message-broker>
  568. ----
  569. ====
  570. ==== Stop Implementing `AbstractSecurityWebSocketMessageBrokerConfigurer`
  571. If you are using Java configuration, you can now simply extend `WebSocketMessageBrokerConfigurer`.
  572. For example, if your class that extends `AbstractSecurityWebSocketMessageBrokerConfigurer` is called `WebSocketSecurityConfig`, then:
  573. ====
  574. .Java
  575. [source,java,role="primary"]
  576. ----
  577. @EnableWebSocketSecurity
  578. @Configuration
  579. public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
  580. // ...
  581. }
  582. ----
  583. .Kotlin
  584. [source,kotlin,role="secondary"]
  585. ----
  586. @EnableWebSocketSecurity
  587. @Configuration
  588. class WebSocketSecurityConfig: AbstractSecurityWebSocketMessageBrokerConfigurer() {
  589. // ...
  590. }
  591. ----
  592. ====
  593. changes to:
  594. ====
  595. .Java
  596. [source,java,role="primary"]
  597. ----
  598. @EnableWebSocketSecurity
  599. @Configuration
  600. public class WebSocketSecurityConfig implements WebSocketMessageBrokerConfigurer {
  601. // ...
  602. }
  603. ----
  604. .Kotlin
  605. [source,kotlin,role="secondary"]
  606. ----
  607. @EnableWebSocketSecurity
  608. @Configuration
  609. class WebSocketSecurityConfig: WebSocketMessageBrokerConfigurer {
  610. // ...
  611. }
  612. ----
  613. ====
  614. [[servlet-authorizationmanager-messages-opt-out]]
  615. ==== Opt-out Steps
  616. In case you had trouble, take a look at these scenarios for optimal opt out behavior:
  617. ===== I cannot declare an authorization rule for all requests
  618. If you are having trouble setting an `anyRequest` authorization rule of `denyAll`, please use {security-api-url}org/springframework/security/messaging/access/intercept/MessageMatcherDelegatingAuthorizationManager.Builder.Constraint.html#permitAll()[`permitAll`] instead, like so:
  619. ====
  620. .Java
  621. [source,java,role="primary"]
  622. ----
  623. @Bean
  624. AuthorizationManager<Message<?>> messageSecurity(MessageMatcherDelegatingAuthorizationManager.Builder messages) {
  625. messages
  626. .simpDestMatchers("/user/queue/errors").permitAll()
  627. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  628. // ...
  629. .anyMessage().permitAll();
  630. return messages.build();
  631. }
  632. ----
  633. .Kotlin
  634. [source,kotlin,role="secondary"]
  635. ----
  636. @Bean
  637. fun messageSecurity(val messages: MessageMatcherDelegatingAuthorizationManager.Builder): AuthorizationManager<Message<?>> {
  638. messages
  639. .simpDestMatchers("/user/queue/errors").permitAll()
  640. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  641. // ...
  642. .anyMessage().permitAll();
  643. return messages.build()
  644. }
  645. ----
  646. .Xml
  647. [source,xml,role="secondary"]
  648. ----
  649. <websocket-message-broker use-authorization-manager="true">
  650. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  651. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  652. <!-- ... -->
  653. <intercept-message pattern="/**" access="permitAll"/>
  654. </websocket-message-broker>
  655. ----
  656. ====
  657. ===== I cannot get CSRF working, need some other `AbstractSecurityWebSocketMessageBrokerConfigurer` feature, or am having trouble with `AuthorizationManager`
  658. In the case of Java, you may continue using `AbstractMessageSecurityWebSocketMessageBrokerConfigurer`.
  659. Even though it is deprecated, it will not be removed in 6.0.
  660. In the case of XML, you can opt out of `AuthorizationManager` by setting `use-authorization-manager="false"`:
  661. ====
  662. .Xml
  663. [source,xml,role="secondary"]
  664. ----
  665. <websocket-message-broker>
  666. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  667. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  668. </websocket-message-broker>
  669. ----
  670. ====
  671. to:
  672. ====
  673. .Xml
  674. [source,xml,role="secondary"]
  675. ----
  676. <websocket-message-broker use-authorization-manager="false">
  677. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  678. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  679. </websocket-message-broker>
  680. ----
  681. ====
  682. === Use `AuthorizationManager` for Request Security
  683. xref:servlet/authorization/authorize-requests.adoc[HTTP Request Security] has been xref:servlet/authorization/authorize-http-requests.adoc[simplified] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API].
  684. Should you run into trouble with making these changes, you can follow the <<servlet-authorizationmanager-requests-opt-out,opt out steps>> at the end of this section.
  685. ==== Ensure that all requests have defined authorization rules
  686. In Spring Security 5.8 and earlier, requests with no authorization rule are permitted by default.
  687. It is a stronger security position to deny by default, thus requiring that authorization rules be clearly defined for every endpoint.
  688. As such, in 6.0, Spring Security by default denies any request that is missing an authorization rule.
  689. The simplest way to prepare for this change is to introduce an appropriate {security-api-url}org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.html#anyRequest()[`anyRequest`] rule as the last authorization rule.
  690. The recommendation is {security-api-url}org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.AuthorizedUrl.html#denyAll()[`denyAll`] since that is the implied 6.0 default.
  691. [NOTE]
  692. ====
  693. You may already have an `anyRequest` rule defined that you are happy with in which case this step can be skipped.
  694. ====
  695. Adding `denyAll` to the end looks like changing:
  696. ====
  697. .Java
  698. [source,java,role="primary"]
  699. ----
  700. http
  701. .authorizeRequests((authorize) -> authorize
  702. .filterSecurityInterceptorOncePerRequest(true)
  703. .mvcMatchers("/app/**").hasRole("APP")
  704. // ...
  705. )
  706. // ...
  707. ----
  708. .Kotlin
  709. [source,kotlin,role="secondary"]
  710. ----
  711. http {
  712. authorizeRequests {
  713. filterSecurityInterceptorOncePerRequest = true
  714. authorize("/app/**", hasRole("APP"))
  715. // ...
  716. }
  717. }
  718. ----
  719. .Xml
  720. [source,xml,role="secondary"]
  721. ----
  722. <http once-per-request="true">
  723. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  724. <!-- ... -->
  725. </http>
  726. ----
  727. ====
  728. to:
  729. ====
  730. .Java
  731. [source,java,role="primary"]
  732. ----
  733. http
  734. .authorizeRequests((authorize) -> authorize
  735. .filterSecurityInterceptorOncePerRequest(true)
  736. .mvcMatchers("/app/**").hasRole("APP")
  737. // ...
  738. .anyRequest().denyAll()
  739. )
  740. // ...
  741. ----
  742. .Kotlin
  743. [source,kotlin,role="secondary"]
  744. ----
  745. http {
  746. authorizeRequests {
  747. filterSecurityInterceptorOncePerRequest = true
  748. authorize("/app/**", hasRole("APP"))
  749. // ...
  750. authorize(anyRequest, denyAll)
  751. }
  752. }
  753. ----
  754. .Xml
  755. [source,xml,role="secondary"]
  756. ----
  757. <http once-per-request="true">
  758. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  759. <!-- ... -->
  760. <intercept-url pattern="/**" access="denyAll"/>
  761. </http>
  762. ----
  763. ====
  764. If you have already migrated to `authorizeHttpRequests`, the recommended change is the same.
  765. ==== Switch to `AuthorizationManager`
  766. To opt in to using `AuthorizationManager`, you can use `authorizeHttpRequests` or xref:servlet/appendix/namespace/http.adoc#nsa-http-use-authorization-manager[`use-authorization-manager`] for Java or XML, respectively.
  767. Change:
  768. ====
  769. .Java
  770. [source,java,role="primary"]
  771. ----
  772. http
  773. .authorizeRequests((authorize) -> authorize
  774. .filterSecurityInterceptorOncePerRequest(true)
  775. .mvcMatchers("/app/**").hasRole("APP")
  776. // ...
  777. .anyRequest().denyAll()
  778. )
  779. // ...
  780. ----
  781. .Kotlin
  782. [source,kotlin,role="secondary"]
  783. ----
  784. http {
  785. authorizeRequests {
  786. filterSecurityInterceptorOncePerRequest = true
  787. authorize("/app/**", hasRole("APP"))
  788. // ...
  789. authorize(anyRequest, denyAll)
  790. }
  791. }
  792. ----
  793. .Xml
  794. [source,xml,role="secondary"]
  795. ----
  796. <http once-per-request="true">
  797. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  798. <!-- ... -->
  799. <intercept-url pattern="/**" access="denyAll"/>
  800. </http>
  801. ----
  802. ====
  803. to:
  804. ====
  805. .Java
  806. [source,java,role="primary"]
  807. ----
  808. http
  809. .authorizeHttpRequests((authorize) -> authorize
  810. .shouldFilterAllDispatcherTypes(false)
  811. .mvcMatchers("/app/**").hasRole("APP")
  812. // ...
  813. .anyRequest().denyAll()
  814. )
  815. // ...
  816. ----
  817. .Kotlin
  818. [source,kotlin,role="secondary"]
  819. ----
  820. http {
  821. authorizeHttpRequests {
  822. shouldFilterAllDispatcherTypes = false
  823. authorize("/app/**", hasRole("APP"))
  824. // ...
  825. authorize(anyRequest, denyAll)
  826. }
  827. }
  828. ----
  829. .Xml
  830. [source,xml,role="secondary"]
  831. ----
  832. <http filter-all-dispatcher-types="false" use-authorization-manager="true">
  833. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  834. <!-- ... -->
  835. <intercept-url pattern="/**" access="denyAll"/>
  836. </http>
  837. ----
  838. ====
  839. ==== Migrate SpEL expressions to `AuthorizationManager`
  840. For authorization rules, Java tends to be easier to test and maintain than SpEL.
  841. As such, `authorizeHttpRequests` does not have a method for declaring a `String` SpEL.
  842. Instead, you can implement your own `AuthorizationManager` implementation or use `WebExpressionAuthorizationManager`.
  843. For completeness, both options will be demonstrated.
  844. First, if you have the following SpEL:
  845. ====
  846. .Java
  847. [source,java,role="primary"]
  848. ----
  849. http
  850. .authorizeRequests((authorize) -> authorize
  851. .filterSecurityInterceptorOncePerRequest(true)
  852. .mvcMatchers("/complicated/**").access("hasRole('ADMIN') || hasAuthority('SCOPE_read')")
  853. // ...
  854. .anyRequest().denyAll()
  855. )
  856. // ...
  857. ----
  858. .Kotlin
  859. [source,kotlin,role="secondary"]
  860. ----
  861. http {
  862. authorizeRequests {
  863. filterSecurityInterceptorOncePerRequest = true
  864. authorize("/complicated/**", access("hasRole('ADMIN') || hasAuthority('SCOPE_read')"))
  865. // ...
  866. authorize(anyRequest, denyAll)
  867. }
  868. }
  869. ----
  870. ====
  871. Then you can compose your own `AuthorizationManager` with Spring Security authorization primitives like so:
  872. ====
  873. .Java
  874. [source,java,role="primary"]
  875. ----
  876. http
  877. .authorizeHttpRequests((authorize) -> authorize
  878. .shouldFilterAllDispatcherTypes(false)
  879. .mvcMatchers("/complicated/**").access(anyOf(hasRole("ADMIN"), hasAuthority("SCOPE_read"))
  880. // ...
  881. .anyRequest().denyAll()
  882. )
  883. // ...
  884. ----
  885. .Kotlin
  886. [source,kotlin,role="secondary"]
  887. ----
  888. http {
  889. authorizeHttpRequests {
  890. shouldFilterAllDispatcherTypes = false
  891. authorize("/complicated/**", access(anyOf(hasRole("ADMIN"), hasAuthority("SCOPE_read"))
  892. // ...
  893. authorize(anyRequest, denyAll)
  894. }
  895. }
  896. ----
  897. ====
  898. Or you can use `WebExpressionAuthorizationManager` in the following way:
  899. ====
  900. .Java
  901. [source,java,role="primary"]
  902. ----
  903. http
  904. .authorizeRequests((authorize) -> authorize
  905. .filterSecurityInterceptorOncePerRequest(true)
  906. .mvcMatchers("/complicated/**").access(
  907. new WebExpressionAuthorizationManager("hasRole('ADMIN') || hasAuthority('SCOPE_read')")
  908. )
  909. // ...
  910. .anyRequest().denyAll()
  911. )
  912. // ...
  913. ----
  914. .Kotlin
  915. [source,kotlin,role="secondary"]
  916. ----
  917. http {
  918. authorizeRequests {
  919. filterSecurityInterceptorOncePerRequest = true
  920. authorize("/complicated/**", access(
  921. WebExpressionAuthorizationManager("hasRole('ADMIN') || hasAuthority('SCOPE_read')"))
  922. )
  923. // ...
  924. authorize(anyRequest, denyAll)
  925. }
  926. }
  927. ----
  928. ====
  929. ==== Switch to filter all dispatcher types
  930. Spring Security 5.8 and earlier only xref:servlet/authorization/architecture.adoc[perform authorization] once per request.
  931. This means that dispatcher types like `FORWARD` and `INCLUDE` that run after `REQUEST` are not secured by default.
  932. It's recommended that Spring Security secure all dispatch types.
  933. As such, in 6.0, Spring Security changes this default.
  934. So, finally, change your authorization rules to filter all dispatcher types.
  935. To do this, change:
  936. ====
  937. .Java
  938. [source,java,role="primary"]
  939. ----
  940. http
  941. .authorizeHttpRequests((authorize) -> authorize
  942. .shouldFilterAllDispatcherTypes(false)
  943. .mvcMatchers("/app/**").hasRole("APP")
  944. // ...
  945. .anyRequest().denyAll()
  946. )
  947. // ...
  948. ----
  949. .Kotlin
  950. [source,kotlin,role="secondary"]
  951. ----
  952. http {
  953. authorizeHttpRequests {
  954. shouldFilterAllDispatcherTypes = false
  955. authorize("/app/**", hasRole("APP"))
  956. // ...
  957. authorize(anyRequest, denyAll)
  958. }
  959. }
  960. ----
  961. .Xml
  962. [source,xml,role="secondary"]
  963. ----
  964. <http filter-all-dispatcher-types="false" use-authorization-manager="true">
  965. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  966. <!-- ... -->
  967. <intercept-url pattern="/**" access="denyAll"/>
  968. </http>
  969. ----
  970. ====
  971. to:
  972. ====
  973. .Java
  974. [source,java,role="primary"]
  975. ----
  976. http
  977. .authorizeHttpRequests((authorize) -> authorize
  978. .shouldFilterAllDispatcherTypes(true)
  979. .mvcMatchers("/app/**").hasRole("APP")
  980. // ...
  981. .anyRequest().denyAll()
  982. )
  983. // ...
  984. ----
  985. .Kotlin
  986. [source,kotlin,role="secondary"]
  987. ----
  988. http {
  989. authorizeHttpRequests {
  990. shouldFilterAllDispatcherTypes = true
  991. authorize("/app/**", hasRole("APP"))
  992. // ...
  993. authorize(anyRequest, denyAll)
  994. }
  995. }
  996. ----
  997. .Xml
  998. [source,xml,role="secondary"]
  999. ----
  1000. <http filter-all-dispatcher-types="true" use-authorization-manager="true">
  1001. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1002. <!-- ... -->
  1003. <intercept-url pattern="/**" access="denyAll"/>
  1004. </http>
  1005. ----
  1006. ====
  1007. ==== Replace any custom filter-security ``AccessDecisionManager``s
  1008. Your application may have a custom {security-api-url}org/springframework/security/access/AccessDecisionManager.html[`AccessDecisionManager`] or {security-api-url}org/springframework/security/access/AccessDecisionVoter.html[`AccessDecisionVoter`] arrangement.
  1009. The preparation strategy will depend on your reason for each arrangement.
  1010. Read on to find the best match for your situation.
  1011. ===== I use `UnanimousBased`
  1012. If your application uses {security-api-url}org/springframework/security/access/vote/UnanimousBased.html[`UnanimousBased`], you should first adapt or replace any ``AccessDecisionVoter``s and then you can construct an `AuthorizationManager` like so:
  1013. ====
  1014. .Java
  1015. [source,java,role="primary"]
  1016. ----
  1017. @Bean
  1018. AuthorizationManager<RequestAuthorizationContext> requestAuthorization() {
  1019. PolicyAuthorizationManager policy = ...;
  1020. LocalAuthorizationManager local = ...;
  1021. return AuthorizationMangers.allOf(policy, local);
  1022. }
  1023. ----
  1024. .Kotlin
  1025. [source,kotlin,role="secondary"]
  1026. ----
  1027. @Bean
  1028. fun requestAuthorization(): AuthorizationManager<RequestAuthorizationContext> {
  1029. val policy: PolicyAuthorizationManager = ...
  1030. val local: LocalAuthorizationManager = ...
  1031. return AuthorizationMangers.allOf(policy, local)
  1032. }
  1033. ----
  1034. .Xml
  1035. [source,xml,role="secondary"]
  1036. ----
  1037. <bean id="requestAuthorization" class="org.springframework.security.authorization.AuthorizationManagers"
  1038. factory-method="allOf">
  1039. <constructor-arg>
  1040. <util:list>
  1041. <bean class="my.PolicyAuthorizationManager"/>
  1042. <bean class="my.LocalAuthorizationManager"/>
  1043. </util:list>
  1044. </constructor-arg>
  1045. </bean>
  1046. ----
  1047. ====
  1048. then, wire it into the DSL like so:
  1049. ====
  1050. .Java
  1051. [source,java,role="primary"]
  1052. ----
  1053. http
  1054. .authorizeHttpRequests((authorize) -> authorize.anyRequest().access(requestAuthorization))
  1055. // ...
  1056. ----
  1057. .Kotlin
  1058. [source,kotlin,role="secondary"]
  1059. ----
  1060. http {
  1061. authorizeHttpRequests {
  1062. authorize(anyRequest, requestAuthorization)
  1063. }
  1064. // ...
  1065. }
  1066. ----
  1067. .Xml
  1068. [source,xml,role="secondary"]
  1069. ----
  1070. <http authorization-manager-ref="requestAuthorization"/>
  1071. ----
  1072. ====
  1073. [NOTE]
  1074. ====
  1075. `authorizeHttpRequests` is designed so that you can apply a custom `AuthorizationManager` to any url pattern.
  1076. See xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[the reference] for more details.
  1077. ====
  1078. ===== I use `AffirmativeBased`
  1079. If your application uses {security-api-url}org/springframework/security/access/vote/AffirmativeBased.html[`AffirmativeBased`], then you can construct an equivalent {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`], like so:
  1080. ====
  1081. .Java
  1082. [source,java,role="primary"]
  1083. ----
  1084. @Bean
  1085. AuthorizationManager<RequestAuthorizationContext> requestAuthorization() {
  1086. PolicyAuthorizationManager policy = ...;
  1087. LocalAuthorizationManager local = ...;
  1088. return AuthorizationMangers.anyOf(policy, local);
  1089. }
  1090. ----
  1091. .Kotlin
  1092. [source,kotlin,role="secondary"]
  1093. ----
  1094. @Bean
  1095. fun requestAuthorization(): AuthorizationManager<RequestAuthorizationContext> {
  1096. val policy: PolicyAuthorizationManager = ...
  1097. val local: LocalAuthorizationManager = ...
  1098. return AuthorizationMangers.anyOf(policy, local)
  1099. }
  1100. ----
  1101. .Xml
  1102. [source,xml,role="secondary"]
  1103. ----
  1104. <bean id="requestAuthorization" class="org.springframework.security.authorization.AuthorizationManagers"
  1105. factory-method="anyOf">
  1106. <constructor-arg>
  1107. <util:list>
  1108. <bean class="my.PolicyAuthorizationManager"/>
  1109. <bean class="my.LocalAuthorizationManager"/>
  1110. </util:list>
  1111. </constructor-arg>
  1112. </bean>
  1113. ----
  1114. ====
  1115. then, wire it into the DSL like so:
  1116. ====
  1117. .Java
  1118. [source,java,role="primary"]
  1119. ----
  1120. http
  1121. .authorizeHttpRequests((authorize) -> authorize.anyRequest().access(requestAuthorization))
  1122. // ...
  1123. ----
  1124. .Kotlin
  1125. [source,kotlin,role="secondary"]
  1126. ----
  1127. http {
  1128. authorizeHttpRequests {
  1129. authorize(anyRequest, requestAuthorization)
  1130. }
  1131. // ...
  1132. }
  1133. ----
  1134. .Xml
  1135. [source,xml,role="secondary"]
  1136. ----
  1137. <http authorization-manager-ref="requestAuthorization"/>
  1138. ----
  1139. ====
  1140. [NOTE]
  1141. ====
  1142. `authorizeHttpRequests` is designed so that you can apply a custom `AuthorizationManager` to any url pattern.
  1143. See xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[the reference] for more details.
  1144. ====
  1145. ===== I use `ConsensusBased`
  1146. There is no framework-provided equivalent for {security-api-url}org/springframework/security/access/vote/ConsensusBased.html[`ConsensusBased`].
  1147. In that case, please implement a composite {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] that takes the set of delegate ``AuthorizationManager``s into account.
  1148. Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[adding a custom `AuthorizationManager`].
  1149. ===== I use a custom `AccessDecisionVoter`
  1150. You should either change the class to implement {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] or create an adapter.
  1151. Without knowing what your custom voter is doing, it is impossible to recommend a general-purpose solution.
  1152. By way of example, though, here is what adapting {security-api-url}org/springframework/security/access/SecurityMetadataSource.html[`SecurityMetadataSource`] and {security-api-url}org/springframework/security/access/AccessDecisionVoter.html[`AccessDecisionVoter`] for `anyRequest().authenticated()` would look like:
  1153. ====
  1154. .Java
  1155. [source,java,role="primary"]
  1156. ----
  1157. public final class AnyRequestAuthenticatedAuthorizationManagerAdapter implements AuthorizationManager<RequestAuthorizationContext> {
  1158. private final SecurityMetadataSource metadata;
  1159. private final AccessDecisionVoter voter;
  1160. public PreAuthorizeAuthorizationManagerAdapter(SecurityExpressionHandler expressionHandler) {
  1161. Map<RequestMatcher, List<ConfigAttribute>> requestMap = Collections.singletonMap(
  1162. AnyRequestMatcher.INSTANCE, Collections.singletonList(new SecurityConfig("authenticated")));
  1163. this.metadata = new DefaultFilterInvocationSecurityMetadataSource(requestMap);
  1164. WebExpressionVoter voter = new WebExpressionVoter();
  1165. voter.setExpressionHandler(expressionHandler);
  1166. this.voter = voter;
  1167. }
  1168. public AuthorizationDecision check(Supplier<Authentication> authentication, RequestAuthorizationContext context) {
  1169. List<ConfigAttribute> attributes = this.metadata.getAttributes(context);
  1170. int decision = this.voter.vote(authentication.get(), invocation, attributes);
  1171. if (decision == ACCESS_GRANTED) {
  1172. return new AuthorizationDecision(true);
  1173. }
  1174. if (decision == ACCESS_DENIED) {
  1175. return new AuthorizationDecision(false);
  1176. }
  1177. return null; // abstain
  1178. }
  1179. }
  1180. ----
  1181. ====
  1182. Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[adding a custom `AuthorizationManager`].
  1183. [[servlet-authorizationmanager-requests-opt-out]]
  1184. ==== Opt-out Steps
  1185. In case you had trouble, take a look at these scenarios for optimal opt out behavior:
  1186. ===== I cannot secure all dispatcher types
  1187. If you cannot secure all dispatcher types, first try and declare which dispatcher types should not require authorization like so:
  1188. ====
  1189. .Java
  1190. [source,java,role="primary"]
  1191. ----
  1192. http
  1193. .authorizeHttpRequests((authorize) -> authorize
  1194. .shouldFilterAllDispatcherTypes(true)
  1195. .dispatcherTypeMatchers(FORWARD, INCLUDE).permitAll()
  1196. .mvcMatchers("/app/**").hasRole("APP")
  1197. // ...
  1198. .anyRequest().denyAll()
  1199. )
  1200. // ...
  1201. ----
  1202. .Kotlin
  1203. [source,kotlin,role="secondary"]
  1204. ----
  1205. http {
  1206. authorizeHttpRequests {
  1207. shouldFilterAllDispatcherTypes = true
  1208. authorize(DispatcherTypeRequestMatcher(FORWARD, INCLUDE), permitAll)
  1209. authorize("/app/**", hasRole("APP"))
  1210. // ...
  1211. authorize(anyRequest, denyAll)
  1212. }
  1213. }
  1214. ----
  1215. .Xml
  1216. [source,xml,role="secondary"]
  1217. ----
  1218. <http filter-all-dispatcher-types="true" use-authorization-manager="true">
  1219. <intercept-url request-matcher-ref="dispatchers"/>
  1220. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1221. <!-- ... -->
  1222. <intercept-url pattern="/**" access="denyAll"/>
  1223. </http>
  1224. <bean id="dispatchers" class="org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher">
  1225. <constructor-arg>
  1226. <util:list value-type="javax.servlet.DispatcherType">
  1227. <value>FORWARD</value>
  1228. <value>INCLUDE</value>
  1229. </util:list>
  1230. </constructor-arg>
  1231. </bean>
  1232. ----
  1233. ====
  1234. Or, if that doesn't work, then you can explicitly opt out of the behavior by setting `filter-all-dispatcher-types` and `filterAllDispatcherTypes` to `false`:
  1235. ====
  1236. .Java
  1237. [source,java,role="primary"]
  1238. ----
  1239. http
  1240. .authorizeHttpRequests((authorize) -> authorize
  1241. .filterAllDispatcherTypes(false)
  1242. .mvcMatchers("/app/**").hasRole("APP")
  1243. // ...
  1244. )
  1245. // ...
  1246. ----
  1247. .Kotlin
  1248. [source,kotlin,role="secondary"]
  1249. ----
  1250. http {
  1251. authorizeHttpRequests {
  1252. filterAllDispatcherTypes = false
  1253. authorize("/messages/**", hasRole("APP"))
  1254. // ...
  1255. }
  1256. }
  1257. ----
  1258. .Xml
  1259. [source,xml,role="secondary"]
  1260. ----
  1261. <http filter-all-dispatcher-types="false" use-authorization-manager="true">
  1262. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1263. <!-- ... -->
  1264. </http>
  1265. ----
  1266. ====
  1267. or, if you are still using `authorizeRequests` or `use-authorization-manager="false"`, set `oncePerRequest` to `true`:
  1268. ====
  1269. .Java
  1270. [source,java,role="primary"]
  1271. ----
  1272. http
  1273. .authorizeRequests((authorize) -> authorize
  1274. .filterSecurityInterceptorOncePerRequest(true)
  1275. .mvcMatchers("/app/**").hasRole("APP")
  1276. // ...
  1277. )
  1278. // ...
  1279. ----
  1280. .Kotlin
  1281. [source,kotlin,role="secondary"]
  1282. ----
  1283. http {
  1284. authorizeRequests {
  1285. filterSecurityInterceptorOncePerRequest = true
  1286. authorize("/messages/**", hasRole("APP"))
  1287. // ...
  1288. }
  1289. }
  1290. ----
  1291. .Xml
  1292. [source,xml,role="secondary"]
  1293. ----
  1294. <http once-per-request="true" use-authorization-manager="false">
  1295. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1296. <!-- ... -->
  1297. </http>
  1298. ----
  1299. ====
  1300. ===== I cannot declare an authorization rule for all requests
  1301. If you are having trouble setting an `anyRequest` authorization rule of `denyAll`, please use {security-api-url}org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.AuthorizedUrl.html#permitAll()[`permitAll`] instead, like so:
  1302. ====
  1303. .Java
  1304. [source,java,role="primary"]
  1305. ----
  1306. http
  1307. .authorizeHttpReqeusts((authorize) -> authorize
  1308. .mvcMatchers("/app/*").hasRole("APP")
  1309. // ...
  1310. .anyRequest().permitAll()
  1311. )
  1312. ----
  1313. .Kotlin
  1314. [source,kotlin,role="secondary"]
  1315. ----
  1316. http {
  1317. authorizeHttpRequests {
  1318. authorize("/app*", hasRole("APP"))
  1319. // ...
  1320. authorize(anyRequest, permitAll)
  1321. }
  1322. }
  1323. ----
  1324. .Xml
  1325. [source,xml,role="secondary"]
  1326. ----
  1327. <http>
  1328. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1329. <!-- ... -->
  1330. <intercept-url pattern="/**" access="permitAll"/>
  1331. </http>
  1332. ----
  1333. ====
  1334. ===== I cannot migrate my SpEL or my `AccessDecisionManager`
  1335. If you are having trouble with SpEL, `AccessDecisionManager`, or there is some other feature that you are needing to keep using in `<http>` or `authorizeRequests`, try the following.
  1336. First, if you still need `authorizeRequests`, you are welcome to keep using it. Even though it is deprecated, it is not removed in 6.0.
  1337. Second, if you still need your custom `access-decision-manager-ref` or have some other reason to opt out of `AuthorizationManager`, do:
  1338. ====
  1339. .Xml
  1340. [source,xml,role="secondary"]
  1341. ----
  1342. <http use-authorization-manager="false">
  1343. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1344. <!-- ... -->
  1345. </http>
  1346. ----
  1347. ====
  1348. == Reactive
  1349. === Use `AuthorizationManager` for Method Security
  1350. xref:reactive/authorization/method.adoc[Method Security] has been xref:reactive/authorization/method.adoc#jc-enable-reactive-method-security-authorization-manager[improved] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
  1351. Should you run into trouble with making these changes, you can follow the
  1352. <<reactive-authorizationmanager-methods-opt-out,opt out steps>> at the end of this section.
  1353. In Spring Security 5.8, `useAuthorizationManager` was added to {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurity.html[`@EnableReactiveMethodSecurity`] to allow applications to opt in to ``AuthorizationManager``'s features.
  1354. [[reactive-change-to-useauthorizationmanager]]
  1355. ==== Change `useAuthorizationManager` to `true`
  1356. To opt in, change `useAuthorizationManager` to `true` like so:
  1357. ====
  1358. .Java
  1359. [source,java,role="primary"]
  1360. ----
  1361. @EnableReactiveMethodSecurity
  1362. ----
  1363. .Kotlin
  1364. [source,kotlin,role="secondary"]
  1365. ----
  1366. @EnableReactiveMethodSecurity
  1367. ----
  1368. ====
  1369. changes to:
  1370. ====
  1371. .Java
  1372. [source,java,role="primary"]
  1373. ----
  1374. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  1375. ----
  1376. .Kotlin
  1377. [source,kotlin,role="secondary"]
  1378. ----
  1379. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  1380. ----
  1381. ====
  1382. [[reactive-check-for-annotationconfigurationexceptions]]
  1383. ==== Check for ``AnnotationConfigurationException``s
  1384. `useAuthorizationManager` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
  1385. If after turning on `useAuthorizationManager` you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
  1386. [[reactive-authorizationmanager-methods-opt-out]]
  1387. ==== Opt-out Steps
  1388. If you ran into trouble with `AuthorizationManager` for reactive method security, you can opt out by changing:
  1389. ====
  1390. .Java
  1391. [source,java,role="primary"]
  1392. ----
  1393. @EnableReactiveMethodSecurity
  1394. ----
  1395. .Kotlin
  1396. [source,kotlin,role="secondary"]
  1397. ----
  1398. @EnableReactiveMethodSecurity
  1399. ----
  1400. ====
  1401. to:
  1402. ====
  1403. .Java
  1404. [source,java,role="primary"]
  1405. ----
  1406. @EnableReactiveMethodSecurity(useAuthorizationManager = false)
  1407. ----
  1408. .Kotlin
  1409. [source,kotlin,role="secondary"]
  1410. ----
  1411. @EnableReactiveMethodSecurity(useAuthorizationManager = false)
  1412. ----
  1413. ====