Просмотр исходного кода

Document SAML Attribute Support

Issue gh-8661
Josh Cummings 5 лет назад
Родитель
Сommit
8cbdcfe756

+ 9 - 2
samples/boot/saml2login/src/main/java/sample/IndexController.java

@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2019 the original author or authors.
+ * Copyright 2002-2020 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,14 +16,21 @@
 
 package sample;
 
+import org.springframework.security.core.annotation.AuthenticationPrincipal;
+import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
 import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
 
 @Controller
 public class IndexController {
 
 	@GetMapping("/")
-	public String index() {
+	public String index(Model model,
+			@AuthenticationPrincipal Saml2AuthenticatedPrincipal principal) {
+		String emailAddress = principal.getFirstAttribute("emailAddress");
+		model.addAttribute("emailAddress", emailAddress);
+		model.addAttribute("userAttributes", principal.getAttributes());
 		return "index";
 	}
 }

+ 12 - 1
samples/boot/saml2login/src/main/resources/templates/index.html

@@ -19,6 +19,11 @@
 <head>
 	<title>Spring Security - SAML 2.0 Login</title>
 	<meta charset="utf-8" />
+    <style>
+        span, dt {
+            font-weight: bold;
+        }
+    </style>
 </head>
 <body>
 	<div>
@@ -30,6 +35,12 @@
         </a>
 	</div>
 	<h1>SAML 2.0 Login with Spring Security</h1>
-	<div>You are successfully logged in as <span sec:authentication="name"></span></div>
+	<p>You are successfully logged in as <span sec:authentication="name"></span></p>
+    <p>You're email address is <span th:text="${emailAddress}"></span></p>
+    <h2>All Your Attributes</h2>
+    <dl th:each="userAttribute : ${userAttributes}">
+        <dt th:text="${userAttribute.key}"></dt>
+        <dd th:text="${userAttribute.value}"></dd>
+    </dl>
 </body>
 </html>