migration.adoc 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. [[servlet-check-for-annotationconfigurationexceptions]]
  312. ==== Check for ``AnnotationConfigurationException``s
  313. `@EnableMethodSecurity` and `<method-security>` activate stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
  314. 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.
  315. === Use `AuthorizationManager` for Message Security
  316. 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.
  317. 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.
  318. ==== Ensure all messages have defined authorization rules
  319. The now-deprecated {security-api-url}org/springframework/security/config/annotation/web/socket/AbstractSecurityWebSocketMessageBrokerConfigurer.html[message security support] permits all messages by default.
  320. xref:servlet/integrations/websocket.adoc[The new support] has the stronger default of denying all messages.
  321. To prepare for this, ensure that authorization rules exist are declared for every request.
  322. For example, an application configuration like:
  323. ====
  324. .Java
  325. [source,java,role="primary"]
  326. ----
  327. @Override
  328. protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
  329. messages
  330. .simpDestMatchers("/user/queue/errors").permitAll()
  331. .simpDestMatchers("/admin/**").hasRole("ADMIN");
  332. }
  333. ----
  334. .Kotlin
  335. [source,kotlin,role="secondary"]
  336. ----
  337. override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
  338. messages
  339. .simpDestMatchers("/user/queue/errors").permitAll()
  340. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  341. }
  342. ----
  343. .Xml
  344. [source,xml,role="secondary"]
  345. ----
  346. <websocket-message-broker>
  347. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  348. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  349. </websocket-message-broker>
  350. ----
  351. ====
  352. should change to:
  353. ====
  354. .Java
  355. [source,java,role="primary"]
  356. ----
  357. @Override
  358. protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
  359. messages
  360. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  361. .simpDestMatchers("/user/queue/errors").permitAll()
  362. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  363. .anyMessage().denyAll();
  364. }
  365. ----
  366. .Kotlin
  367. [source,kotlin,role="secondary"]
  368. ----
  369. override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
  370. messages
  371. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  372. .simpDestMatchers("/user/queue/errors").permitAll()
  373. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  374. .anyMessage().denyAll()
  375. }
  376. ----
  377. .Xml
  378. [source,xml,role="secondary"]
  379. ----
  380. <websocket-message-broker>
  381. <intercept-message type="CONNECT" access="permitAll"/>
  382. <intercept-message type="DISCONNECT" access="permitAll"/>
  383. <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
  384. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  385. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  386. <intercept-message pattern="/**" access="denyAll"/>
  387. </websocket-message-broker>
  388. ----
  389. ====
  390. ==== Add `@EnableWebSocketSecurity`
  391. [NOTE]
  392. ====
  393. If you want to have CSRF disabled and you are using Java configuration, the migration steps are slightly different.
  394. Instead of using `@EnableWebSocketSecurity`, you will override the appropriate methods in `WebSocketMessageBrokerConfigurer` yourself.
  395. Please see xref:servlet/integrations/websocket.adoc#websocket-sameorigin-disable[the reference manual] for details about this step.
  396. ====
  397. If you are using Java Configuration, add {security-api-url}org/springframework/security/config/annotation/web/socket/EnableWebSocketSecurity.html[`@EnableWebSocketSecurity`] to your application.
  398. For example, you can add it to your websocket security configuration class, like so:
  399. ====
  400. .Java
  401. [source,java,role="primary"]
  402. ----
  403. @EnableWebSocketSecurity
  404. @Configuration
  405. public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
  406. // ...
  407. }
  408. ----
  409. .Kotlin
  410. [source,kotlin,role="secondary"]
  411. ----
  412. @EnableWebSocketSecurity
  413. @Configuration
  414. class WebSocketSecurityConfig: AbstractSecurityWebSocketMessageBrokerConfigurer() {
  415. // ...
  416. }
  417. ----
  418. ====
  419. This will make a prototype instance of `MessageMatcherDelegatingAuthorizationManager.Builder` available to encourage configuration by composition instead of extension.
  420. ==== Use an `AuthorizationManager<Message<?>>` instance
  421. To start using `AuthorizationManager`, you can set the `use-authorization-manager` attribute in XML or you can publish an `AuthorizationManager<Message<?>>` `@Bean` in Java.
  422. For example, the following application configuration:
  423. ====
  424. .Java
  425. [source,java,role="primary"]
  426. ----
  427. @Override
  428. protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
  429. messages
  430. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  431. .simpDestMatchers("/user/queue/errors").permitAll()
  432. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  433. .anyMessage().denyAll();
  434. }
  435. ----
  436. .Kotlin
  437. [source,kotlin,role="secondary"]
  438. ----
  439. override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
  440. messages
  441. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  442. .simpDestMatchers("/user/queue/errors").permitAll()
  443. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  444. .anyMessage().denyAll()
  445. }
  446. ----
  447. .Xml
  448. [source,xml,role="secondary"]
  449. ----
  450. <websocket-message-broker>
  451. <intercept-message type="CONNECT" access="permitAll"/>
  452. <intercept-message type="DISCONNECT" access="permitAll"/>
  453. <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
  454. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  455. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  456. <intercept-message pattern="/**" access="denyAll"/>
  457. </websocket-message-broker>
  458. ----
  459. ====
  460. changes to:
  461. ====
  462. .Java
  463. [source,java,role="primary"]
  464. ----
  465. @Bean
  466. AuthorizationManager<Message<?>> messageSecurity(MessageMatcherDelegatingAuthorizationManager.Builder messages) {
  467. messages
  468. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  469. .simpDestMatchers("/user/queue/errors").permitAll()
  470. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  471. .anyMessage().denyAll();
  472. return messages.build();
  473. }
  474. ----
  475. .Kotlin
  476. [source,kotlin,role="secondary"]
  477. ----
  478. @Bean
  479. fun messageSecurity(val messages: MessageMatcherDelegatingAuthorizationManager.Builder): AuthorizationManager<Message<?>> {
  480. messages
  481. .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
  482. .simpDestMatchers("/user/queue/errors").permitAll()
  483. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  484. .anyMessage().denyAll()
  485. return messages.build()
  486. }
  487. ----
  488. .Xml
  489. [source,xml,role="secondary"]
  490. ----
  491. <websocket-message-broker use-authorization-manager="true">
  492. <intercept-message type="CONNECT" access="permitAll"/>
  493. <intercept-message type="DISCONNECT" access="permitAll"/>
  494. <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
  495. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  496. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  497. <intercept-message pattern="/**" access="denyAll"/>
  498. </websocket-message-broker>
  499. ----
  500. ====
  501. ==== Stop Implementing `AbstractSecurityWebSocketMessageBrokerConfigurer`
  502. If you are using Java configuration, you can now simply extend `WebSocketMessageBrokerConfigurer`.
  503. For example, if your class that extends `AbstractSecurityWebSocketMessageBrokerConfigurer` is called `WebSocketSecurityConfig`, then:
  504. ====
  505. .Java
  506. [source,java,role="primary"]
  507. ----
  508. @EnableWebSocketSecurity
  509. @Configuration
  510. public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
  511. // ...
  512. }
  513. ----
  514. .Kotlin
  515. [source,kotlin,role="secondary"]
  516. ----
  517. @EnableWebSocketSecurity
  518. @Configuration
  519. class WebSocketSecurityConfig: AbstractSecurityWebSocketMessageBrokerConfigurer() {
  520. // ...
  521. }
  522. ----
  523. ====
  524. changes to:
  525. ====
  526. .Java
  527. [source,java,role="primary"]
  528. ----
  529. @EnableWebSocketSecurity
  530. @Configuration
  531. public class WebSocketSecurityConfig implements WebSocketMessageBrokerConfigurer {
  532. // ...
  533. }
  534. ----
  535. .Kotlin
  536. [source,kotlin,role="secondary"]
  537. ----
  538. @EnableWebSocketSecurity
  539. @Configuration
  540. class WebSocketSecurityConfig: WebSocketMessageBrokerConfigurer {
  541. // ...
  542. }
  543. ----
  544. ====
  545. [[servlet-authorizationmanager-messages-opt-out]]
  546. ==== Opt-out Steps
  547. In case you had trouble, take a look at these scenarios for optimal opt out behavior:
  548. ===== I cannot declare an authorization rule for all requests
  549. 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:
  550. ====
  551. .Java
  552. [source,java,role="primary"]
  553. ----
  554. @Bean
  555. AuthorizationManager<Message<?>> messageSecurity(MessageMatcherDelegatingAuthorizationManager.Builder messages) {
  556. messages
  557. .simpDestMatchers("/user/queue/errors").permitAll()
  558. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  559. // ...
  560. .anyMessage().permitAll();
  561. return messages.build();
  562. }
  563. ----
  564. .Kotlin
  565. [source,kotlin,role="secondary"]
  566. ----
  567. @Bean
  568. fun messageSecurity(val messages: MessageMatcherDelegatingAuthorizationManager.Builder): AuthorizationManager<Message<?>> {
  569. messages
  570. .simpDestMatchers("/user/queue/errors").permitAll()
  571. .simpDestMatchers("/admin/**").hasRole("ADMIN")
  572. // ...
  573. .anyMessage().permitAll();
  574. return messages.build()
  575. }
  576. ----
  577. .Xml
  578. [source,xml,role="secondary"]
  579. ----
  580. <websocket-message-broker use-authorization-manager="true">
  581. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  582. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  583. <!-- ... -->
  584. <intercept-message pattern="/**" access="permitAll"/>
  585. </websocket-message-broker>
  586. ----
  587. ====
  588. ===== I cannot get CSRF working, need some other `AbstractSecurityWebSocketMessageBrokerConfigurer` feature, or am having trouble with `AuthorizationManager`
  589. In the case of Java, you may continue using `AbstractMessageSecurityWebSocketMessageBrokerConfigurer`.
  590. Even though it is deprecated, it will not be removed in 6.0.
  591. In the case of XML, you can opt out of `AuthorizationManager` by setting `use-authorization-manager="false"`:
  592. ====
  593. .Xml
  594. [source,xml,role="secondary"]
  595. ----
  596. <websocket-message-broker>
  597. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  598. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  599. </websocket-message-broker>
  600. ----
  601. ====
  602. to:
  603. ====
  604. .Xml
  605. [source,xml,role="secondary"]
  606. ----
  607. <websocket-message-broker use-authorization-manager="false">
  608. <intercept-message pattern="/user/queue/errors" access="permitAll"/>
  609. <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
  610. </websocket-message-broker>
  611. ----
  612. ====
  613. === Use `AuthorizationManager` for Request Security
  614. 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].
  615. 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.
  616. ==== Ensure that all requests have defined authorization rules
  617. In Spring Security 5.8 and earlier, requests with no authorization rule are permitted by default.
  618. It is a stronger security position to deny by default, thus requiring that authorization rules be clearly defined for every endpoint.
  619. As such, in 6.0, Spring Security by default denies any request that is missing an authorization rule.
  620. 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.
  621. 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.
  622. [NOTE]
  623. ====
  624. You may already have an `anyRequest` rule defined that you are happy with in which case this step can be skipped.
  625. ====
  626. Adding `denyAll` to the end looks like changing:
  627. ====
  628. .Java
  629. [source,java,role="primary"]
  630. ----
  631. http
  632. .authorizeRequests((authorize) -> authorize
  633. .filterSecurityInterceptorOncePerRequest(true)
  634. .mvcMatchers("/app/**").hasRole("APP")
  635. // ...
  636. )
  637. // ...
  638. ----
  639. .Kotlin
  640. [source,kotlin,role="secondary"]
  641. ----
  642. http {
  643. authorizeRequests {
  644. filterSecurityInterceptorOncePerRequest = true
  645. authorize("/app/**", hasRole("APP"))
  646. // ...
  647. }
  648. }
  649. ----
  650. .Xml
  651. [source,xml,role="secondary"]
  652. ----
  653. <http once-per-request="true">
  654. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  655. <!-- ... -->
  656. </http>
  657. ----
  658. ====
  659. to:
  660. ====
  661. .Java
  662. [source,java,role="primary"]
  663. ----
  664. http
  665. .authorizeRequests((authorize) -> authorize
  666. .filterSecurityInterceptorOncePerRequest(true)
  667. .mvcMatchers("/app/**").hasRole("APP")
  668. // ...
  669. .anyRequest().denyAll()
  670. )
  671. // ...
  672. ----
  673. .Kotlin
  674. [source,kotlin,role="secondary"]
  675. ----
  676. http {
  677. authorizeRequests {
  678. filterSecurityInterceptorOncePerRequest = true
  679. authorize("/app/**", hasRole("APP"))
  680. // ...
  681. authorize(anyRequest, denyAll)
  682. }
  683. }
  684. ----
  685. .Xml
  686. [source,xml,role="secondary"]
  687. ----
  688. <http once-per-request="true">
  689. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  690. <!-- ... -->
  691. <intercept-url pattern="/**" access="denyAll"/>
  692. </http>
  693. ----
  694. ====
  695. If you have already migrated to `authorizeHttpRequests`, the recommended change is the same.
  696. ==== Switch to `AuthorizationManager`
  697. 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.
  698. Change:
  699. ====
  700. .Java
  701. [source,java,role="primary"]
  702. ----
  703. http
  704. .authorizeRequests((authorize) -> authorize
  705. .filterSecurityInterceptorOncePerRequest(true)
  706. .mvcMatchers("/app/**").hasRole("APP")
  707. // ...
  708. .anyRequest().denyAll()
  709. )
  710. // ...
  711. ----
  712. .Kotlin
  713. [source,kotlin,role="secondary"]
  714. ----
  715. http {
  716. authorizeRequests {
  717. filterSecurityInterceptorOncePerRequest = true
  718. authorize("/app/**", hasRole("APP"))
  719. // ...
  720. authorize(anyRequest, denyAll)
  721. }
  722. }
  723. ----
  724. .Xml
  725. [source,xml,role="secondary"]
  726. ----
  727. <http once-per-request="true">
  728. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  729. <!-- ... -->
  730. <intercept-url pattern="/**" access="denyAll"/>
  731. </http>
  732. ----
  733. ====
  734. to:
  735. ====
  736. .Java
  737. [source,java,role="primary"]
  738. ----
  739. http
  740. .authorizeHttpRequests((authorize) -> authorize
  741. .shouldFilterAllDispatcherTypes(false)
  742. .mvcMatchers("/app/**").hasRole("APP")
  743. // ...
  744. .anyRequest().denyAll()
  745. )
  746. // ...
  747. ----
  748. .Kotlin
  749. [source,kotlin,role="secondary"]
  750. ----
  751. http {
  752. authorizeHttpRequests {
  753. shouldFilterAllDispatcherTypes = false
  754. authorize("/app/**", hasRole("APP"))
  755. // ...
  756. authorize(anyRequest, denyAll)
  757. }
  758. }
  759. ----
  760. .Xml
  761. [source,xml,role="secondary"]
  762. ----
  763. <http filter-all-dispatcher-types="false" use-authorization-manager="true">
  764. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  765. <!-- ... -->
  766. <intercept-url pattern="/**" access="denyAll"/>
  767. </http>
  768. ----
  769. ====
  770. ==== Migrate SpEL expressions to `AuthorizationManager`
  771. For authorization rules, Java tends to be easier to test and maintain than SpEL.
  772. As such, `authorizeHttpRequests` does not have a method for declaring a `String` SpEL.
  773. Instead, you can implement your own `AuthorizationManager` implementation or use `WebExpressionAuthorizationManager`.
  774. For completeness, both options will be demonstrated.
  775. First, if you have the following SpEL:
  776. ====
  777. .Java
  778. [source,java,role="primary"]
  779. ----
  780. http
  781. .authorizeRequests((authorize) -> authorize
  782. .filterSecurityInterceptorOncePerRequest(true)
  783. .mvcMatchers("/complicated/**").access("hasRole('ADMIN') || hasAuthority('SCOPE_read')")
  784. // ...
  785. .anyRequest().denyAll()
  786. )
  787. // ...
  788. ----
  789. .Kotlin
  790. [source,kotlin,role="secondary"]
  791. ----
  792. http {
  793. authorizeRequests {
  794. filterSecurityInterceptorOncePerRequest = true
  795. authorize("/complicated/**", access("hasRole('ADMIN') || hasAuthority('SCOPE_read')"))
  796. // ...
  797. authorize(anyRequest, denyAll)
  798. }
  799. }
  800. ----
  801. ====
  802. Then you can compose your own `AuthorizationManager` with Spring Security authorization primitives like so:
  803. ====
  804. .Java
  805. [source,java,role="primary"]
  806. ----
  807. http
  808. .authorizeHttpRequests((authorize) -> authorize
  809. .shouldFilterAllDispatcherTypes(false)
  810. .mvcMatchers("/complicated/**").access(anyOf(hasRole("ADMIN"), hasAuthority("SCOPE_read"))
  811. // ...
  812. .anyRequest().denyAll()
  813. )
  814. // ...
  815. ----
  816. .Kotlin
  817. [source,kotlin,role="secondary"]
  818. ----
  819. http {
  820. authorizeHttpRequests {
  821. shouldFilterAllDispatcherTypes = false
  822. authorize("/complicated/**", access(anyOf(hasRole("ADMIN"), hasAuthority("SCOPE_read"))
  823. // ...
  824. authorize(anyRequest, denyAll)
  825. }
  826. }
  827. ----
  828. ====
  829. Or you can use `WebExpressionAuthorizationManager` in the following way:
  830. ====
  831. .Java
  832. [source,java,role="primary"]
  833. ----
  834. http
  835. .authorizeRequests((authorize) -> authorize
  836. .filterSecurityInterceptorOncePerRequest(true)
  837. .mvcMatchers("/complicated/**").access(
  838. new WebExpressionAuthorizationManager("hasRole('ADMIN') || hasAuthority('SCOPE_read')")
  839. )
  840. // ...
  841. .anyRequest().denyAll()
  842. )
  843. // ...
  844. ----
  845. .Kotlin
  846. [source,kotlin,role="secondary"]
  847. ----
  848. http {
  849. authorizeRequests {
  850. filterSecurityInterceptorOncePerRequest = true
  851. authorize("/complicated/**", access(
  852. WebExpressionAuthorizationManager("hasRole('ADMIN') || hasAuthority('SCOPE_read')"))
  853. )
  854. // ...
  855. authorize(anyRequest, denyAll)
  856. }
  857. }
  858. ----
  859. ====
  860. ==== Switch to filter all dispatcher types
  861. Spring Security 5.8 and earlier only xref:servlet/authorization/architecture.adoc[perform authorization] once per request.
  862. This means that dispatcher types like `FORWARD` and `INCLUDE` that run after `REQUEST` are not secured by default.
  863. It's recommended that Spring Security secure all dispatch types.
  864. As such, in 6.0, Spring Security changes this default.
  865. So, finally, change your authorization rules to filter all dispatcher types.
  866. To do this, change:
  867. ====
  868. .Java
  869. [source,java,role="primary"]
  870. ----
  871. http
  872. .authorizeHttpRequests((authorize) -> authorize
  873. .shouldFilterAllDispatcherTypes(false)
  874. .mvcMatchers("/app/**").hasRole("APP")
  875. // ...
  876. .anyRequest().denyAll()
  877. )
  878. // ...
  879. ----
  880. .Kotlin
  881. [source,kotlin,role="secondary"]
  882. ----
  883. http {
  884. authorizeHttpRequests {
  885. shouldFilterAllDispatcherTypes = false
  886. authorize("/app/**", hasRole("APP"))
  887. // ...
  888. authorize(anyRequest, denyAll)
  889. }
  890. }
  891. ----
  892. .Xml
  893. [source,xml,role="secondary"]
  894. ----
  895. <http filter-all-dispatcher-types="false" use-authorization-manager="true">
  896. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  897. <!-- ... -->
  898. <intercept-url pattern="/**" access="denyAll"/>
  899. </http>
  900. ----
  901. ====
  902. to:
  903. ====
  904. .Java
  905. [source,java,role="primary"]
  906. ----
  907. http
  908. .authorizeHttpRequests((authorize) -> authorize
  909. .shouldFilterAllDispatcherTypes(true)
  910. .mvcMatchers("/app/**").hasRole("APP")
  911. // ...
  912. .anyRequest().denyAll()
  913. )
  914. // ...
  915. ----
  916. .Kotlin
  917. [source,kotlin,role="secondary"]
  918. ----
  919. http {
  920. authorizeHttpRequests {
  921. shouldFilterAllDispatcherTypes = true
  922. authorize("/app/**", hasRole("APP"))
  923. // ...
  924. authorize(anyRequest, denyAll)
  925. }
  926. }
  927. ----
  928. .Xml
  929. [source,xml,role="secondary"]
  930. ----
  931. <http filter-all-dispatcher-types="true" use-authorization-manager="true">
  932. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  933. <!-- ... -->
  934. <intercept-url pattern="/**" access="denyAll"/>
  935. </http>
  936. ----
  937. ====
  938. [[servlet-authorizationmanager-requests-opt-out]]
  939. ==== Opt-out Steps
  940. In case you had trouble, take a look at these scenarios for optimal opt out behavior:
  941. ===== I cannot secure all dispatcher types
  942. If you cannot secure all dispatcher types, first try and declare which dispatcher types should not require authorization like so:
  943. ====
  944. .Java
  945. [source,java,role="primary"]
  946. ----
  947. http
  948. .authorizeHttpRequests((authorize) -> authorize
  949. .shouldFilterAllDispatcherTypes(true)
  950. .dispatcherTypeMatchers(FORWARD, INCLUDE).permitAll()
  951. .mvcMatchers("/app/**").hasRole("APP")
  952. // ...
  953. .anyRequest().denyAll()
  954. )
  955. // ...
  956. ----
  957. .Kotlin
  958. [source,kotlin,role="secondary"]
  959. ----
  960. http {
  961. authorizeHttpRequests {
  962. shouldFilterAllDispatcherTypes = true
  963. authorize(DispatcherTypeRequestMatcher(FORWARD, INCLUDE), permitAll)
  964. authorize("/app/**", hasRole("APP"))
  965. // ...
  966. authorize(anyRequest, denyAll)
  967. }
  968. }
  969. ----
  970. .Xml
  971. [source,xml,role="secondary"]
  972. ----
  973. <http filter-all-dispatcher-types="true" use-authorization-manager="true">
  974. <intercept-url request-matcher-ref="dispatchers"/>
  975. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  976. <!-- ... -->
  977. <intercept-url pattern="/**" access="denyAll"/>
  978. </http>
  979. <bean id="dispatchers" class="org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher">
  980. <constructor-arg>
  981. <util:list value-type="javax.servlet.DispatcherType">
  982. <value>FORWARD</value>
  983. <value>INCLUDE</value>
  984. </util:list>
  985. </constructor-arg>
  986. </bean>
  987. ----
  988. ====
  989. 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`:
  990. ====
  991. .Java
  992. [source,java,role="primary"]
  993. ----
  994. http
  995. .authorizeHttpRequests((authorize) -> authorize
  996. .filterAllDispatcherTypes(false)
  997. .mvcMatchers("/app/**").hasRole("APP")
  998. // ...
  999. )
  1000. // ...
  1001. ----
  1002. .Kotlin
  1003. [source,kotlin,role="secondary"]
  1004. ----
  1005. http {
  1006. authorizeHttpRequests {
  1007. filterAllDispatcherTypes = false
  1008. authorize("/messages/**", hasRole("APP"))
  1009. // ...
  1010. }
  1011. }
  1012. ----
  1013. .Xml
  1014. [source,xml,role="secondary"]
  1015. ----
  1016. <http filter-all-dispatcher-types="false" use-authorization-manager="true">
  1017. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1018. <!-- ... -->
  1019. </http>
  1020. ----
  1021. ====
  1022. or, if you are still using `authorizeRequests` or `use-authorization-manager="false"`, set `oncePerRequest` to `true`:
  1023. ====
  1024. .Java
  1025. [source,java,role="primary"]
  1026. ----
  1027. http
  1028. .authorizeRequests((authorize) -> authorize
  1029. .filterSecurityInterceptorOncePerRequest(true)
  1030. .mvcMatchers("/app/**").hasRole("APP")
  1031. // ...
  1032. )
  1033. // ...
  1034. ----
  1035. .Kotlin
  1036. [source,kotlin,role="secondary"]
  1037. ----
  1038. http {
  1039. authorizeRequests {
  1040. filterSecurityInterceptorOncePerRequest = true
  1041. authorize("/messages/**", hasRole("APP"))
  1042. // ...
  1043. }
  1044. }
  1045. ----
  1046. .Xml
  1047. [source,xml,role="secondary"]
  1048. ----
  1049. <http once-per-request="true" use-authorization-manager="false">
  1050. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1051. <!-- ... -->
  1052. </http>
  1053. ----
  1054. ====
  1055. ===== I cannot declare an authorization rule for all requests
  1056. 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:
  1057. ====
  1058. .Java
  1059. [source,java,role="primary"]
  1060. ----
  1061. http
  1062. .authorizeHttpReqeusts((authorize) -> authorize
  1063. .mvcMatchers("/app/*").hasRole("APP")
  1064. // ...
  1065. .anyRequest().permitAll()
  1066. )
  1067. ----
  1068. .Kotlin
  1069. [source,kotlin,role="secondary"]
  1070. ----
  1071. http {
  1072. authorizeHttpRequests {
  1073. authorize("/app*", hasRole("APP"))
  1074. // ...
  1075. authorize(anyRequest, permitAll)
  1076. }
  1077. }
  1078. ----
  1079. .Xml
  1080. [source,xml,role="secondary"]
  1081. ----
  1082. <http>
  1083. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1084. <!-- ... -->
  1085. <intercept-url pattern="/**" access="permitAll"/>
  1086. </http>
  1087. ----
  1088. ====
  1089. ===== I cannot migrate my SpEL or my `AccessDecisionManager`
  1090. 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.
  1091. 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.
  1092. Second, if you still need your custom `access-decision-manager-ref` or have some other reason to opt out of `AuthorizationManager`, do:
  1093. ====
  1094. .Xml
  1095. [source,xml,role="secondary"]
  1096. ----
  1097. <http use-authorization-manager="false">
  1098. <intercept-url pattern="/app/*" access="hasRole('APP')"/>
  1099. <!-- ... -->
  1100. </http>
  1101. ----
  1102. ====
  1103. == Reactive
  1104. === Use `AuthorizationManager` for Method Security
  1105. 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.
  1106. Should you run into trouble with making these changes, you can follow the
  1107. <<reactive-authorizationmanager-methods-opt-out,opt out steps>> at the end of this section.
  1108. 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.
  1109. [[reactive-change-to-useauthorizationmanager]]
  1110. ==== Change `useAuthorizationManager` to `true`
  1111. To opt in, change `useAuthorizationManager` to `true` like so:
  1112. ====
  1113. .Java
  1114. [source,java,role="primary"]
  1115. ----
  1116. @EnableReactiveMethodSecurity
  1117. ----
  1118. .Kotlin
  1119. [source,kotlin,role="secondary"]
  1120. ----
  1121. @EnableReactiveMethodSecurity
  1122. ----
  1123. ====
  1124. changes to:
  1125. ====
  1126. .Java
  1127. [source,java,role="primary"]
  1128. ----
  1129. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  1130. ----
  1131. .Kotlin
  1132. [source,kotlin,role="secondary"]
  1133. ----
  1134. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  1135. ----
  1136. ====
  1137. [[reactive-check-for-annotationconfigurationexceptions]]
  1138. ==== Check for ``AnnotationConfigurationException``s
  1139. `useAuthorizationManager` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
  1140. 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.
  1141. [[reactive-authorizationmanager-methods-opt-out]]
  1142. ==== Opt-out Steps
  1143. If you ran into trouble with `AuthorizationManager` for reactive method security, you can opt out by changing:
  1144. ====
  1145. .Java
  1146. [source,java,role="primary"]
  1147. ----
  1148. @EnableReactiveMethodSecurity
  1149. ----
  1150. .Kotlin
  1151. [source,kotlin,role="secondary"]
  1152. ----
  1153. @EnableReactiveMethodSecurity
  1154. ----
  1155. ====
  1156. to:
  1157. ====
  1158. .Java
  1159. [source,java,role="primary"]
  1160. ----
  1161. @EnableReactiveMethodSecurity(useAuthorizationManager = false)
  1162. ----
  1163. .Kotlin
  1164. [source,kotlin,role="secondary"]
  1165. ----
  1166. @EnableReactiveMethodSecurity(useAuthorizationManager = false)
  1167. ----
  1168. ====