title: How to get same session with Spring Security and Spring Session From multiple server
body: <p>I'm sorry that my english is still not so good.
  Please bear with me, I hope you can understand my question..</p>
  
  <hr>
  
  <p>I have two web servers.
  (each web application is same)</p>
  
  <p>Web servers are sharing one redis server.
  And I use Spring Security and Spring Session.
  When I login first server and access second server,
  I want to login second server automatically, but it isn't.</p>
  
  <p>I guess, because session id is different from different server ip.</p>
  
  <ul>
  <li>how to get same session id ?</li>
  </ul>
  
  <h2>WEB.XML</h2>
  
  <pre><code>&lt;!-- The definition of the Root Spring Container shared by all Servlets 
      and Filters --&gt;
  &lt;!-- Loads Spring Security config file --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;
          /WEB-INF/spring/root-context.xml,
          /WEB-INF/spring/spring-security.xml,
          /WEB-INF/spring/jedis.xml
              &lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Processes application requests --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;!-- Encoding --&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;encoding&lt;/param-name&gt;
          &lt;param-value&gt;utf-8&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;!-- Session Filter --&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;springSessionRepositoryFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSessionRepositoryFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;!-- Spring Security --&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <h2>jedis.xml</h2>
  
  <pre><code>&lt;bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"&gt;
      &lt;property name="hostName" value=""&lt;!-- My Server IP --&gt; /&gt;
      &lt;property name="port" value="6379" /&gt;
      &lt;property name="poolConfig" ref="redisPoolConfig" /&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"&gt;
      &lt;property name="testOnBorrow" value="true" /&gt;
      &lt;property name="minEvictableIdleTimeMillis" value="60000" /&gt;
      &lt;property name="softMinEvictableIdleTimeMillis" value="1800000" /&gt;
      &lt;property name="numTestsPerEvictionRun" value="-1" /&gt;
      &lt;property name="testOnReturn" value="false" /&gt;
      &lt;property name="testWhileIdle" value="true" /&gt;
      &lt;property name="timeBetweenEvictionRunsMillis" value="30000" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- string serializer to make redis key more readible  --&gt;
  &lt;bean id="stringRedisSerializer"
      class="org.springframework.data.redis.serializer.StringRedisSerializer" /&gt;
  
  &lt;!-- redis template definition  --&gt;
  &lt;bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" &gt;
      &lt;property name="connectionFactory" ref="jedisConnectionFactory" /&gt;
      &lt;property name="keySerializer" ref="stringRedisSerializer" /&gt;
      &lt;property name="hashKeySerializer" ref="stringRedisSerializer" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <h2>spring-security.xml</h2>
  
  <pre><code>&lt;http pattern="/resources/**" security="none" /&gt;
  
  &lt;http auto-config="true" &gt;
      &lt;session-management session-fixation-protection="changeSessionId"&gt;
          &lt;concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/&gt; &lt;!-- I couldn't clear understand of this element--&gt;
      &lt;/session-management&gt;
      &lt;intercept-url pattern="/" access="ROLE_ANONYMOUS, ROLE_USER" /&gt;
      &lt;intercept-url pattern="/perBoard" access="ROLE_ANONYMOUS, ROLE_USER" /&gt;
      &lt;intercept-url pattern="/per" access="ROLE_ANONYMOUS, ROLE_USER" /&gt;
      &lt;intercept-url pattern="/perSearchTag" access="ROLE_ANONYMOUS, ROLE_USER" /&gt;
      &lt;intercept-url pattern="/**" access="ROLE_USER" /&gt;
      &lt;form-login login-page="/" 
                  authentication-success-handler-ref="loginSuccessHandler"
                  authentication-failure-handler-ref="loginFailureHandler"
                  always-use-default-target="true" 
                  username-parameter="j_username" 
                  password-parameter="j_password"/&gt;
                  &lt;!-- default-target-url="/board"  --&gt;
      &lt;logout logout-success-url="/" invalidate-session="true" delete-cookies="true" /&gt;
  &lt;/http&gt;
  
  &lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref="userDetailsService" /&gt;
  &lt;/authentication-manager&gt;
  
  &lt;beans:bean id="loginSuccessHandler" class=".......LoginSuccessHandler"&gt;
      &lt;beans:property name="sqlSession" ref="sqlSession" /&gt;
  &lt;/beans:bean&gt;
  &lt;beans:bean id="loginFailureHandler" class=".......LoginFailureHandler" /&gt;
  &lt;beans:bean id="userDetailsService" class="......UserDetailsServiceImpl"&gt;
      &lt;beans:property name="sqlSession" ref="sqlSession" /&gt;
  &lt;/beans:bean&gt;
  </code></pre>