2012-02-24 8 views
0

ユーザがブラウザAからログイン後、スプリングMVC 3.1ログインおよびセッションビーン共有困難

ブラウザBは、ブラウザAと同じUserBeanのをロードするので、ブラウザBが自動的に記録されている。

I希望Springのセキュリティの代わりにHandlerInterceptorAdapterを使い続けるのが好きです。

解決策はありますか?

ありがとうございます。ここではコード、

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 オブジェクトはセッションスコープです。したがって、ユーザーが別のブラウザーを使用すると、新しいセッションが作成されます(別のユーザービーン)