2015-06-07 19 views
5

(Đã chỉnh sửa để làm rõ) Tôi có POJO (SessionStorage) để lưu trữ dữ liệu cụ thể của phiên mà tôi muốn điền sau khi xác thực thành công. Vì tôi đang thiết lập Phạm vi thành "phiên", tôi mong đợi cho MainController và AuthenticationSuccesshandler sử dụng cùng một thể hiện của đối tượng.Đặt các đối tượng phạm vi phiên trong AuthenticationSuccessHandler

Khi tôi chạy WebApp, bộ điều khiển chính khởi tạo một cá thể (như mong đợi), nhưng khi tôi đăng nhập, AuthenticationSuccesshandler dường như không có đối tượng SessionStorage tự động, vì nó ném NullPointerException.

Làm cách nào để làm điều mình muốn? Sau đây là các trích đoạn mã của tôi:

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

Bộ điều khiển chính trông như sau:

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

Các AuthentificationSuccesshandler (nơi các lỗi được ném):

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

Phần liên quan của 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

Tôi nhận ra đây là một câu hỏi cũ, nhưng tôi đang chạy vào cùng một vấn đề và đã tự hỏi nếu bạn đã đưa ra một giải pháp? – lastmannorth

Trả lời

2

Câu hỏi này cũ nhưng được hiển thị là một trong những liên kết đầu tiên cho câu hỏi của tôi trong Google.

Khắc phục sự cố mà tôi thấy hiệu quả nhất là đặt Phạm vi trên tùy chỉnh AuthenticationSuccessHandler.

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

Thông tin chi tiết có thể được tìm thấy ở đây: https://tuhrig.de/making-a-spring-bean-session-scoped/

Các vấn đề liên quan