migration.adoc 39 KB

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