2015-06-07 3 views
5

(Bearbeitet zur Verdeutlichung) Ich habe ein POJO (SessionStorage) zum Speichern sitzungsspezifischer Daten, die nach einer erfolgreichen Authentifizierung ausgefüllt werden sollen. Da ich das Scope auf "Sitzung" setze, erwarte ich, dass der MainController und der AuthenticationSuccesshandler dieselbe Instanz des Objekts verwenden.Festlegen von sitzungsspezifischen Objekten in AuthenticationSuccessHandler

Wenn ich die WebApp ausführen, initiiert der Hauptcontroller eine Instanz (wie erwartet), aber wenn ich mich anmelde, scheint der AuthenticationSuccesshandler das SessionStorage-Objekt nicht automatisch ausgelöst zu haben, da es eine NullPointerException auslöst.

Wie bekomme ich es zu tun, was ich will? Hier sind die Auszüge aus meinem Code:

@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(); 
    } 
} 

Die Hauptsteuerung sieht wie folgt aus:

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

Die AuthentificationSuccesshandler (wo der Fehler ausgelöst wird):

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

Der entsprechende Teil des 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

Ich weiß, das eine alte Frage, aber ich bin in das gleiche Problem laufen und mich gefragt, ob Sie mit einer Lösung kommen? – lastmannorth

Antwort

2

Diese Frage ist alt, aber zeigte als einer der ersten Links auf meine Frage in Google auf.

Der Fix, der am besten funktionierte, bestand darin, das Scope auf den benutzerdefinierten AuthenticationSuccessHandler zu setzen.

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

Weitere Details finden Sie hier: https://tuhrig.de/making-a-spring-bean-session-scoped/

Verwandte Themen