interceptor credit user
This commit is contained in:
@@ -1,12 +1,29 @@
|
||||
package fr.eni.enchere.controllers;
|
||||
|
||||
import fr.eni.enchere.interceptor.UserInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
|
||||
private final UserInterceptor userInterceptor;
|
||||
|
||||
@Autowired
|
||||
public MvcConfig(UserInterceptor userInterceptor) {
|
||||
this.userInterceptor = userInterceptor;
|
||||
}
|
||||
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/login").setViewName("security/login");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(userInterceptor);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package fr.eni.enchere.interceptor;
|
||||
|
||||
import fr.eni.enchere.bll.UserService;
|
||||
import fr.eni.enchere.bo.UserProfil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Component
|
||||
public class UserInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
@Autowired
|
||||
public UserInterceptor(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
if (modelAndView != null && modelAndView.getModelMap() != null) {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && authentication.isAuthenticated() && !authentication.getName().equals("anonymousUser")) {
|
||||
UserProfil user = this.userService.utilisateurByName(authentication.getName());
|
||||
System.out.println(user);
|
||||
modelAndView.getModelMap().addAttribute("user", user.getCredit());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
//package fr.eni.enchere.interceptor;
|
||||
//
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
//
|
||||
//public class UserServiceInterceptorAppConfig implements WebMvcConfigurer {
|
||||
//
|
||||
// @Override
|
||||
// public void addInterceptors(InterceptorRegistry registry) {
|
||||
// registry.addInterceptor(new UserInterceptor());
|
||||
// }
|
||||
//}
|
||||
@@ -13,10 +13,10 @@
|
||||
<a class="navbar-brand" href="/accueil">
|
||||
<img src="/img/logo.png" width="70" height="70" alt="Logo">
|
||||
</a>
|
||||
<!-- <div th:if="${#authentication.principal != 'anonymousUser'}">-->
|
||||
<!-- <p>Mes crédits :</p>-->
|
||||
<!-- <p th:text="${userProfile.credit}"></p>-->
|
||||
<!-- </div>-->
|
||||
<div th:if="${#authentication.principal != 'anonymousUser'}">
|
||||
<p>Mes crédits :</p>
|
||||
<p th:text="${user}"></p>
|
||||
</div>
|
||||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user