2015-06-07 11 views
5

(Düzenleme amaçlı olarak düzenlenmiştir) Oturum açmaya yönelik verileri depolamak için başarılı bir kimlik doğrulamasından sonra doldurmak üzere POJO (SessionStorage) kullanıyorum. Kapsamı "oturum" olarak ayarladığımdan, MainController ve AuthenticationSuccesshandler'ın nesnenin aynı örneğini kullanmasını bekliyorum.Kimlik doğrulama nesnelerini AuthenticationSuccessHandler olarak ayarlama

WebApp'ı çalıştırdığımda, Ana Denetleyici bir örneği (beklendiği gibi) başlatır, ancak oturum açtığımda, AuthenticationSuccesshandler'ın bir NullPointerException atar, çünkü SessionStorage nesnesinin otomatikleştirilmiş olması gerekmez.

İstediğimi yapmak için nasıl alabilirim? aşağıdaki gibi

@Component 
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class SessionStorage implements Serializable{ 
    long id; 
    public int getId() { 
    return id; 
    } 

    public SessionStorage() { 
     System.out.println("New Session Storage"); 
     id = System.currentTimeMillis(); 
    } 
} 

ana kontrolör görünüyor: İşte benim kod alıntılar verilmiştir

@Controller 
@Scope("request") 
@RequestMapping("/") 
public class MainController { 
    @Autowired 
    private SessionStorage sessionStorage; 

    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    public ModelAndView login(
      @RequestParam(value = "error", required = false) String error, 
      @RequestParam(value = "logout", required = false) String logout) { 

     System.out.println(sessionStorage.getId()); //Works fine 

     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; 
    } 
} 

(hata atılan) AuthentificationSuccesshandler:

public class AuthentificationSuccessHandler implements AuthenticationSuccessHandler { 

    @Autowired 
    private SessionStorage sessionStorage; 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest hsr, HttpServletResponse hsr1, Authentication a) throws IOException, ServletException { 
     System.out.println("Authentication successful: " + a.getName()); 
     System.out.println(sessionStorage.getId()); //NullPointerException 
    } 
} 

ilgili kısmı spring-security.xml:

<beans:bean id="authentificationFailureHandler" class="service.AuthentificationFailureHandler" /> 
    <beans:bean id="authentificationSuccessHandler" class="service.AuthentificationSuccessHandler" /> 
    <http auto-config="true" use-expressions="true"> 
     <intercept-url pattern="/secure/**" access="hasRole('USER')" /> 


     <form-login 
      login-page="/login" 
      default-target-url="/index" 
      authentication-failure-handler-ref="authentificationFailureHandler" 
      authentication-failure-url="/login?error" 
      authentication-success-handler-ref="authentificationSuccessHandler" 
      username-parameter="username" 
      password-parameter="password" /> 
     <logout logout-success-url="/login?logout" /> 
     <!-- enable csrf protection --> 
     <csrf/> 
    </http> 

web xml:

<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
+0

Ben bu eski bir sorudur farkındayım ama aynı sorunla çalıştırıyorum ve bir çözüm geldi acaba? – lastmannorth

cevap

2

Bu soru eski ama Google'da soruma ilk bağlantılardan biri olarak ortaya çıktı.

Bulunan en iyi düzeltme, özel AuthenticationSuccessHandler üzerinde Scope ayarlamaktı.

@Component 
@Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS) 

fazla ayrıntı burada bulunabilir: https://tuhrig.de/making-a-spring-bean-session-scoped/