You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChangeSessionId.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.vaadin.tests.applicationcontext;
  2. import com.vaadin.server.VaadinService;
  3. import com.vaadin.tests.components.AbstractTestCase;
  4. import com.vaadin.tests.util.Log;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.ui.Button.ClickEvent;
  7. import com.vaadin.ui.Button.ClickListener;
  8. import com.vaadin.ui.LegacyWindow;
  9. public class ChangeSessionId extends AbstractTestCase {
  10. private Log log = new Log(5);
  11. Button loginButton = new Button("Change session");
  12. boolean requestSessionSwitch = false;
  13. @Override
  14. public void init() {
  15. LegacyWindow mainWindow = new LegacyWindow("Sestest Application");
  16. mainWindow.addComponent(log);
  17. mainWindow.addComponent(loginButton);
  18. mainWindow.addComponent(
  19. new Button("Show session id", new Button.ClickListener() {
  20. @Override
  21. public void buttonClick(ClickEvent event) {
  22. logSessionId();
  23. }
  24. }));
  25. setMainWindow(mainWindow);
  26. loginButton.addClickListener(new ClickListener() {
  27. @Override
  28. public void buttonClick(ClickEvent event) {
  29. String oldSessionId = getSessionId();
  30. VaadinService
  31. .reinitializeSession(VaadinService.getCurrentRequest());
  32. String newSessionId = getSessionId();
  33. if (oldSessionId.equals(newSessionId)) {
  34. log.log("FAILED! Both old and new session id is "
  35. + newSessionId);
  36. } else {
  37. log.log("Session id changed successfully from "
  38. + oldSessionId + " to " + newSessionId);
  39. }
  40. }
  41. });
  42. logSessionId();
  43. }
  44. private void logSessionId() {
  45. log.log("Session id: " + getSessionId());
  46. }
  47. protected String getSessionId() {
  48. return getContext().getSession().getId();
  49. }
  50. @Override
  51. protected String getDescription() {
  52. return "Tests that the session id can be changed to prevent session fixation attacks";
  53. }
  54. @Override
  55. protected Integer getTicketNumber() {
  56. return 6094;
  57. }
  58. }