HttpSessionEventPublisher本身不會創建會話。它只是將servlet會話事件轉換爲spring安全性的等價事件。實際上,會話的創建不受春季安全控制,但如果需要,它可以啓動一次。
如果您只是想在認證時設置會話超時,那麼您可以擴展您使用的認證處理程序並在那裏設置超時。
例如,下面的代碼擴展SavedRequestAwareAuthenticationSuccessHandler和檢索來自應用程序屬性超時(而不是數據庫作爲你的情況)
@Component
public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Value("#{appProperties['session.timeout']}")
private int sessionTimeout;
private final Logger logger = LoggerFactory.getLogger(AuthenticationSuccessHandler.class);
@Override
public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse res, Authentication authentication) throws ServletException, IOException {
logger.debug("onAuthenticationSuccess");
HttpSession session = req.getSession();
session.setMaxInactiveInterval(sessionTimeout);
super.onAuthenticationSuccess(req, res, authentication);
}
}
是啊是啊!這就是我最終做的。 :d – mpmp 2014-10-30 07:52:32