Skip to content

Commit

Permalink
add login and logout buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimprinzbach committed Jun 23, 2024
1 parent 47be914 commit 2e5e92f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@
<artifactId>lombok</artifactId>
<scope>annotationProcessor</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.baloise.platformplanedemoapp;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collection;

@Controller
public class ThymeleafIndexController {

Expand All @@ -15,7 +18,13 @@ public String index(final Model model) {
SecurityContext context = SecurityContextHolder.getContext();
if(context.getAuthentication().getPrincipal() instanceof DefaultOidcUser user) {
String userName = user.getName();
model.addAttribute("currentUser", userName);
String mail = user.getPreferredUsername();
model.addAttribute("currentUser", userName + " (" + mail + ")");
Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
model.addAttribute("role", authorities.iterator().next());
model.addAttribute("issuer", "Azure EntraID");
model.addAttribute("mail", mail);
model.addAttribute("name", userName);
} else {
model.addAttribute("currentUser", "Anonymous");
}
Expand Down
55 changes: 51 additions & 4 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,61 @@
</header>
<bal-stage color="green" size="small">
<bal-stage-body>
<bal-heading>Welcome to the</bal-heading>
<bal-heading> Control Plane Demo< App</bal-heading>
<bal-heading level="h1">Welcome to the Control Plane Demo App</bal-heading>
</bal-stage-body>
</bal-stage>
<main class="container mt-xx-large">
<bal-content>
<bal-label>Hello Platform Plane</bal-label>
<bal-text>Platform Plane Demo App</bal-text>
<bal-heading level="h2">Platform Plane Demo App</bal-heading>
<bal-card sec:authorize="isAnonymous()" fullheight="true" color="purple" flat="true">
<bal-card-content>
<div class="flex justify-content-start flex-direction-column">
<bal-heading level="h4" space="bottom">Hello Anonymous User!</bal-heading>
<bal-text class="mb-normal">You are currently not logged in. Use either the button on the top right corner or click down below!</bal-text>
<bal-button-group position="center" class="mt-auto">
<bal-button expanded="true" href="/oauth2/authorization/azure">Sign in</bal-button>
</bal-button-group>
</div>
</bal-card-content>
</bal-card>
<bal-list sec:authorize="isAuthenticated()" border="">
<bal-list-item>
<bal-list-item-icon>
<bal-badge color="green" size="large" icon="info-circle">1</bal-badge>
</bal-list-item-icon>
<bal-list-item-content>
<bal-list-item-title>Name</bal-list-item-title>
<bal-list-item-subtitle th:text="${name}"></bal-list-item-subtitle>
</bal-list-item-content>
</bal-list-item>
<bal-list-item>
<bal-list-item-icon>
<bal-badge color="yellow" size="large" icon="web">1</bal-badge>
</bal-list-item-icon>
<bal-list-item-content>
<bal-list-item-title>Mail</bal-list-item-title>
<bal-list-item-subtitle th:text="${mail}"></bal-list-item-subtitle>
</bal-list-item-content>
</bal-list-item>
<bal-list-item>
<bal-list-item-icon>
<bal-badge size="large" icon="document">1</bal-badge>
</bal-list-item-icon>
<bal-list-item-content>
<bal-list-item-title>Role</bal-list-item-title>
<bal-list-item-subtitle th:text="${role}"></bal-list-item-subtitle>
</bal-list-item-content>
</bal-list-item>
<bal-list-item>
<bal-list-item-icon>
<bal-badge color="purple" size="large" icon="plus">1</bal-badge>
</bal-list-item-icon>
<bal-list-item-content>
<bal-list-item-title>Issuer</bal-list-item-title>
<bal-list-item-subtitle th:text="${issuer}"></bal-list-item-subtitle>
</bal-list-item-content>
</bal-list-item>
</bal-list>
</bal-content>
</main>
<bal-footer hide-links hide-language-selection/>
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
</bal-navbar-menu-start>
<bal-navbar-menu-end>
<bal-button-group>
<bal-tag color="info" th:text="${currentUser}"></bal-tag>
<bal-tag sec:authorize="isAuthenticated()" color="success" th:text="${currentUser}"></bal-tag>
<bal-button sec:authorize="isAnonymous()" color="tertiary-green" href="/oauth2/authorization/azure">Sign in</bal-button>
<form action="#" th:action="@{/logout}" method="post">
<button class="button is-tertiary-red" type="submit" sec:authorize="isAuthenticated()">Sign out</button>
</form>
</bal-button-group>
</bal-navbar-menu-end>
</bal-navbar-menu>
Expand Down

0 comments on commit 2e5e92f

Please sign in to comment.