2
0

whats-new.adoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. [[new]]
  2. = What's New in Spring Security 6.4
  3. Spring Security 6.4 provides a number of new features.
  4. Below are the highlights of the release, or you can view https://github.com/spring-projects/spring-security/releases[the release notes] for a detailed listing of each feature and bug fix.
  5. == Deprecation Notices
  6. As we get closer to Spring Security 7, it's important to stay up to date on deprecations.
  7. As such, this section points out deprecations in the 6.4 release.
  8. * *Method Security* - `AuthorizationManager#check` is deprecated in favor of `AuthorizationManager#authorize`.
  9. This is primarily to allow the return type to be an interface instead of a concrete class.
  10. If you are invoking `AuthorizationManager#check`, please invoke `AuthorizationManager#authorize` instead.
  11. +
  12. Relatedly, `AuthorizationEventPublisher#publishEvent` that takes an `AuthorizationDecision` is deprecated in favor of a method of the same name that takes an `AuthorizationResult` interface instead.
  13. * *Method Security* - `PrePostTemplateDefaults` is deprecated in favor of the more generic `AnnotationTemplateExpressionDefaults` as there is now meta-annotation property support for `@AuthenticationPrincipal` and `@CurrentSecurityContext` as well.
  14. If you are constructing a `PrePostTemplateDefaults`, change this out for an `AnnotationTemplateExpressionDefaults`.
  15. * *OAuth 2.0* - `NimbusOpaqueTokenIntrospector` has been deprecated in favor of `SpringOpaqueTokenIntrospector` in order to remove Spring Security OAuth 2.0 Resource Server's reliance on the `oidc-oauth2-sdk` package.
  16. If you are constructing a `NimbusOpaqueTokenIntrospector`, replace it with ``SpringOpaqueTokenIntrospector``'s constructor
  17. * *OAuth 2.0* - `DefaultAuthorizationCodeTokenResponseClient`, `DefaultClientCredentialsTokenResponseClient`, `DefaultJwtBearerTokenResponseClient`, `DefaultPasswordTokenResponseClient`, `DefaultRefreshTokenTokenResponseClient`, and `DefaultTokenExchangeTokenResponseClient` are deprecated in favor of their `RestClient` equivalents.
  18. +
  19. Relatedly,`JwtBearerGrantRequestEntityConverter`, `OAuth2AuthorizationCodeGrantRequestEntityConverter`, `OAuth2ClientCredentialsGrantRequestEntityConverter`, `OAuth2PasswordGrantRequestEntityConverter`, `OAuth2RefreshTokenGrantRequestEntityConverter` are deprecated in favor of providing an instance of `DefaultOAuth2TokenRequestParametersConverter` to one of the above token response clients
  20. +
  21. For example, if you have the following arrangement:
  22. +
  23. [source,java]
  24. ----
  25. private static class MyCustomConverter
  26. extends AbstractOAuth2AuthorizationGrantRequestEntityConverter<OAuth2AuthorizationCodeGrantRequest> {
  27. @Override
  28. protected MultiValueMap<String, String> createParameters
  29. (OAuth2AuthorizationCodeGrantRequest request) {
  30. MultiValueMap<String, String> parameters = super.createParameters(request);
  31. parameters.add("custom", "value");
  32. return parameters;
  33. }
  34. }
  35. @Bean
  36. OAuth2AccessTokenResponseClient authorizationCode() {
  37. DefaultAuthorizationCodeTokenResponseClient client =
  38. new DefaultAuthorizationCodeTokenResponseClient();
  39. Converter<AuthorizationCodeGrantRequest, RequestEntity<?>> entityConverter =
  40. new OAuth2AuthorizationCodeGrantRequestEntityConverter();
  41. entityConverter.setParametersConverter(new MyCustomConverter());
  42. client.setRequestEntityConverter(entityConverter);
  43. return client;
  44. }
  45. ----
  46. +
  47. This configuration is deprecated since it uses `DefaultAuthorizationCodeTokenResponseClient` and `OAuth2AuthorizationCodeGrantRequestEntityConverter`.
  48. The recommended configuration is now:
  49. +
  50. [source,java]
  51. ----
  52. private static class MyCustomConverter implements Converter<OAuth2AuthorizationCodeGrantRequest, Map<String, String>> {
  53. @Override
  54. public MultiValueMap<String, String> convert(OAuth2AuthorizeCodeGrantRequest request) {
  55. MultiValueMap<String, String> parameters = OAuth2AuthorizationCodeGrantRequest.defaultParameters(request);
  56. parameters.add("custom", "value");
  57. return parameters;
  58. }
  59. }
  60. @Bean
  61. OAuth2AccessTokenResponseClient authorizationCode() {
  62. RestClientAuthorizationCodeTokenResponseClient client =
  63. new RestClientAuthorizationCodeTokenResponseClient();
  64. client.setParametersConverter(new MyCustomConverter());
  65. return client;
  66. }
  67. ----
  68. * *SAML 2.0* - Unversioned OpenSAML implementations of Spring Security SAML 2.0 Service Provider's interfaces have been deprecated in favor of versioned ones.
  69. For example, `OpenSamlAuthenticationTokenConverter` is now replaced by `OpenSaml4AuthenticationTokenConverter` and `OpenSaml5AuthenticationTokenConverter`.
  70. If you are constructing one of these deprecated versions, please replace it with the one that corresponds to the OpenSAML version you are using.
  71. * *SAML 2.0* - Methods surrounding `AssertingPartyDetails` are deprecated in favor of equivalent methods that use the `AssertingPartyMetadata` interface.
  72. * *LDAP* - Usages of `DistinguishedName` are now deprecated in order to align with Spring LDAP's deprecations
  73. == One-Time Token Login
  74. * Spring Security now xref:servlet/authentication/onetimetoken.adoc[supports One-Time Token Login] via the `oneTimeTokenLogin()` DSL, including xref:servlet/authentication/onetimetoken.adoc#customize-generate-consume-token[JDBC support].
  75. == Passkeys
  76. Spring Security now has xref:servlet/authentication/passkeys.adoc[Passkeys] support.
  77. == Method Security
  78. * All xref:servlet/authorization/method-security.adoc#meta-annotations[method security annotations] now support {spring-framework-api-url}org/springframework/core/annotation/AliasFor.html[Framework's `@AliasFor`]
  79. * `@AuthenticationPrincipal` and `@CurrentSecurityContext` now support xref:servlet/authorization/method-security.adoc#_templating_meta_annotation_expressions[annotation templates].
  80. +
  81. This means that you can now use Spring's meta-annotation support like so:
  82. +
  83. [tabs]
  84. ======
  85. Java::
  86. +
  87. [source,java,role="primary"]
  88. ----
  89. @Target(TargetType.TYPE)
  90. @Retention(RetentionPolicy.RUNTIME)
  91. @AuthenticationPrincipal("claims['{claim}']")
  92. @interface CurrentUsername {
  93. String claim() default "sub";
  94. }
  95. // ...
  96. @GetMapping
  97. public String method(@CurrentUsername("username") String username) {
  98. // ...
  99. }
  100. ----
  101. Kotlin::
  102. +
  103. [source,kotlin,role="secondary"]
  104. ----
  105. annotation CurrentUsername(val claim: String = "sub")
  106. // ...
  107. @GetMapping
  108. fun method(@CurrentUsername("username") val username: String): String {
  109. // ...
  110. }
  111. ----
  112. ======
  113. * https://github.com/spring-projects/spring-security/issues/13490[Several] https://github.com/spring-projects/spring-security/issues/13234[improvements] https://github.com/spring-projects/spring-security/issues/15097[were made] to align Security's annotation search with ``AbstractFallbackMethodSecurityMetadataSource``'s algorithm.
  114. This aids in migration from earlier versions of Spring Security.
  115. * Native applications can now xref:servlet/authorization/method-security.adoc#authorize-return-object-aot[use `@AuthorizeReturnObject`]
  116. * Native applications can now xref:servlet/authorization/method-security.adoc#pre-post-authorize-aot[reference beans in `@PreAuthorize` and `@PostAuthorize`]
  117. * `SecurityAnnotationScanners` offers https://github.com/spring-projects/spring-security/issues/15700[a convenient API] for scanning for Security annotations and for adding Security's selection and templating features to custom annotations
  118. == OAuth 2.0
  119. * `oauth2Login()` now accepts https://github.com/spring-projects/spring-security/pull/15237[`OAuth2AuthorizationRequestResolver` as a `@Bean`]
  120. * `ClientRegistrations` now supports externally obtained configuration
  121. * Added `loginPage()` to DSL in reactive `oauth2Login()`
  122. * OIDC Back-Channel support now accepts https://github.com/spring-projects/spring-security/issues/15003[logout tokens of type `logout+jwt`]
  123. * `RestClient` can now be xref:servlet/oauth2/index.adoc#oauth2-client-access-protected-resources[configured] with `OAuth2ClientHttpRequestInterceptor` to xref:servlet/oauth2/index.adoc#oauth2-client-accessing-protected-resources-example[make protected resources requests]
  124. * Added `RestClient`-based implementations of `OAuth2AccessTokenResponseClient` for more consistent configuration of access token requests.
  125. +
  126. To opt-in to using `RestClient` support, simply publish a bean for each grant type as in the following example:
  127. +
  128. [tabs]
  129. ======
  130. Java::
  131. +
  132. [source,java,role="primary"]
  133. ----
  134. @Configuration
  135. public class SecurityConfig {
  136. @Bean
  137. public OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeAccessTokenResponseClient() {
  138. return new RestClientAuthorizationCodeTokenResponseClient();
  139. }
  140. @Bean
  141. public OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenAccessTokenResponseClient() {
  142. return new RestClientRefreshTokenTokenResponseClient();
  143. }
  144. @Bean
  145. public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsAccessTokenResponseClient() {
  146. return new RestClientClientCredentialsTokenResponseClient();
  147. }
  148. @Bean
  149. public OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerAccessTokenResponseClient() {
  150. return new RestClientJwtBearerTokenResponseClient();
  151. }
  152. @Bean
  153. public OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeAccessTokenResponseClient() {
  154. return new RestClientTokenExchangeTokenResponseClient();
  155. }
  156. }
  157. ----
  158. Kotlin::
  159. +
  160. [source,kotlin,role="secondary"]
  161. ----
  162. @Configuration
  163. class SecurityConfig {
  164. @Bean
  165. fun authorizationCodeAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> {
  166. return RestClientAuthorizationCodeTokenResponseClient()
  167. }
  168. @Bean
  169. fun refreshTokenAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> {
  170. return RestClientRefreshTokenTokenResponseClient()
  171. }
  172. @Bean
  173. fun clientCredentialsAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> {
  174. return RestClientClientCredentialsTokenResponseClient()
  175. }
  176. @Bean
  177. fun jwtBearerAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> {
  178. return RestClientJwtBearerTokenResponseClient()
  179. }
  180. @Bean
  181. fun tokenExchangeAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> {
  182. return RestClientTokenExchangeTokenResponseClient()
  183. }
  184. }
  185. ----
  186. ======
  187. * Token Exchange now https://github.com/spring-projects/spring-security/issues/15534[supports refresh tokens]
  188. == SAML 2.0
  189. * Added xref:servlet/saml2/opensaml.adoc[OpenSAML 5 Support].
  190. Now you can use either OpenSAML 4 or OpenSAML 5; by default, Spring Security will select the write implementations based on what's on your classpath.
  191. * Using EntityIDs for the `registrationId` is simplified.
  192. +
  193. A common pattern is to identify asserting parties by their `entityID`.
  194. In previous versions, this required directly configuring `OpenSamlAuthenticationRequestResolver`.
  195. Now, the request resolver looks by default for the `registrationId` https://github.com/spring-projects/spring-security/issues/15017[as a request parameter] in addition to looking for it in the path.
  196. This allows you to use `RelyingPartyRegistrations` or `OpenSaml4/5AssertingPartyMetadataRepository` without also needing to modify the `registrationId` values or customize the request resolver.
  197. +
  198. Relatedly, you can now configure your `authenticationRequestUri` to xref:servlet/saml2/login/authentication-requests.adoc#configuring-authentication-request-uri[contain a query parameter]
  199. * Asserting Parties can now be refreshed in the background according to the metadata's expiry.
  200. +
  201. For example, you can now use xref:servlet/saml2/metadata.adoc#using-assertingpartymetadatarepository[`OpenSaml5AssertingPartyMetadataRepository`] to do:
  202. +
  203. [tabs]
  204. ======
  205. Java::
  206. +
  207. [source,java,role="primary"]
  208. ----
  209. @Component
  210. public class RefreshableRelyingPartyRegistrationRepository implements IterableRelyingPartyRegistrationRepository {
  211. private final AssertingPartyMetadataRepository assertingParties = OpenSaml5AssertingPartyMetadataRepository
  212. .fromTrustedMetadataLocation("https://idp.example.org").build();
  213. @Override
  214. public RelyingPartyRegistration findByRegistrationId(String registrationId) {
  215. AssertingPartyMetadata assertingParty = this.assertingParties.findByEntityId(registrationId);
  216. return RelyingPartyRegistration.withAssertingPartyMetadata(assertingParty)
  217. // relying party configurations
  218. .build();
  219. }
  220. // ...
  221. }
  222. ----
  223. Kotlin::
  224. +
  225. [source,kotlin,role="secondary"]
  226. ----
  227. @Component
  228. open class RefreshableRelyingPartyRegistrationRepository: IterableRelyingPartyRegistrationRepository {
  229. private val assertingParties: AssertingPartyMetadataRepository = OpenSaml5AssertingPartyMetadataRepository
  230. .fromTrustedMetadataLocation("https://idp.example.org").build()
  231. override fun findByRegistrationId(String registrationId): RelyingPartyRegistration {
  232. val assertingParty = this.assertingParties.findByEntityId(registrationId)
  233. return RelyingPartyRegistration.withAssertingPartyMetadata(assertingParty)
  234. // relying party configurations
  235. .build()
  236. }
  237. // ...
  238. }
  239. ----
  240. ======
  241. +
  242. This implementation also supports the validation of a metadata's signature.
  243. * You can now sign https://github.com/spring-projects/spring-security/pull/14916[relying party metadata]
  244. * `RelyingPartyRegistrationRepository` results can now be javadoc:org.springframework.security.saml2.provider.service.registration.CachingRelyingPartyRegistrationRepository[cached].
  245. This is helpful if you want to defer the loading of the registration values til after application startup.
  246. It is also helpful if you want to control when metadata gets refreshed via Spring Cache.
  247. * To align with the SAML 2.0 standard, the metadata endpoint now https://github.com/spring-projects/spring-security/issues/15147[uses the `application/samlmetadata+xml` MIME type]
  248. == Web
  249. * CSRF BREACH tokens are now https://github.com/spring-projects/spring-security/issues/15187[more consistent]
  250. * The Remember Me cookie now is https://github.com/spring-projects/spring-security/pull/15203[more customizable]
  251. * Security Filter Chain finds more invalid configurations.
  252. For example, a filter chain declared after an any-request filter chain is invalid since it will never be invoked:
  253. +
  254. [tabs]
  255. ======
  256. Java::
  257. +
  258. [source,java,role="primary"]
  259. ----
  260. @Bean
  261. @Order(0)
  262. SecurityFilterChain api(HttpSecurity http) throws Exception {
  263. http
  264. // implicit securityMatcher("/**")
  265. .authorizeHttpRequests(...)
  266. .httpBasic(...)
  267. return http.build();
  268. }
  269. @Bean
  270. @Order(1)
  271. SecurityFilterChain app(HttpSecurity http) throws Exception {
  272. http
  273. .securityMatcher("/app/**")
  274. .authorizeHttpRequests(...)
  275. .formLogin(...)
  276. return http.build();
  277. }
  278. ----
  279. Kotlin::
  280. +
  281. [source,kotlin,role="secondary"]
  282. ----
  283. @Bean
  284. @Order(0)
  285. fun api(val http: HttpSecurity): SecurityFilterChain {
  286. http {
  287. authorizeHttpRequests {
  288. // ...
  289. }
  290. }
  291. return http.build()
  292. }
  293. @Bean
  294. @Order(1)
  295. fun app(val http: HttpSecurity): SecurityFilterChain {
  296. http {
  297. securityMatcher("/app/**")
  298. authorizeHttpRequests {
  299. // ...
  300. }
  301. }
  302. return http.build()
  303. }
  304. ----
  305. ======
  306. You can read more https://github.com/spring-projects/spring-security/issues/15220[in the related ticket].
  307. * `ServerHttpSecurity` now https://github.com/spring-projects/spring-security/issues/15974[picks up `ServerWebExchangeFirewall` as a `@Bean`]
  308. == Observability
  309. Observability now supports xref:servlet/integrations/observability.adoc#observability-tracing-disable[toggling authorization, authentication, and request observations separately]
  310. For example, to turn off filter chain observations, you can publish a `@Bean` like this one:
  311. [tabs]
  312. ======
  313. Java::
  314. +
  315. [source,java,role="primary"]
  316. ----
  317. @Bean
  318. SecurityObservationSettings allSpringSecurityObservations() {
  319. return SecurityObservationSettings.withDefaults()
  320. .shouldObserveFilterChains(false).build();
  321. }
  322. ----
  323. Kotlin::
  324. +
  325. [source,kotlin,role="secondary"]
  326. ----
  327. @Bean
  328. fun allSpringSecurityObservations(): SecurityObservationSettings {
  329. return SecurityObservationSettings.builder()
  330. .shouldObserveFilterChains(false).build()
  331. }
  332. ----
  333. ======
  334. == Kotlin
  335. * The Kotlin DSL now supports https://github.com/spring-projects/spring-security/issues/14935[SAML 2.0] and https://github.com/spring-projects/spring-security/issues/15171[`GrantedAuthorityDefaults`] and https://github.com/spring-projects/spring-security/issues/15136[`RoleHierarchy`] ``@Bean``s
  336. * `@PreFilter` and `@PostFilter` are https://github.com/spring-projects/spring-security/pull/15095[now supported] in Kotlin
  337. * The Kotlin Reactive DSL now supports https://github.com/spring-projects/spring-security/pull/15013[`SecurityContextRepository`]
  338. == Acl
  339. * `AclAuthorizationStrategyImpl` now https://github.com/spring-projects/spring-security/issues/4186[supports `RoleHierarchy`]