Commit bdc16fb8 by Patryk Czarnik

dodanie security

parent 59a375fb
...@@ -46,6 +46,10 @@ ...@@ -46,6 +46,10 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Dodatkowe zależności, aby używać JSP w Springu --> <!-- Dodatkowe zależności, aby używać JSP w Springu -->
<dependency> <dependency>
......
package sklep.controller; package sklep.controller;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -27,4 +28,13 @@ public class RootController { ...@@ -27,4 +28,13 @@ public class RootController {
return "show_time"; return "show_time";
} }
@GetMapping("/whoami")
public String whoAmI(Authentication authentication, Model model) {
if(authentication != null && authentication.isAuthenticated()) {
model.addAttribute("userName", authentication.getName());
model.addAttribute("authorities", authentication.getAuthorities());
}
return "whoami";
}
} }
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="jakarta.tags.core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<c:choose>
<c:when test="${not empty userName}">
<p>Jesteś zalogowany jako <strong>${userName}</strong>.</p>
<p>Twoje uprawnienia (<i>authorities</i>):</p>
<ul>
<c:forEach var="auth" items="${authorities}">
<li>${auth}</li>
</c:forEach>
</ul>
</c:when>
<c:otherwise>
<p>Nie jesteś zalogowany.</p>
</c:otherwise>
</c:choose>
<div><a href="/">Powrót do spisu treści</a></div>
<div><a href="/logout">Wyloguj się</a></div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment