Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. @Component
  2. @Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
  3. public class CurrentUser {
  4.  
  5. private boolean isUserLoggedIn;
  6.  
  7. @Autowired
  8. public CurrentUser(UserService users) {
  9. isUserLoggedIn = users.isUserLoggedIn();
  10. }
  11.  
  12. public boolean isLoggedIn() {
  13. return isUserLoggedIn;
  14. }
  15. }
  16.  
  17. @Service
  18. public class UserService {
  19.  
  20. public boolean isUserLoggedIn() {
  21. return isAuthenticated(getAuth());
  22. }
  23.  
  24. private boolean isAuthenticated(Authentication auth) {
  25. return auth.isAuthenticated() &&
  26. auth.getPrincipal() instanceof org.springframework.security.core.userdetails.User;
  27. }
  28.  
  29. private Authentication getAuth() {
  30. return SecurityContextHolder.getContext().getAuthentication();
  31. }
  32. }
  33.  
  34. @RequestMapping(value={"tasks"})
  35. @PreAuthorize("hasRole('ROLE_USER')")
  36. @Controller
  37. public class TaskController {
  38.  
  39. private final CurrentUser currentTaskUser;
  40.  
  41. @Autowired
  42. public TaskController(CurrentUser currentTaskUser) {
  43. this.currentTaskUser = currentTaskUser;
  44. }
  45.  
  46. @ModelAttribute
  47. public boolean isTaskUserLoggedIn() {
  48. System.out.println("TaskController isLoggedIn: " + currentTaskUser.isLoggedIn());
  49. return false;
  50. }
  51. }
  52.  
  53. @ControllerAdvice
  54. public class ControllerSettings {
  55.  
  56. private final CurrentUser currentUser;
  57.  
  58. @Autowired
  59. public ControllerSettings(CurrentUser currentUser) {
  60. this.currentUser = currentUser;
  61. }
  62.  
  63. @ModelAttribute
  64. public boolean isUserLoggedIn() {
  65. /* the following line creates the problem */
  66. System.out.println("ControllerSettings isLoggedIn: " + currentUser.isLoggedIn());
  67. return false;
  68. }
  69. }
  70.  
  71. TaskController isLoggedIn: true
  72.  
  73. ControllerSettings isLoggedIn: false
  74. TaskController isLoggedIn: false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement