Skip to content

Commit

Permalink
obteniendo programaticamente el ROLE del usuario en el controlador
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaiasMorochi committed Sep 7, 2019
1 parent 82f4fee commit ed92423
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/auth/handler/LoginSuccesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
45 changes: 42 additions & 3 deletions src/main/java/com/controllers/ClienteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.Map;

import javax.validation.Valid;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<? extends GrantedAuthority> 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; */
}
}

0 comments on commit ed92423

Please sign in to comment.