Compare commits

...

2 Commits

Author SHA1 Message Date
bd002fcbae feat: add readme 2026-03-16 11:57:32 +01:00
aa504dd881 feat: use oauth 2026-03-16 11:51:46 +01:00
7 changed files with 52 additions and 12 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ target/
!**/src/main/**/target/ !**/src/main/**/target/
!**/src/test/**/target/ !**/src/test/**/target/
.kotlin .kotlin
.env
### IntelliJ IDEA ### ### IntelliJ IDEA ###
.idea/modules.xml .idea/modules.xml

View File

@ -52,6 +52,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

3
readme.md Normal file
View File

@ -0,0 +1,3 @@
# TODO
- When trying to add a product while not logged, it redirects to the login page but product infos aren't restored

View File

@ -18,11 +18,13 @@ public class SecurityConfig {
.requestMatchers("/add", "/delete/**").authenticated() // Seul l'admin ajoute/supprime .requestMatchers("/add", "/delete/**").authenticated() // Seul l'admin ajoute/supprime
.anyRequest().authenticated() .anyRequest().authenticated()
) )
.formLogin((form) -> form .oauth2Login((oauth2) -> oauth2
.defaultSuccessUrl("/", true) .defaultSuccessUrl("/", true)
.permitAll()
) )
.logout((logout) -> logout.permitAll()); .logout((logout) -> logout
.logoutSuccessUrl("/")
.permitAll()
);
return http.build(); return http.build();
} }

View File

@ -6,6 +6,8 @@ import org.springframework.beans.factory.annotation.Autowired;
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.*; import org.springframework.web.bind.annotation.*;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import java.util.List; import java.util.List;
@ -16,8 +18,9 @@ public class ProductController {
private ProductRepository productRepository; // Spring gère SQL tout seul ! private ProductRepository productRepository; // Spring gère SQL tout seul !
@GetMapping("/") @GetMapping("/")
public String listProducts(Model model) { public String listProducts(Model model, @AuthenticationPrincipal OidcUser user) {
model.addAttribute("products", productRepository.findAll()); model.addAttribute("products", productRepository.findAll());
model.addAttribute("username", user != null ? user.getPreferredUsername() : null);
return "index"; // Ça va chercher src/main/resources/templates/index.html return "index"; // Ça va chercher src/main/resources/templates/index.html
} }

View File

@ -5,3 +5,11 @@ spring.jpa.hibernate.ddl-auto=validate
server.port=8083 server.port=8083
spring.security.user.name=admin spring.security.user.name=admin
spring.security.user.password=${ADMIN_PASS} spring.security.user.password=${ADMIN_PASS}
spring.security.oauth2.client.registration.keycloak.client-id=${KEYCLOAK_CLIENT_ID}
spring.security.oauth2.client.registration.keycloak.client-secret=${KEYCLOAK_CLIENT_SECRET}
spring.security.oauth2.client.provider.keycloak.issuer-uri=${KEYCLOAK_ISSUER_URI}
spring.security.oauth2.client.registration.keycloak.scope=openid,profile
spring.security.oauth2.client.registration.keycloak.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.keycloak.redirect-uri={baseUrl}/login/oauth2/code/keycloak
spring.security.oauth2.client.provider.keycloak.user-name-attribute=preferred_username

View File

@ -17,6 +17,7 @@
</h1> </h1>
<p class="text-gray-500 text-sm">Surveillance des prix en temps réel</p> <p class="text-gray-500 text-sm">Surveillance des prix en temps réel</p>
</div> </div>
<div class="flex items-center gap-3">
<div class="flex items-center gap-3 bg-gray-900 border border-gray-800 p-2 rounded-2xl shadow-inner"> <div class="flex items-center gap-3 bg-gray-900 border border-gray-800 p-2 rounded-2xl shadow-inner">
<span class="relative flex h-3 w-3"> <span class="relative flex h-3 w-3">
<span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span> <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
@ -24,6 +25,24 @@
</span> </span>
<span class="text-xs font-mono text-gray-400 uppercase tracking-widest">System Active</span> <span class="text-xs font-mono text-gray-400 uppercase tracking-widest">System Active</span>
</div> </div>
<!-- Not logged in -->
<a th:if="${username == null}" href="/oauth2/authorization/keycloak"
class="bg-blue-600 hover:bg-blue-500 text-white font-bold py-2 px-4 rounded-2xl shadow-lg text-xs uppercase tracking-widest transition-colors">
Login
</a>
<!-- Logged in -->
<div th:if="${username != null}" class="flex items-center gap-2">
<span class="bg-gray-900 border border-gray-800 text-gray-300 font-bold py-2 px-4 rounded-2xl text-xs uppercase tracking-widest"
th:text="${username}">
</span>
<a href="/logout"
class="bg-red-600 hover:bg-red-500 text-white font-bold py-2 px-4 rounded-2xl shadow-lg text-xs uppercase tracking-widest transition-colors">
Logout
</a>
</div>
</div>
</header> </header>
<section class="bg-gray-900 p-1 rounded-3xl border border-gray-800 shadow-2xl mb-12"> <section class="bg-gray-900 p-1 rounded-3xl border border-gray-800 shadow-2xl mb-12">