Advertisement
Guest User

Controle.java

a guest
Mar 9th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. @RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET)
  2.     public String listUsers(ModelMap model) {
  3.         List users = userService.listAllUsers();
  4.         model.addAttribute("users", users);
  5.         model.addAttribute("loggedinuser", getPrincipal());
  6.         return "userslist";
  7.     }
  8.  
  9.     @ModelAttribute("roles")
  10.     public List initializeProfiles() {
  11.         return userProfileService.findAll();
  12.     }
  13.  
  14.     @RequestMapping(value = "/Access_Denied", method = RequestMethod.GET)
  15.     public String accessDeniedPage(ModelMap model) {
  16.         model.addAttribute("loggedinuser", getPrincipal());
  17.         return "accessDenied";
  18.     }
  19.  
  20.     @RequestMapping(value = "/login", method = RequestMethod.GET)
  21.     public String loginPage() {
  22.         if (isCurrentAuthenticationAnonymous()) {
  23.             return "login";
  24.         } else {
  25.             return "redirect:/list";
  26.         }
  27.     }
  28.  
  29.     @RequestMapping(value="/logout", method = RequestMethod.GET)
  30.     public String logoutPage (HttpServletRequest request, HttpServletResponse response){
  31.         Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  32.         if (auth != null){
  33.             //new SecurityContextLogoutHandler().logout(request, response, auth);
  34.             persistentTokenBasedRememberMeServices.logout(request, response, auth);
  35.             SecurityContextHolder.getContext().setAuthentication(null);
  36.         }
  37.         return "redirect:/login?logout";
  38.     }
  39.  
  40.     private String getPrincipal(){
  41.         String userName = null;
  42.         Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  43.  
  44.         if (principal instanceof UserDetails) {
  45.             userName = ((UserDetails)principal).getUsername();
  46.         } else {
  47.             userName = principal.toString();
  48.         }
  49.         return userName;
  50.     }
  51.  
  52.     private boolean isCurrentAuthenticationAnonymous() {
  53.         final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  54.         return authenticationTrustResolver.isAnonymous(authentication);
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement