2012-02-24 68 views
0

用戶從瀏覽器阿登錄後,彈簧MVC 3.1登錄和會話bean共享困難

瀏覽程序B加載相同的UserBean作爲瀏覽器A,所以瀏覽器B在自動登錄。

我會喜歡繼續使用HandlerInterceptorAdapter而不是spring安全性。

什麼是修復它?

在此先感謝。下面是代碼,

LoginInterceptor

public class LoginInterceptor extends HandlerInterceptorAdapter { 

    @Autowired 
    private UserBean userBean; 

    public boolean preHandle(
      HttpServletRequest request, 
      HttpServletResponse response, 
      Object handler) throws Exception { 

     String contextPath = request.getContextPath(); 
     String requestURI = request.getRequestURI(); 
     Boolean matched = requestURI.contains(contextPath + "/login"); 
     if (matched || userBean.getLogined()) { 
      return true; 
     } else {    
      response.sendRedirect(contextPath + "/login"); 
      return false; 
     } 
    } 
} 

的LoginController

@Controller 
@RequestMapping("login") 
public class LoginController { 

    @Autowired 
    private UserBean userBean; 

    @RequestMapping(method = RequestMethod.GET) 
    public String loginGET(Model model) { 
     LoginInput loginInput = new LoginInput(); 
     model.addAttribute("login", loginInput); 
     return "login"; 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    public String loginPOST(@Valid LoginInput loginInput, BindingResult result, Model model) { 
     if (result.hasErrors()) { 
      return "login"; 
     }  
     Boolean logined = userBean.login(loginInput.getUserName(), loginInput.getPassword()); 
     if (!logined) { 
      result.rejectValue("userName", "IncorrectLogin", "Incorrect login or password!"); 
      return "login"; 
     }  
     return "redirect:/index"; 
    } 
} 

LoginInput

public class LoginInput { 
    @NotEmpty 
    @Size(min = 1, max = 50) 
    private String userName; 
    @NotEmpty(message = "Password must not be blank.") 
    @Size(min = 4, max = 20, message = "Password must between 4 to 20 Characters.") 
    private String password; 

    public void setUserName(String userName) { 
      this.userName = userName; 
    } 
    public String getUserName() { 
      return userName; 
    } 
    public void setPassword(String password) { 
      this.password = password; 
    } 
    public String getPassword() { 
      return password; 
    } 

的AppConfig

@Configuration 
public class AppConfig { 
    @Bean @Scope("singleton") 
    public LdapService ldapService() throws LDAPException { 
     return new LdapService(); 
    } 

    @Bean @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 
    public UserBean userBean() { 
     return new UserBean(); 
    } 

    @Bean @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 
    public UserConfig userConfig() { 
     return new UserConfig(); 
    } 
} 

回答

0

可惜,這是行不通的。您的UserBean 對象是會話作用域。所以當用戶使用不同的瀏覽器時,他們將會創建一個新的會話(以及一個不同的UserBean)