Created
March 31, 2016 22:03
-
-
Save MafaldaLandeiro/d3c5a9100444e755493146efb8c1a552 to your computer and use it in GitHub Desktop.
Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.springSecurityLogin.controller; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import org.springframework.security.core.Authentication; | |
| import org.springframework.security.core.context.SecurityContextHolder; | |
| import org.springframework.security.core.userdetails.UserDetails; | |
| import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; | |
| import org.springframework.stereotype.Controller; | |
| import org.springframework.util.ObjectUtils; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RequestMethod; | |
| import org.springframework.web.bind.annotation.RequestParam; | |
| import org.springframework.web.servlet.ModelAndView; | |
| @Controller | |
| public class GreetingController { | |
| @RequestMapping(value = { "/", "/index" }, method = RequestMethod.GET) | |
| public ModelAndView indexPage() { | |
| ModelAndView model = new ModelAndView(); | |
| model.addObject("greeting", "WELCOME - Spring Security Greeting"); | |
| model.setViewName("index"); | |
| return model; | |
| } | |
| @RequestMapping(value = "/admin", method = RequestMethod.GET) | |
| public ModelAndView adminPage() { | |
| ModelAndView model = new ModelAndView(); | |
| model.addObject("msg", "Hi " + getPrincipal() | |
| + ", welcome to ADMIN page!"); | |
| model.setViewName("admin"); | |
| return model; | |
| } | |
| @RequestMapping(value = "/login", method = RequestMethod.GET) | |
| public ModelAndView login( | |
| @RequestParam(value = "error", required = false) String error, | |
| @RequestParam(value = "logout", required = false) String logout) { | |
| ModelAndView model = new ModelAndView(); | |
| if (error != null) { | |
| model.addObject("error", "Invalid username and password!"); | |
| } | |
| if (logout != null) { | |
| model.addObject("msg", "You've been logged out successfully."); | |
| } | |
| model.setViewName("login"); | |
| return model; | |
| } | |
| @RequestMapping(value = "/logout", method = RequestMethod.GET) | |
| public String logoutPage(HttpServletRequest request, | |
| HttpServletResponse response) { | |
| Authentication auth = SecurityContextHolder.getContext() | |
| .getAuthentication(); | |
| if (auth != null) { | |
| new SecurityContextLogoutHandler().logout(request, response, auth); | |
| } | |
| return "redirect:/login?logout=true"; | |
| } | |
| @RequestMapping(value = "/403", method = RequestMethod.GET) | |
| public ModelAndView accesssDenied() { | |
| ModelAndView model = new ModelAndView(); | |
| String user = getPrincipal(); | |
| if (!ObjectUtils.isEmpty(user)) { | |
| model.addObject("msg", "Hi " + user | |
| + ", you do not have permission to access this page!"); | |
| } else { | |
| model.addObject("msg", | |
| "You do not have permission to access this page!"); | |
| } | |
| model.setViewName("403"); | |
| return model; | |
| } | |
| @RequestMapping(value = "/db", method = RequestMethod.GET) | |
| public ModelAndView dba() { | |
| ModelAndView model = new ModelAndView(); | |
| model.addObject("msg", "Hi " + getPrincipal() | |
| + ", welcome to DBA page!"); | |
| model.setViewName("db"); | |
| return model; | |
| } | |
| /** | |
| * Get current user with login | |
| * | |
| * @return String - user name | |
| */ | |
| private String getPrincipal() { | |
| String userName = null; | |
| Object principal = SecurityContextHolder.getContext() | |
| .getAuthentication().getPrincipal(); | |
| if (principal instanceof UserDetails) { | |
| userName = ((UserDetails) principal).getUsername(); | |
| } else { | |
| userName = principal.toString(); | |
| } | |
| return userName; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment