|
| 1 | +package com.baeldung.controllers; |
| 2 | + |
| 3 | +import com.baeldung.models.UserCredentials; |
| 4 | +import org.apache.shiro.SecurityUtils; |
| 5 | +import org.apache.shiro.authc.AuthenticationException; |
| 6 | +import org.apache.shiro.authc.UsernamePasswordToken; |
| 7 | +import org.apache.shiro.subject.Subject; |
| 8 | +import org.springframework.stereotype.Controller; |
| 9 | +import org.springframework.ui.ModelMap; |
| 10 | +import org.springframework.web.bind.annotation.GetMapping; |
| 11 | +import org.springframework.web.bind.annotation.PostMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 14 | +import org.springframework.web.servlet.mvc.support.RedirectAttributes; |
| 15 | + |
| 16 | +import javax.servlet.http.HttpServletRequest; |
| 17 | + |
| 18 | +@Controller |
| 19 | +public class ShiroSpringController { |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + @GetMapping("/") |
| 24 | + public String index() { |
| 25 | + return "index"; |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + @RequestMapping( value = "/login", method = {RequestMethod.GET, RequestMethod.POST}) |
| 30 | + public String login(HttpServletRequest req, UserCredentials cred, RedirectAttributes attr) { |
| 31 | + |
| 32 | + if(req.getMethod().equals(RequestMethod.GET.toString())) { |
| 33 | + return "login"; |
| 34 | + } |
| 35 | + else { |
| 36 | + |
| 37 | + Subject subject = SecurityUtils.getSubject(); |
| 38 | + |
| 39 | + if(!subject.isAuthenticated()) { |
| 40 | + UsernamePasswordToken token = new UsernamePasswordToken( |
| 41 | + cred.getUsername(), cred.getPassword(), cred.isRememberMe()); |
| 42 | + try { |
| 43 | + subject.login(token); |
| 44 | + } catch (AuthenticationException ae) { |
| 45 | + ae.printStackTrace(); |
| 46 | + attr.addFlashAttribute("error", "Invalid Credentials"); |
| 47 | + return "redirect:/login"; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return "redirect:/secure"; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @GetMapping("/secure") |
| 57 | + public String secure(ModelMap modelMap) { |
| 58 | + |
| 59 | + Subject currentUser = SecurityUtils.getSubject(); |
| 60 | + String role = "", permission = ""; |
| 61 | + |
| 62 | + if(currentUser.hasRole("admin")) { |
| 63 | + role = role + "You are an Admin"; |
| 64 | + } |
| 65 | + else if(currentUser.hasRole("editor")) { |
| 66 | + role = role + "You are an Editor"; |
| 67 | + } |
| 68 | + else if(currentUser.hasRole("author")) { |
| 69 | + role = role + "You are an Author"; |
| 70 | + } |
| 71 | + |
| 72 | + if(currentUser.isPermitted("articles:compose")) { |
| 73 | + permission = permission + "You can compose an article, "; |
| 74 | + } else { |
| 75 | + permission = permission + "You are not permitted to compose an article!, "; |
| 76 | + } |
| 77 | + |
| 78 | + if(currentUser.isPermitted("articles:save")) { |
| 79 | + permission = permission + "You can save articles, "; |
| 80 | + } else { |
| 81 | + permission = permission + "\nYou can not save articles, "; |
| 82 | + } |
| 83 | + |
| 84 | + if(currentUser.isPermitted("articles:publish")) { |
| 85 | + permission = permission + "\nYou can publish articles"; |
| 86 | + } else { |
| 87 | + permission = permission + "\nYou can not publish articles"; |
| 88 | + } |
| 89 | + |
| 90 | + modelMap.addAttribute("username", currentUser.getPrincipal()); |
| 91 | + modelMap.addAttribute("permission", permission); |
| 92 | + modelMap.addAttribute("role", role); |
| 93 | + |
| 94 | + return "secure"; |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + @PostMapping("/logout") |
| 99 | + public String logout() { |
| 100 | + Subject subject = SecurityUtils.getSubject(); |
| 101 | + subject.logout(); |
| 102 | + return "redirect:/"; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments