authentication.adoc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. [[servlet-saml2login-authenticate-responses]]
  2. = Authenticating ``<saml2:Response>``s
  3. To verify SAML 2.0 Responses, Spring Security uses xref:servlet/saml2/login/overview.adoc#servlet-saml2login-authentication-saml2authenticationtokenconverter[`Saml2AuthenticationTokenConverter`] to populate the `Authentication` request and xref:servlet/saml2/login/overview.adoc#servlet-saml2login-architecture[`OpenSaml5AuthenticationProvider`] to authenticate it.
  4. You can configure this in a number of ways including:
  5. 1. Changing the way the `RelyingPartyRegistration` is Looked Up
  6. 2. Setting a clock skew to timestamp validation
  7. 3. Mapping the response to a list of `GrantedAuthority` instances
  8. 4. Customizing the strategy for validating assertions
  9. 5. Customizing the strategy for decrypting response and assertion elements
  10. To configure these, you'll use the `saml2Login#authenticationManager` method in the DSL.
  11. [[saml2-response-processing-endpoint]]
  12. == Changing the SAML Response Processing Endpoint
  13. The default endpoint is `+/login/saml2/sso/{registrationId}+`.
  14. You can change this in the DSL and in the associated metadata like so:
  15. [tabs]
  16. ======
  17. Java::
  18. +
  19. [source,java,role="primary"]
  20. ----
  21. @Bean
  22. SecurityFilterChain securityFilters(HttpSecurity http) throws Exception {
  23. http
  24. // ...
  25. .saml2Login((saml2) -> saml2.loginProcessingUrl("/saml2/login/sso"))
  26. // ...
  27. return http.build();
  28. }
  29. ----
  30. Kotlin::
  31. +
  32. [source,kotlin,role="secondary"]
  33. ----
  34. @Bean
  35. fun securityFilters(val http: HttpSecurity): SecurityFilterChain {
  36. http {
  37. // ...
  38. .saml2Login {
  39. loginProcessingUrl = "/saml2/login/sso"
  40. }
  41. // ...
  42. }
  43. return http.build()
  44. }
  45. ----
  46. ======
  47. and:
  48. [tabs]
  49. ======
  50. Java::
  51. +
  52. [source,java,role="primary"]
  53. ----
  54. relyingPartyRegistrationBuilder.assertionConsumerServiceLocation("/saml/SSO")
  55. ----
  56. Kotlin::
  57. +
  58. [source,kotlin,role="secondary"]
  59. ----
  60. relyingPartyRegistrationBuilder.assertionConsumerServiceLocation("/saml/SSO")
  61. ----
  62. ======
  63. [[relyingpartyregistrationresolver-apply]]
  64. == Changing `RelyingPartyRegistration` lookup
  65. By default, this converter will match against any associated `<saml2:AuthnRequest>` or any `registrationId` it finds in the URL.
  66. Or, if it cannot find one in either of those cases, then it attempts to look it up by the `<saml2:Response#Issuer>` element.
  67. There are a number of circumstances where you might need something more sophisticated, like if you are supporting `ARTIFACT` binding.
  68. In those cases, you can customize lookup through a custom `AuthenticationConverter`, which you can customize like so:
  69. [tabs]
  70. ======
  71. Java::
  72. +
  73. [source,java,role="primary"]
  74. ----
  75. @Bean
  76. SecurityFilterChain securityFilters(HttpSecurity http, AuthenticationConverter authenticationConverter) throws Exception {
  77. http
  78. // ...
  79. .saml2Login((saml2) -> saml2.authenticationConverter(authenticationConverter))
  80. // ...
  81. return http.build();
  82. }
  83. ----
  84. Kotlin::
  85. +
  86. [source,kotlin,role="secondary"]
  87. ----
  88. @Bean
  89. fun securityFilters(val http: HttpSecurity, val converter: AuthenticationConverter): SecurityFilterChain {
  90. http {
  91. // ...
  92. .saml2Login {
  93. authenticationConverter = converter
  94. }
  95. // ...
  96. }
  97. return http.build()
  98. }
  99. ----
  100. ======
  101. [[servlet-saml2login-opensamlauthenticationprovider-clockskew]]
  102. == Setting a Clock Skew
  103. It's not uncommon for the asserting and relying parties to have system clocks that aren't perfectly synchronized.
  104. For that reason, you can configure `OpenSaml5AuthenticationProvider.AssertionValidator` as follows:
  105. [tabs]
  106. ======
  107. Java::
  108. +
  109. [source,java,role="primary"]
  110. ----
  111. @Configuration
  112. @EnableWebSecurity
  113. public class SecurityConfig {
  114. @Bean
  115. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  116. OpenSaml5AuthenticationProvider authenticationProvider = new OpenSaml5AuthenticationProvider();
  117. AssertionValidator assertionValidator = AssertionValidator.builder()
  118. .clockSkew(Duration.ofMinutes(10)).build();
  119. authenticationProvider.setAssertionValidator(assertionValidator);
  120. http
  121. .authorizeHttpRequests((authorize) -> authorize
  122. .anyRequest().authenticated()
  123. )
  124. .saml2Login((saml2) -> saml2
  125. .authenticationManager(new ProviderManager(authenticationProvider))
  126. );
  127. return http.build();
  128. }
  129. }
  130. ----
  131. Kotlin::
  132. +
  133. [source,kotlin,role="secondary"]
  134. ----
  135. @Configuration @EnableWebSecurity
  136. class SecurityConfig {
  137. @Bean
  138. @Throws(Exception::class)
  139. fun filterChain(http: HttpSecurity): SecurityFilterChain {
  140. val authenticationProvider = OpenSaml5AuthenticationProvider()
  141. val assertionValidator = AssertionValidator.builder().clockSkew(Duration.ofMinutes(10)).build()
  142. authenticationProvider.setAssertionValidator(assertionValidator)
  143. http {
  144. authorizeHttpRequests {
  145. authorize(anyRequest, authenticated)
  146. }
  147. saml2Login {
  148. authenticationManager = ProviderManager(authenticationProvider)
  149. }
  150. }
  151. return http.build()
  152. }
  153. }
  154. ----
  155. ======
  156. == Converting an `Assertion` into an `Authentication`
  157. `OpenSamlXAuthenticationProvider#setResponseAuthenticationConverter` provides a way for you to change how it converts your assertion into an `Authentication` instance.
  158. You can set a custom converter in the following way:
  159. [tabs]
  160. ======
  161. Java::
  162. +
  163. [source,java,role="primary"]
  164. ----
  165. @Configuration
  166. @EnableWebSecurity
  167. public class SecurityConfig {
  168. @Autowired
  169. Converter<ResponseToken, Saml2Authentication> authenticationConverter;
  170. @Bean
  171. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  172. OpenSaml5AuthenticationProvider authenticationProvider = new OpenSaml5AuthenticationProvider();
  173. authenticationProvider.setResponseAuthenticationConverter(this.authenticationConverter);
  174. http
  175. .authorizeHttpRequests((authorize) -> authorize
  176. .anyRequest().authenticated())
  177. .saml2Login((saml2) -> saml2
  178. .authenticationManager(new ProviderManager(authenticationProvider))
  179. );
  180. return http.build();
  181. }
  182. }
  183. ----
  184. Kotlin::
  185. +
  186. [source,kotlin,role="secondary"]
  187. ----
  188. @Configuration
  189. @EnableWebSecurity
  190. open class SecurityConfig {
  191. @Autowired
  192. var authenticationConverter: Converter<ResponseToken, Saml2Authentication>? = null
  193. @Bean
  194. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  195. val authenticationProvider = OpenSaml5AuthenticationProvider()
  196. authenticationProvider.setResponseAuthenticationConverter(this.authenticationConverter)
  197. http {
  198. authorizeHttpRequests {
  199. authorize(anyRequest, authenticated)
  200. }
  201. saml2Login {
  202. authenticationManager = ProviderManager(authenticationProvider)
  203. }
  204. }
  205. return http.build()
  206. }
  207. }
  208. ----
  209. ======
  210. The ensuing examples all build off of this common construct to show you different ways this converter comes in handy.
  211. [[servlet-saml2login-opensamlauthenticationprovider-userdetailsservice]]
  212. == Coordinating with a `UserDetailsService`
  213. Or, perhaps you would like to include user details from a legacy `UserDetailsService`.
  214. In that case, the response authentication converter can come in handy, as can be seen below:
  215. [tabs]
  216. ======
  217. Java::
  218. +
  219. [source,java,role="primary"]
  220. ----
  221. @Component
  222. class MyUserDetailsResponseAuthenticationConverter implements Converter<ResponseToken, Saml2Authentication> {
  223. private final ResponseAuthenticationConverter delegate = new ResponseAuthenticationConverter();
  224. private final UserDetailsService userDetailsService;
  225. MyUserDetailsResponseAuthenticationConverter(UserDetailsService userDetailsService) {
  226. this.userDetailsService = userDetailsService;
  227. }
  228. @Override
  229. public Saml2Authentication convert(ResponseToken responseToken) {
  230. Saml2Authentication authentication = this.delegate.convert(responseToken); <1>
  231. UserDetails principal = this.userDetailsService.loadByUsername(username); <2>
  232. String saml2Response = authentication.getSaml2Response();
  233. Saml2ResponseAssertionAccessor assertion = new OpenSamlResponseAssertionAccessor(
  234. saml2Response, CollectionUtils.getFirst(response.getAssertions()));
  235. Collection<GrantedAuthority> authorities = principal.getAuthorities();
  236. return new Saml2AssertionAuthentication(userDetails, assertion, authorities); <3>
  237. }
  238. }
  239. ----
  240. Kotlin::
  241. +
  242. [source,kotlin,role="secondary"]
  243. ----
  244. @Component
  245. open class MyUserDetailsResponseAuthenticationConverter(val delegate: ResponseAuthenticationConverter,
  246. UserDetailsService userDetailsService): Converter<ResponseToken, Saml2Authentication> {
  247. @Override
  248. open fun convert(responseToken: ResponseToken): Saml2Authentication {
  249. val authentication = this.delegate.convert(responseToken) <1>
  250. val principal = this.userDetailsService.loadByUsername(username) <2>
  251. val saml2Response = authentication.getSaml2Response()
  252. val assertion = OpenSamlResponseAssertionAccessor(
  253. saml2Response, CollectionUtils.getFirst(response.getAssertions()))
  254. val authorities = principal.getAuthorities()
  255. return Saml2AssertionAuthentication(userDetails, assertion, authorities) <3>
  256. }
  257. }
  258. ----
  259. ======
  260. <1> First, call the default converter, which extracts attributes and authorities from the response
  261. <2> Second, call the xref:servlet/authentication/passwords/user-details-service.adoc#servlet-authentication-userdetailsservice[`UserDetailsService`] using the relevant information
  262. <3> Third, return an authentication that includes the user details
  263. [TIP]
  264. ====
  265. If your `UserDetailsService` returns a value that also implements `AuthenticatedPrincipal`, then you don't need a custom authentication implementation.
  266. ====
  267. [NOTE]
  268. It's not required to call ``OpenSaml5AuthenticationProvider``'s default authentication converter.
  269. It returns a `Saml2AuthenticatedPrincipal` containing the attributes it extracted from ``AttributeStatement``s as well as the single `ROLE_USER` authority.
  270. === Configuring the Principal Name
  271. Sometimes, the principal name is not in the `<saml2:NameID>` element.
  272. In that case, you can configure the `ResponseAuthenticationConverter` with a custom strategy like so:
  273. [tabs]
  274. ======
  275. Java::
  276. +
  277. [source,java,role="primary"]
  278. ----
  279. @Bean
  280. ResponseAuthenticationConverter authenticationConverter() {
  281. ResponseAuthenticationConverter authenticationConverter = new ResponseAuthenticationConverter();
  282. authenticationConverter.setPrincipalNameConverter((assertion) -> {
  283. // ... work with OpenSAML's Assertion object to extract the principal
  284. });
  285. return authenticationConverter;
  286. }
  287. ----
  288. Kotlin::
  289. +
  290. [source,kotlin,role="secondary"]
  291. ----
  292. @Bean
  293. fun authenticationConverter(): ResponseAuthenticationConverter {
  294. val authenticationConverter: ResponseAuthenticationConverter = ResponseAuthenticationConverter()
  295. authenticationConverter.setPrincipalNameConverter { assertion ->
  296. // ... work with OpenSAML's Assertion object to extract the principal
  297. }
  298. return authenticationConverter
  299. }
  300. ----
  301. ======
  302. === Configuring a Principal's Granted Authorities
  303. Spring Security automatically grants `ROLE_USER` when using `OpenSamlXAuhenticationProvider`.
  304. With `OpenSaml5AuthenticationProvider`, you can configure a different set of granted authorities like so:
  305. [tabs]
  306. ======
  307. Java::
  308. +
  309. [source,java,role="primary"]
  310. ----
  311. @Bean
  312. ResponseAuthenticationConverter authenticationConverter() {
  313. ResponseAuthenticationConverter authenticationConverter = new ResponseAuthenticationConverter();
  314. authenticationConverter.setPrincipalNameConverter((assertion) -> {
  315. // ... grant the needed authorities based on attributes in the assertion
  316. });
  317. return authenticationConverter;
  318. }
  319. ----
  320. Kotlin::
  321. +
  322. [source,kotlin,role="secondary"]
  323. ----
  324. @Bean
  325. fun authenticationConverter(): ResponseAuthenticationConverter {
  326. val authenticationConverter = ResponseAuthenticationConverter()
  327. authenticationConverter.setPrincipalNameConverter{ assertion ->
  328. // ... grant the needed authorities based on attributes in the assertion
  329. }
  330. return authenticationConverter
  331. }
  332. ----
  333. ======
  334. [[servlet-saml2login-opensamlauthenticationprovider-additionalvalidation]]
  335. == Performing Additional Response Validation
  336. `OpenSaml5AuthenticationProvider` validates the `Issuer` and `Destination` values right after decrypting the `Response`.
  337. You can customize the validation by extending the default validator concatenating with your own response validator, or you can replace it entirely with yours.
  338. For example, you can throw a custom exception with any additional information available in the `Response` object, like so:
  339. [source,java]
  340. ----
  341. OpenSaml5AuthenticationProvider provider = new OpenSaml5AuthenticationProvider();
  342. ResponseValidator responseValidator = ResponseValidator.withDefaults(myCustomValidator);
  343. provider.setResponseValidator(responseValidator);
  344. ----
  345. You can also customize which validation steps Spring Security should do.
  346. For example, if you want to skip `Response#InResponseTo` validation, you can call ``ResponseValidator``'s constructor, excluding `InResponseToValidator` from the list:
  347. [source,java]
  348. ----
  349. OpenSaml5AuthenticationProvider provider = new OpenSaml5AuthenticationProvider();
  350. ResponseValidator responseValidator = new ResponseValidator(new DestinationValidator(), new IssuerValidator());
  351. provider.setResponseValidator(responseValidator);
  352. ----
  353. [TIP]
  354. ====
  355. OpenSAML performs `Asssertion#InResponseTo` validation in its `BearerSubjectConfirmationValidator` class, which is configurable using <<_performing_additional_assertion_validation, setAssertionValidator>>.
  356. ====
  357. == Performing Additional Assertion Validation
  358. `OpenSaml5AuthenticationProvider` performs minimal validation on SAML 2.0 Assertions.
  359. After verifying the signature, it will:
  360. 1. Validate `<AudienceRestriction>` and `<DelegationRestriction>` conditions
  361. 2. Validate ``<SubjectConfirmation>``s, expect for any IP address information
  362. To perform additional validation, you can configure your own assertion validator that delegates to ``OpenSaml5AuthenticationProvider``'s default and then performs its own.
  363. [[servlet-saml2login-opensamlauthenticationprovider-onetimeuse]]
  364. For example, you can use OpenSAML's `OneTimeUseConditionValidator` to also validate a `<OneTimeUse>` condition, like so:
  365. [tabs]
  366. ======
  367. Java::
  368. +
  369. [source,java,role="primary"]
  370. ----
  371. OpenSaml5AuthenticationProvider provider = new OpenSaml5AuthenticationProvider();
  372. OneTimeUseConditionValidator validator = ...;
  373. AssertionValidator assertionValidator = AssertionValidator.builder()
  374. .conditionValidators((c) -> c.add(validator)).build();
  375. provider.setAssertionValidator(assertionValidator);
  376. ----
  377. Kotlin::
  378. +
  379. [source,kotlin,role="secondary"]
  380. ----
  381. val provider = OpenSaml5AuthenticationProvider()
  382. val validator: OneTimeUseConditionValidator = ...;
  383. val assertionValidator = AssertionValidator.builder()
  384. .conditionValidators { add(validator) }.build()
  385. provider.setAssertionValidator(assertionValidator)
  386. ----
  387. ======
  388. You can use this same builder to remove validators that you don't want to use like so:
  389. [tabs]
  390. ======
  391. Java::
  392. +
  393. [source,java,role="primary"]
  394. ----
  395. OpenSaml5AuthenticationProvider provider = new OpenSaml5AuthenticationProvider();
  396. AssertionValidator assertionValidator = AssertionValidator.builder()
  397. .conditionValidators((c) -> c.removeIf(AudienceRestrictionValidator.class::isInstance)).build();
  398. provider.setAssertionValidator(assertionValidator);
  399. ----
  400. Kotlin::
  401. +
  402. [source,kotlin,role="secondary"]
  403. ----
  404. val provider = new OpenSaml5AuthenticationProvider()
  405. val assertionValidator = AssertionValidator.builder()
  406. .conditionValidators {
  407. c: List<ConditionValidator> -> c.removeIf { it is AudienceRestrictionValidator }
  408. }.build()
  409. provider.setAssertionValidator(assertionValidator)
  410. ----
  411. ======
  412. [[servlet-saml2login-opensamlauthenticationprovider-decryption]]
  413. == Customizing Decryption
  414. Spring Security decrypts `<saml2:EncryptedAssertion>`, `<saml2:EncryptedAttribute>`, and `<saml2:EncryptedID>` elements automatically by using the decryption xref:servlet/saml2/login/overview.adoc#servlet-saml2login-rpr-credentials[`Saml2X509Credential` instances] registered in the xref:servlet/saml2/login/overview.adoc#servlet-saml2login-relyingpartyregistration[`RelyingPartyRegistration`].
  415. `OpenSaml5AuthenticationProvider` exposes xref:servlet/saml2/login/overview.adoc#servlet-saml2login-architecture[two decryption strategies].
  416. The response decrypter is for decrypting encrypted elements of the `<saml2:Response>`, like `<saml2:EncryptedAssertion>`.
  417. The assertion decrypter is for decrypting encrypted elements of the `<saml2:Assertion>`, like `<saml2:EncryptedAttribute>` and `<saml2:EncryptedID>`.
  418. You can replace ``OpenSaml5AuthenticationProvider``'s default decryption strategy with your own.
  419. For example, if you have a separate service that decrypts the assertions in a `<saml2:Response>`, you can use it instead like so:
  420. [tabs]
  421. ======
  422. Java::
  423. +
  424. [source,java,role="primary"]
  425. ----
  426. MyDecryptionService decryptionService = ...;
  427. OpenSaml5AuthenticationProvider provider = new OpenSaml5AuthenticationProvider();
  428. provider.setResponseElementsDecrypter((responseToken) -> decryptionService.decrypt(responseToken.getResponse()));
  429. ----
  430. Kotlin::
  431. +
  432. [source,kotlin,role="secondary"]
  433. ----
  434. val decryptionService: MyDecryptionService = ...
  435. val provider = OpenSaml5AuthenticationProvider()
  436. provider.setResponseElementsDecrypter { responseToken -> decryptionService.decrypt(responseToken.response) }
  437. ----
  438. ======
  439. If you are also decrypting individual elements in a `<saml2:Assertion>`, you can customize the assertion decrypter, too:
  440. [tabs]
  441. ======
  442. Java::
  443. +
  444. [source,java,role="primary"]
  445. ----
  446. provider.setAssertionElementsDecrypter((assertionToken) -> decryptionService.decrypt(assertionToken.getAssertion()));
  447. ----
  448. Kotlin::
  449. +
  450. [source,kotlin,role="secondary"]
  451. ----
  452. provider.setAssertionElementsDecrypter { assertionToken -> decryptionService.decrypt(assertionToken.assertion) }
  453. ----
  454. ======
  455. NOTE: There are two separate decrypters since assertions can be signed separately from responses.
  456. Trying to decrypt a signed assertion's elements before signature verification may invalidate the signature.
  457. If your asserting party signs the response only, then it's safe to decrypt all elements using only the response decrypter.
  458. [[servlet-saml2login-authenticationmanager-custom]]
  459. == Using a Custom Authentication Manager
  460. [[servlet-saml2login-opensamlauthenticationprovider-authenticationmanager]]
  461. Of course, the `authenticationManager` DSL method can be also used to perform a completely custom SAML 2.0 authentication.
  462. This authentication manager should expect a `Saml2AuthenticationToken` object containing the SAML 2.0 Response XML data.
  463. [tabs]
  464. ======
  465. Java::
  466. +
  467. [source,java,role="primary"]
  468. ----
  469. @Configuration
  470. @EnableWebSecurity
  471. public class SecurityConfig {
  472. @Bean
  473. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  474. AuthenticationManager authenticationManager = new MySaml2AuthenticationManager(...);
  475. http
  476. .authorizeHttpRequests((authorize) -> authorize
  477. .anyRequest().authenticated()
  478. )
  479. .saml2Login((saml2) -> saml2
  480. .authenticationManager(authenticationManager)
  481. )
  482. ;
  483. return http.build();
  484. }
  485. }
  486. ----
  487. Kotlin::
  488. +
  489. [source,kotlin,role="secondary"]
  490. ----
  491. @Configuration
  492. @EnableWebSecurity
  493. open class SecurityConfig {
  494. @Bean
  495. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  496. val customAuthenticationManager: AuthenticationManager = MySaml2AuthenticationManager(...)
  497. http {
  498. authorizeHttpRequests {
  499. authorize(anyRequest, authenticated)
  500. }
  501. saml2Login {
  502. authenticationManager = customAuthenticationManager
  503. }
  504. }
  505. return http.build()
  506. }
  507. }
  508. ----
  509. ======
  510. [[servlet-saml2login-authenticatedprincipal]]
  511. == Using `Saml2AuthenticatedPrincipal`
  512. With the relying party correctly configured for a given asserting party, it's ready to accept assertions.
  513. Once the relying party validates an assertion, the result is a `Saml2Authentication` with a `Saml2AuthenticatedPrincipal`.
  514. This means that you can access the principal in your controller like so:
  515. [tabs]
  516. ======
  517. Java::
  518. +
  519. [source,java,role="primary"]
  520. ----
  521. @Controller
  522. public class MainController {
  523. @GetMapping("/")
  524. public String index(@AuthenticationPrincipal Saml2AuthenticatedPrincipal principal, Model model) {
  525. String email = principal.getFirstAttribute("email");
  526. model.setAttribute("email", email);
  527. return "index";
  528. }
  529. }
  530. ----
  531. Kotlin::
  532. +
  533. [source,kotlin,role="secondary"]
  534. ----
  535. @Controller
  536. class MainController {
  537. @GetMapping("/")
  538. fun index(@AuthenticationPrincipal principal: Saml2AuthenticatedPrincipal, model: Model): String {
  539. val email = principal.getFirstAttribute<String>("email")
  540. model.setAttribute("email", email)
  541. return "index"
  542. }
  543. }
  544. ----
  545. ======
  546. [TIP]
  547. Because the SAML 2.0 specification allows for each attribute to have multiple values, you can either call `getAttribute` to get the list of attributes or `getFirstAttribute` to get the first in the list.
  548. `getFirstAttribute` is quite handy when you know that there is only one value.