2
0

config.adoc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. = Configuration Migrations
  2. The following steps relate to changes around how to configure `HttpSecurity`, `WebSecurity`, and `AuthenticationManager`.
  3. [[use-new-requestmatchers]]
  4. == Use the new `requestMatchers` methods
  5. In Spring Security 5.8, the {security-api-url}org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.html#antMatchers(java.lang.String...)[`antMatchers`], {security-api-url}org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.html#mvcMatchers(java.lang.String...)[`mvcMatchers`], and {security-api-url}org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.html#regexMatchers(java.lang.String...)[`regexMatchers`] methods were deprecated in favor of new xref:servlet/authorization/authorize-http-requests.adoc#_request_matchers[`requestMatchers` methods].
  6. The new `requestMatchers` methods were added xref:servlet/authorization/authorize-http-requests.adoc[to `authorizeHttpRequests`], `authorizeRequests`, CSRF configuration, `WebSecurityCustomizer` and any other places that had the specialized `RequestMatcher` methods.
  7. The deprecated methods are removed in Spring Security 6.
  8. These new methods have more secure defaults since they choose the most appropriate `RequestMatcher` implementation for your application.
  9. In summary, the new methods choose the `MvcRequestMatcher` implementation if your application has Spring MVC in the classpath, falling back to the `AntPathRequestMatcher` implementation if Spring MVC is not present (aligning the behavior with the Kotlin equivalent methods).
  10. To start using the new methods, you can replace the deprecated methods with the new ones. For example, the following application configuration:
  11. ====
  12. .Java
  13. [source,java,role="primary"]
  14. ----
  15. @Configuration
  16. @EnableWebSecurity
  17. public class SecurityConfig {
  18. @Bean
  19. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  20. http
  21. .authorizeHttpRequests((authz) -> authz
  22. .antMatchers("/api/admin/**").hasRole("ADMIN")
  23. .antMatchers("/api/user/**").hasRole("USER")
  24. .anyRequest().authenticated()
  25. );
  26. return http.build();
  27. }
  28. }
  29. ----
  30. ====
  31. can be changed to:
  32. ====
  33. .Java
  34. [source,java,role="primary"]
  35. ----
  36. @Configuration
  37. @EnableWebSecurity
  38. public class SecurityConfig {
  39. @Bean
  40. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  41. http
  42. .authorizeHttpRequests((authz) -> authz
  43. .requestMatchers("/api/admin/**").hasRole("ADMIN")
  44. .requestMatchers("/api/user/**").hasRole("USER")
  45. .anyRequest().authenticated()
  46. );
  47. return http.build();
  48. }
  49. }
  50. ----
  51. ====
  52. If you have Spring MVC in the classpath and are using the `mvcMatchers` methods, you can replace it with the new methods and Spring Security will choose the `MvcRequestMatcher` implementation for you.
  53. The following configuration:
  54. ====
  55. .Java
  56. [source,java,role="primary"]
  57. ----
  58. @Configuration
  59. @EnableWebSecurity
  60. @EnableWebMvc
  61. public class SecurityConfig {
  62. @Bean
  63. SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  64. http
  65. .authorizeHttpRequests((authz) -> authz
  66. .mvcMatchers("/admin/**").hasRole("ADMIN")
  67. .anyRequest().authenticated()
  68. );
  69. return http.build();
  70. }
  71. }
  72. ----
  73. ====
  74. is equivalent to:
  75. ====
  76. .Java
  77. [source,java,role="primary"]
  78. ----
  79. @Configuration
  80. @EnableWebSecurity
  81. @EnableWebMvc
  82. public class SecurityConfig {
  83. @Bean
  84. SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  85. http
  86. .authorizeHttpRequests((authz) -> authz
  87. .requestMatchers("/admin/**").hasRole("ADMIN")
  88. .anyRequest().authenticated()
  89. );
  90. return http.build();
  91. }
  92. }
  93. ----
  94. ====
  95. If you are customizing the `servletPath` property of the `MvcRequestMatcher`, you can now use the `MvcRequestMatcher.Builder` to create `MvcRequestMatcher` instances that share the same servlet path:
  96. ====
  97. .Java
  98. [source,java,role="primary"]
  99. ----
  100. @Configuration
  101. @EnableWebSecurity
  102. @EnableWebMvc
  103. public class SecurityConfig {
  104. @Bean
  105. SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  106. http
  107. .authorizeHttpRequests((authz) -> authz
  108. .mvcMatchers("/admin").servletPath("/path").hasRole("ADMIN")
  109. .mvcMatchers("/user").servletPath("/path").hasRole("USER")
  110. .anyRequest().authenticated()
  111. );
  112. return http.build();
  113. }
  114. }
  115. ----
  116. ====
  117. The code above can be rewritten using the `MvcRequestMatcher.Builder` and the `requestMatchers` method:
  118. ====
  119. .Java
  120. [source,java,role="primary"]
  121. ----
  122. @Configuration
  123. @EnableWebSecurity
  124. @EnableWebMvc
  125. public class SecurityConfig {
  126. @Bean
  127. SecurityFilterChain securityFilterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
  128. MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector).servletPath("/path");
  129. http
  130. .authorizeHttpRequests((authz) -> authz
  131. .requestMatchers(mvcMatcherBuilder.pattern("/admin")).hasRole("ADMIN")
  132. .requestMatchers(mvcMatcherBuilder.pattern("/user")).hasRole("USER")
  133. .anyRequest().authenticated()
  134. );
  135. return http.build();
  136. }
  137. }
  138. ----
  139. ====
  140. If you are having problem with the new `requestMatchers` methods, you can always switch back to the `RequestMatcher` implementation that you were using.
  141. For example, if you still want to use `AntPathRequestMatcher` and `RegexRequestMatcher` implementations, you can use the `requestMatchers` method that accepts a `RequestMatcher` instance:
  142. ====
  143. .Java
  144. [source,java,role="primary"]
  145. ----
  146. import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.antMatcher;
  147. import static org.springframework.security.web.util.matcher.RegexRequestMatcher.regexMatcher;
  148. @Configuration
  149. @EnableWebSecurity
  150. public class SecurityConfig {
  151. @Bean
  152. SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  153. http
  154. .authorizeHttpRequests((authz) -> authz
  155. .requestMatchers(antMatcher("/user/**")).hasRole("USER")
  156. .requestMatchers(antMatcher(HttpMethod.POST, "/user/**")).hasRole("ADMIN")
  157. .requestMatchers(regexMatcher(".*\\?x=y")).hasRole("SPECIAL") // matches /any/path?x=y
  158. .anyRequest().authenticated()
  159. );
  160. return http.build();
  161. }
  162. }
  163. ----
  164. ====
  165. Note that the above sample uses static factory methods from {security-api-url}org/springframework/security/web/util/matcher/AntPathRequestMatcher.html[`AntPathRequestMatcher`] and {security-api-url}org/springframework/security/web/util/matcher/RegexRequestMatcher.html[`RegexRequestMatcher`] to improve readability.
  166. If you are using the `WebSecurityCustomizer` interface, you can replace the deprecated `antMatchers` methods:
  167. ====
  168. .Java
  169. [source,java,role="primary"]
  170. ----
  171. @Bean
  172. public WebSecurityCustomizer webSecurityCustomizer() {
  173. return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
  174. }
  175. ----
  176. ====
  177. with their `requestMatchers` counterparts:
  178. ====
  179. .Java
  180. [source,java,role="primary"]
  181. ----
  182. @Bean
  183. public WebSecurityCustomizer webSecurityCustomizer() {
  184. return (web) -> web.ignoring().requestMatchers("/ignore1", "/ignore2");
  185. }
  186. ----
  187. ====
  188. The same way, if you are customizing the CSRF configuration to ignore some paths, you can replace the deprecated methods with the `requestMatchers` methods:
  189. ====
  190. .Java
  191. [source,java,role="primary"]
  192. ----
  193. @Bean
  194. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  195. http
  196. .csrf((csrf) -> csrf
  197. .ignoringAntMatchers("/no-csrf")
  198. );
  199. return http.build();
  200. }
  201. ----
  202. ====
  203. can be changed to:
  204. ====
  205. .Java
  206. [source,java,role="primary"]
  207. ----
  208. @Bean
  209. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  210. http
  211. .csrf((csrf) -> csrf
  212. .ignoringRequestMatchers("/no-csrf")
  213. );
  214. return http.build();
  215. }
  216. ----
  217. ====
  218. [[use-new-security-matchers]]
  219. == Use the new `securityMatchers` methods
  220. In Spring Security 5.8, the `antMatchers`, `mvcMatchers` and `requestMatchers` methods from `HttpSecurity` were deprecated in favor of new `securityMatchers` methods.
  221. Note that these methods are not the same from `authorizeHttpRequests` methods <<use-new-requestmatchers,which were deprecated>> in favor of the `requestMatchers` methods.
  222. However, the `securityMatchers` methods are similar to the `requestMatchers` methods in the sense that they will choose the most appropriate `RequestMatcher` implementation for your application.
  223. In summary, the new methods choose the `MvcRequestMatcher` implementation if your application has Spring MVC in the classpath, falling back to the `AntPathRequestMatcher` implementation if Spring MVC is not present (aligning the behavior with the Kotlin equivalent methods).
  224. Another reason for adding the `securityMatchers` methods is to avoid confusion with the `requestMatchers` methods from `authorizeHttpRequests`.
  225. The following configuration:
  226. ====
  227. .Java
  228. [source,java,role="primary"]
  229. ----
  230. @Bean
  231. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  232. http
  233. .antMatcher("/api/**", "/app/**")
  234. .authorizeHttpRequests((authz) -> authz
  235. .requestMatchers("/api/admin/**").hasRole("ADMIN")
  236. .anyRequest().authenticated()
  237. );
  238. return http.build();
  239. }
  240. ----
  241. ====
  242. can be rewritten using the `securityMatchers` methods:
  243. ====
  244. .Java
  245. [source,java,role="primary"]
  246. ----
  247. @Bean
  248. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  249. http
  250. .securityMatcher("/api/**", "/app/**")
  251. .authorizeHttpRequests((authz) -> authz
  252. .requestMatchers("/api/admin/**").hasRole("ADMIN")
  253. .anyRequest().authenticated()
  254. );
  255. return http.build();
  256. }
  257. ----
  258. ====
  259. If you are using a custom `RequestMatcher` in your `HttpSecurity` configuration:
  260. ====
  261. .Java
  262. [source,java,role="primary"]
  263. ----
  264. @Bean
  265. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  266. http
  267. .requestMatcher(new MyCustomRequestMatcher())
  268. .authorizeHttpRequests((authz) -> authz
  269. .requestMatchers("/api/admin/**").hasRole("ADMIN")
  270. .anyRequest().authenticated()
  271. );
  272. return http.build();
  273. }
  274. public class MyCustomRequestMatcher implements RequestMatcher {
  275. // ...
  276. }
  277. ----
  278. ====
  279. you can do the same using `securityMatcher`:
  280. ====
  281. .Java
  282. [source,java,role="primary"]
  283. ----
  284. @Bean
  285. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  286. http
  287. .securityMatcher(new MyCustomRequestMatcher())
  288. .authorizeHttpRequests((authz) -> authz
  289. .requestMatchers("/api/admin/**").hasRole("ADMIN")
  290. .anyRequest().authenticated()
  291. );
  292. return http.build();
  293. }
  294. public class MyCustomRequestMatcher implements RequestMatcher {
  295. // ...
  296. }
  297. ----
  298. ====
  299. If you are combining multiple `RequestMatcher` implementations in your `HttpSecurity` configuration:
  300. ====
  301. .Java
  302. [source,java,role="primary"]
  303. ----
  304. @Bean
  305. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  306. http
  307. .requestMatchers((matchers) -> matchers
  308. .antMatchers("/api/**", "/app/**")
  309. .mvcMatchers("/admin/**")
  310. .requestMatchers(new MyCustomRequestMatcher())
  311. )
  312. .authorizeHttpRequests((authz) -> authz
  313. .requestMatchers("/admin/**").hasRole("ADMIN")
  314. .anyRequest().authenticated()
  315. );
  316. return http.build();
  317. }
  318. ----
  319. ====
  320. you can change it by using `securityMatchers`:
  321. ====
  322. .Java
  323. [source,java,role="primary"]
  324. ----
  325. @Bean
  326. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  327. http
  328. .securityMatchers((matchers) -> matchers
  329. .requestMatchers("/api/**", "/app/**", "/admin/**")
  330. .requestMatchers(new MyCustomRequestMatcher())
  331. )
  332. .authorizeHttpRequests((authz) -> authz
  333. .requestMatchers("/admin/**").hasRole("ADMIN")
  334. .anyRequest().authenticated()
  335. );
  336. return http.build();
  337. }
  338. ----
  339. ====
  340. If you are having problems with the `securityMatchers` methods choosing the `RequestMatcher` implementation for you, you can always choose the `RequestMatcher` implementation yourself:
  341. ====
  342. .Java
  343. [source,java,role="primary"]
  344. ----
  345. import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.antMatcher;
  346. @Bean
  347. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  348. http
  349. .securityMatcher(antMatcher("/api/**"), antMatcher("/app/**"))
  350. .authorizeHttpRequests((authz) -> authz
  351. .requestMatchers(antMatcher("/api/admin/**")).hasRole("ADMIN")
  352. .anyRequest().authenticated()
  353. );
  354. return http.build();
  355. }
  356. ----
  357. ====
  358. == Stop Using `WebSecurityConfigurerAdapter`
  359. === Publish a `SecurityFilterChain` Bean
  360. Spring Security 5.4 introduced the capability to publish a `SecurityFilterChain` bean instead of extending `WebSecurityConfigurerAdapter`.
  361. In 6.0, `WebSecurityConfigurerAdapter` is removed.
  362. To prepare for this change, you can replace constructs like:
  363. ====
  364. .Java
  365. [source,java,role="primary"]
  366. ----
  367. @Configuration
  368. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  369. @Override
  370. protected void configure(HttpSecurity http) throws Exception {
  371. http
  372. .authorizeHttpRequests((authorize) -> authorize
  373. .anyRequest().authenticated()
  374. )
  375. .httpBasic(withDefaults());
  376. }
  377. }
  378. ----
  379. .Kotlin
  380. [source,kotlin,role="secondary"]
  381. ----
  382. @Configuration
  383. open class SecurityConfiguration: WebSecurityConfigurerAdapter() {
  384. @Override
  385. override fun configure(val http: HttpSecurity) {
  386. http {
  387. authorizeHttpRequests {
  388. authorize(anyRequest, authenticated)
  389. }
  390. httpBasic {}
  391. }
  392. }
  393. }
  394. ----
  395. ====
  396. with:
  397. ====
  398. .Java
  399. [source,java,role="primary"]
  400. ----
  401. @Configuration
  402. public class SecurityConfiguration {
  403. @Bean
  404. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  405. http
  406. .authorizeHttpRequests((authorize) -> authorize
  407. .anyRequest().authenticated()
  408. )
  409. .httpBasic(withDefaults());
  410. return http.build();
  411. }
  412. }
  413. ----
  414. .Kotlin
  415. [source,kotlin,role="secondary"]
  416. ----
  417. @Configuration
  418. open class SecurityConfiguration {
  419. @Bean
  420. fun filterChain(http: HttpSecurity): SecurityFilterChain {
  421. http {
  422. authorizeHttpRequests {
  423. authorize(anyRequest, authenticated)
  424. }
  425. httpBasic {}
  426. }
  427. return http.build()
  428. }
  429. }
  430. ----
  431. ====
  432. === Publish a `WebSecurityCustomizer` Bean
  433. Spring Security 5.4 https://github.com/spring-projects/spring-security/issues/8978[introduced `WebSecurityCustomizer`] to replace `configure(WebSecurity web)` in `WebSecurityConfigurerAdapter`.
  434. To prepare for its removal, you can replace code like the following:
  435. ====
  436. .Java
  437. [source,java,role="primary"]
  438. ----
  439. @Configuration
  440. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  441. @Override
  442. public void configure(WebSecurity web) {
  443. web.ignoring().antMatchers("/ignore1", "/ignore2");
  444. }
  445. }
  446. ----
  447. .Kotlin
  448. [source,kotlin,role="secondary"]
  449. ----
  450. @Configuration
  451. open class SecurityConfiguration: WebSecurityConfigurerAdapter() {
  452. override fun configure(val web: WebSecurity) {
  453. web.ignoring().antMatchers("/ignore1", "/ignore2")
  454. }
  455. }
  456. ----
  457. ====
  458. with:
  459. ====
  460. .Java
  461. [source,java,role="primary"]
  462. ----
  463. @Configuration
  464. public class SecurityConfiguration {
  465. @Bean
  466. public WebSecurityCustomizer webSecurityCustomizer() {
  467. return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
  468. }
  469. }
  470. ----
  471. .Kotlin
  472. [source,kotlin,role="secondary"]
  473. ----
  474. @Configuration
  475. open class SecurityConfiguration {
  476. @Bean
  477. fun webSecurityCustomizer(): WebSecurityCustomizer {
  478. return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2")
  479. }
  480. }
  481. ----
  482. ====
  483. === Publish an `AuthenticationManager` Bean
  484. As part of `WebSecurityConfigurerAdapeter` removal, `configure(AuthenticationManagerBuilder)` is also removed.
  485. Preparing for its removal will differ based on your reason for using it.
  486. ==== LDAP Authentication
  487. If you are using `auth.ldapAuthentication()` for xref:servlet/authentication/passwords/ldap.adoc[LDAP authentication support], you can replace:
  488. ====
  489. .Java
  490. [source,java,role="primary"]
  491. ----
  492. @Configuration
  493. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  494. @Override
  495. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  496. auth
  497. .ldapAuthentication()
  498. .userDetailsContextMapper(new PersonContextMapper())
  499. .userDnPatterns("uid={0},ou=people")
  500. .contextSource()
  501. .port(0);
  502. }
  503. }
  504. ----
  505. .Kotlin
  506. [source,kotlin,role="secondary"]
  507. ----
  508. @Configuration
  509. open class SecurityConfiguration: WebSecurityConfigurerAdapter() {
  510. override fun configure(auth: AuthenticationManagerBuilder) {
  511. auth
  512. .ldapAuthentication()
  513. .userDetailsContextMapper(PersonContextMapper())
  514. .userDnPatterns("uid={0},ou=people")
  515. .contextSource()
  516. .port(0)
  517. }
  518. }
  519. ----
  520. ====
  521. with:
  522. ====
  523. .Java
  524. [source,java,role="primary"]
  525. ----
  526. @Configuration
  527. public class SecurityConfiguration {
  528. @Bean
  529. public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
  530. EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean =
  531. EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
  532. contextSourceFactoryBean.setPort(0);
  533. return contextSourceFactoryBean;
  534. }
  535. @Bean
  536. AuthenticationManager ldapAuthenticationManager(BaseLdapPathContextSource contextSource) {
  537. LdapBindAuthenticationManagerFactory factory =
  538. new LdapBindAuthenticationManagerFactory(contextSource);
  539. factory.setUserDnPatterns("uid={0},ou=people");
  540. factory.setUserDetailsContextMapper(new PersonContextMapper());
  541. return factory.createAuthenticationManager();
  542. }
  543. }
  544. ----
  545. .Kotlin
  546. [source,kotlin,role="secondary"]
  547. ----
  548. @Configuration
  549. open class SecurityConfiguration {
  550. @Bean
  551. fun contextSourceFactoryBean(): EmbeddedLdapServerContextSourceFactoryBean {
  552. val contextSourceFactoryBean: EmbeddedLdapServerContextSourceFactoryBean =
  553. EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer()
  554. contextSourceFactoryBean.setPort(0)
  555. return contextSourceFactoryBean
  556. }
  557. @Bean
  558. fun ldapAuthenticationManager(val contextSource: BaseLdapPathContextSource): AuthenticationManager {
  559. val factory = LdapBindAuthenticationManagerFactory(contextSource)
  560. factory.setUserDnPatterns("uid={0},ou=people")
  561. factory.setUserDetailsContextMapper(PersonContextMapper())
  562. return factory.createAuthenticationManager()
  563. }
  564. }
  565. ----
  566. ====
  567. ==== JDBC Authentication
  568. If you are using `auth.jdbcAuthentication()` for xref:servlet/authentication/passwords/jdbc.adoc[JDBC Authentication support], you can replace:
  569. ====
  570. .Java
  571. [source,java,role="primary"]
  572. ----
  573. @Configuration
  574. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  575. @Bean
  576. public DataSource dataSource() {
  577. return new EmbeddedDatabaseBuilder()
  578. .setType(EmbeddedDatabaseType.H2)
  579. .build();
  580. }
  581. @Override
  582. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  583. UserDetails user = User.withDefaultPasswordEncoder()
  584. .username("user")
  585. .password("password")
  586. .roles("USER")
  587. .build();
  588. auth.jdbcAuthentication()
  589. .withDefaultSchema()
  590. .dataSource(this.dataSource)
  591. .withUser(user);
  592. }
  593. }
  594. ----
  595. .Kotlin
  596. [source,kotlin,role="secondary"]
  597. ----
  598. @Configuration
  599. open class SecurityConfiguration: WebSecurityConfigurerAdapter() {
  600. @Bean
  601. fun dataSource(): DataSource {
  602. return EmbeddedDatabaseBuilder()
  603. .setType(EmbeddedDatabaseType.H2)
  604. .build()
  605. }
  606. override fun configure(val auth: AuthenticationManagerBuilder) {
  607. UserDetails user = User.withDefaultPasswordEncoder()
  608. .username("user")
  609. .password("password")
  610. .roles("USER")
  611. .build()
  612. auth.jdbcAuthentication()
  613. .withDefaultSchema()
  614. .dataSource(this.dataSource)
  615. .withUser(user)
  616. }
  617. }
  618. ----
  619. ====
  620. with:
  621. ====
  622. .Java
  623. [source,java,role="primary"]
  624. ----
  625. @Configuration
  626. public class SecurityConfiguration {
  627. @Bean
  628. public DataSource dataSource() {
  629. return new EmbeddedDatabaseBuilder()
  630. .setType(EmbeddedDatabaseType.H2)
  631. .addScript(JdbcDaoImpl.DEFAULT_USER_SCHEMA_DDL_LOCATION)
  632. .build();
  633. }
  634. @Bean
  635. public UserDetailsManager users(DataSource dataSource) {
  636. UserDetails user = User.withDefaultPasswordEncoder()
  637. .username("user")
  638. .password("password")
  639. .roles("USER")
  640. .build();
  641. JdbcUserDetailsManager users = new JdbcUserDetailsManager(dataSource);
  642. users.createUser(user);
  643. return users;
  644. }
  645. }
  646. ----
  647. .Kotlin
  648. [source,kotlin,role="secondary"]
  649. ----
  650. @Configuration
  651. open class SecurityConfiguration {
  652. @Bean
  653. fun dataSource(): DataSource {
  654. return EmbeddedDatabaseBuilder()
  655. .setType(EmbeddedDatabaseType.H2)
  656. .addScript(JdbcDaoImpl.DEFAULT_USER_SCHEMA_DDL_LOCATION)
  657. .build()
  658. }
  659. @Bean
  660. fun users(val dataSource: DataSource): UserDetailsManager {
  661. val user = User.withDefaultPasswordEncoder()
  662. .username("user")
  663. .password("password")
  664. .roles("USER")
  665. .build()
  666. val users = JdbcUserDetailsManager(dataSource)
  667. users.createUser(user)
  668. return users
  669. }
  670. }
  671. ----
  672. ====
  673. ==== In-Memory Authentication
  674. If you are using `auth.inMemoryAuthentication()` for xref:servlet/authentication/passwords/in-memory.adoc[In-Memory Authentication support], you can replace:
  675. ====
  676. .Java
  677. [source,java,role="primary"]
  678. ----
  679. @Configuration
  680. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  681. @Override
  682. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  683. UserDetails user = User.withDefaultPasswordEncoder()
  684. .username("user")
  685. .password("password")
  686. .roles("USER")
  687. .build();
  688. auth.inMemoryAuthentication()
  689. .withUser(user);
  690. }
  691. }
  692. ----
  693. .Kotlin
  694. [source,kotlin,role="secondary"]
  695. ----
  696. @Configuration
  697. open class SecurityConfiguration: WebSecurityConfigurerAdapter() {
  698. override fun configure(val auth: AuthenticationManagerBuilder) {
  699. val user = User.withDefaultPasswordEncoder()
  700. .username("user")
  701. .password("password")
  702. .roles("USER")
  703. .build()
  704. auth.inMemoryAuthentication()
  705. .withUser(user)
  706. }
  707. }
  708. ----
  709. ====
  710. with:
  711. ====
  712. .Java
  713. [source,java,role="primary"]
  714. ----
  715. @Configuration
  716. public class SecurityConfiguration {
  717. @Bean
  718. public InMemoryUserDetailsManager userDetailsService() {
  719. UserDetails user = User.withDefaultPasswordEncoder()
  720. .username("user")
  721. .password("password")
  722. .roles("USER")
  723. .build();
  724. return new InMemoryUserDetailsManager(user);
  725. }
  726. }
  727. ----
  728. .Kotlin
  729. [source,kotlin,role="secondary"]
  730. ----
  731. @Configuration
  732. open class SecurityConfiguration {
  733. @Bean
  734. fun userDetailsService(): InMemoryUserDetailsManager {
  735. UserDetails user = User.withDefaultPasswordEncoder()
  736. .username("user")
  737. .password("password")
  738. .roles("USER")
  739. .build()
  740. return InMemoryUserDetailsManager(user)
  741. }
  742. }
  743. ----
  744. ====
  745. ==== Other Scenarios
  746. If you are using `AuthenticationManagerBuilder` for something more sophisticated, you can xref:servlet/authentication/architecture.adoc#servlet-authentication-authenticationmanager[publish your own `AuthenticationManager` `@Bean`] or wire an `AuthenticationManager` instance into the `HttpSecurity` DSL with {security-api-url}org/springframework/security/config/annotation/web/builders/HttpSecurity.html#authenticationManager(org.springframework.security.authentication.AuthenticationManager)[`HttpSecurity#authenticationManager`].