diff --git a/src/main/java/com/auth/handler/LoginSuccesHandler.java b/src/main/java/com/auth/handler/LoginSuccesHandler.java index 7ba4a4f..d15b0a6 100644 --- a/src/main/java/com/auth/handler/LoginSuccesHandler.java +++ b/src/main/java/com/auth/handler/LoginSuccesHandler.java @@ -20,7 +20,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo SessionFlashMapManager flashMapManager = new SessionFlashMapManager(); FlashMap flashMap = new FlashMap(); - flashMap.put("success", "Hola, " + authentication.getName() + "Haz iniciado sesion con exito"); + flashMap.put("success", "Hola, " + authentication.getName() + " haz iniciado sesion con exito"); flashMapManager.saveOutputFlashMap(flashMap,request, response); if (authentication != null){ diff --git a/src/main/java/com/controllers/ClienteController.java b/src/main/java/com/controllers/ClienteController.java index 9ea41ff..5c45593 100644 --- a/src/main/java/com/controllers/ClienteController.java +++ b/src/main/java/com/controllers/ClienteController.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.net.MalformedURLException; +import java.util.Collection; import java.util.Map; import javax.validation.Valid; @@ -23,6 +24,9 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -82,8 +86,14 @@ public String listar(@RequestParam(name="page", defaultValue="0") int page, Mode Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - if (authentication != null){ - logger.info("Utilizando forma estatic: SecurityContextHolder.getContext().getAuthentication(): Usuario autenticado, username es: ".concat(authentication.getName())); + if(auth != null) { + logger.info("Utilizando forma estática SecurityContextHolder.getContext().getAuthentication(): Usuario autenticado: ".concat(auth.getName())); + } + + if(hasRole("ROLE_ADMIN")) { + logger.info("Hola ".concat(auth.getName()).concat(" tienes acceso!")); + } else { + logger.info("Hola ".concat(auth.getName()).concat(" NO tienes acceso!")); } Pageable pageRequest = PageRequest.of(page, 4); //spring 5 @@ -172,5 +182,34 @@ public String eliminar(@PathVariable(value="id") Long id, RedirectAttributes fla } } return "redirect:/listar"; - } + } + + private boolean hasRole(String role) { + + SecurityContext context = SecurityContextHolder.getContext(); + + if(context == null) { + return false; + } + + Authentication auth = context.getAuthentication(); + + if(auth == null) { + return false; + } + + Collection authorities = auth.getAuthorities(); + + return authorities.contains(new SimpleGrantedAuthority(role)); + + /* // nos permite obtener el ROLE_ + for(GrantedAuthority authority: authorities) { + if(role.equals(authority.getAuthority())) { + logger.info("Hola usuario ".concat(auth.getName()).concat(" tu role es: ".concat(authority.getAuthority()))); + return true; + } + } + + return false; */ + } }