원칙과 핵심 원칙 레디 스 실제 실현 (6) 봄 세션 분산 세션의 문제를 해결 설명

원칙과 핵심 원칙 레디 스 실제 실현 (7) 봄 세션 분산 세션의 문제를 해결 설명

당신이 봄 세션의 원칙의 실현에 대한 자세한 내용은 아래의 이전이 봄 세션 및 세션 레디 스 도메인 간 분산 공유 문제를 해결 사용하는 방법에 대해 설명합니다, 우리는 간단한 사례를 소개했다.

web.xml 파일에 대해 이야기 시작

우리는 다시 시작 톰캣 먼저로드 갈 때 알고 web.xml파일을 때, Tomcat이 시작 web.xml순서가로드됩니다 context-param -> listener -> filter -> servlet.

우리는 봄 세션을 사용하는 경우, 우리는 필터를 구성한 다음과 같이 구성 코드는 다음과 같습니다

<filter>
     <filter-name>springSessionRepositoryFilter</filter-name>
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
     <filter-name>springSessionRepositoryFilter</filter-name>
     <url-pattern>/*</url-pattern>
     <dispatcher>REQUEST</dispatcher>
     <dispatcher>ERROR</dispatcher>
</filter-mapping>
 
  • 1
  • 4
  • 5
  • 6
  • (7)
  • 8
  • 9
  • (10)
  • (11)

여기에 설명 된 사진을 쓰기

에 대한 정보를 알려 DelegatingFilterProxy이 클래스 :

DelegatingFilterProxy에 의해 클래스 springSessionRepositoryFilter이름은 구성 콩 스프링 컨테이너와 필터 계산기를 조회하기 위해 호출 DelegatingFilterProxy이 필터는 springSessionRepositoryFilter 호출됩니다, 각 요청.

당신은 지정하지 않으면 init-param매개 변수를 다음 DelegatingFilterProxy그것을 넣어 filter-name당신이 찾고있는 빈 개체를, 이것은이다 DelegatingFilterProxy역할의 종류. 각각의 요청이 필터를 통해 볼 것이다, 필터가 통해 해당 통해 요청한다 springSessionRepositoryFilter이 필터, 우리는 다음에서 볼 수 springSessionRepositoryFilter필터.

springSessionRepositoryFilter 필터 만들기

스프링 컨테이너에서 springSessionRepositoryFilter을 찾을 수있는 방법에 DelegatingFilterProxy는, 우리는 어떤 장소 어디에서 springSessionRepositoryFilter을 이식하는? 답은 여기에 있습니다 :

<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
 
  • 1

여기에 설명 된 사진을 쓰기

Spring 설정 파일에서 우리는 수동으로 RedisHttpSessionConfiguration을 주입 우리는 세션을 저장하는 기본 레디 스를 사용 때문이다.

RedisHttpSessionConfiguration이 클래스는 사출 프로파일로, 구성 노트를 추가합니다.

RedisHttpSessionConfiguration 역할은 필터에서 상속 봄 콩의 이름 springSessionRepositoryFilter를 만드는 것입니다. springSessionRepositoryFilter 기본 HttpSession에 컨테이너 지원 봄 세션의 레디 스에 저장된 세션 인스턴스를 리필.

(1) RedisHttpSessionConfiguration 상속은 다음과 같이

@Configuration
@EnableScheduling
public class RedisHttpSessionConfiguration extends SpringHttpSessionConfiguration
		implements EmbeddedValueResolverAware, ImportAware {
}
 
  • 1
  • 4
  • 5

여기에 설명 된 사진을 쓰기

(2) 메인 RedisHttpSessionConfiguration 방법 및 특성은 다음과 같이 :

여기에 설명 된 사진을 쓰기

@Bean로서 (3) RedisHttpSessionConfiguration RedisMessageListenerContainer、RedisTemplate、RedisOperationsSessionRepository등 봄 용기 내로 주입된다.

(4) RedisHttpSessionConfiguration SpringHttpSessionConfiguration이 클래스를 상속이 클래스는 매우 중요 springSessionRepositoryFilter 용기에 붓고 의해 SpringHttpSessionConfiguration @Bean :

여기에 설명 된 사진을 쓰기

이 필터 springSessionRepositoryFilter 앞 DelegatingFilterProxy 찾는 필터입니다!

(6) 우리는 그가 역할 SessionRepositoryFilter의 SessionRepositoryFilter 유형이 있음을 볼 수있는 것은 기본 컨테이너 javax.servlet.http.HttpSession 지원 org.springframework.session.Session을 교체하는 것입니다.

SessionRepositoryFilter的主要方法和属性如下:

여기에 설명 된 사진을 쓰기

(7)其中SessionRepositoryResponseWrapper、SessionRepositoryRequestWrapper、HttpSessionWrapper为内部类,这个也是很关键的。例如SessionRepositoryRequestWrapper类:

여기에 설명 된 사진을 쓰기

可以看出SessionRepositoryRequestWrapper继承了javax.servlet.http.HttpServletRequestWrapper这个类,我们知道HttpServletRequest接口的默认实现是有HttpServletRequestWrapper的,如下:

여기에 설명 된 사진을 쓰기

(8)因为SessionRepositoryRequestWrapper继承了HttpServletRequestWrapper,而HttpServletRequestWrapper实现了HttpServletRequest接口,在SessionRepositoryRequestWrapper又重写了HttpServletRequest接口中的一些方法,所以才会有:getSession、changeSessionId等这些方法。

到此,我们应该大致明白了,原有的request请求和response都被重新进行了包装。我们也就明白了原有的HttpSeesion是如何被Spring Session替换掉的。

需要注意的是:

The SessionRepositoryFilter must be placed before any Filter that access the HttpSession or that might commit the response to ensure the session is overridden and persisted properly.
 
  • 1

案例分析

(1)Controller代码如下:

여기에 설명 된 사진을 쓰기

(2)查看效果:

여기에 설명 된 사진을 쓰기

我们通过快捷键查看request.getSession() 的具体实现,就可以看出已经有了SessionRepositoryRequestWrapper 重写的方法。

위의 두 가지 기본 구현, 하나의 원본이 있는데, 하나는 봄 세션 실현 어떤 종류의 특정 선택으로 구현이며, 이것은 우리가 위에서 말한 DelegatingFilterProxy프록시 역할, 그가 DelegatingFilterProxy을 통해, 각 필터를 요청을 각 요청은 특정 프로세스를 달성하는 SessionRepositoryRequestWrapper 원래 요청의 전환을 달성하기 springSessionRepositoryFilter 필터 springSessionRepositoryFilter 필터의 대상이 될 것이다!

(3) request.getSession ()의 setAttribute (이름, 값) 的 实现. :

코드를 추적, 당신은 다음과 같은 내용을 도달 할 수있다

여기에 설명 된 사진을 쓰기

세션에 this.session.setAttribute (이름, 값)은 세션 인터페이스 특정 구현에 정의되어있다 :

여기에 설명 된 사진을 쓰기

레디 스 관련 작업을 볼 수 있습니다!

이 시점에서, 우리는 명확해야한다, 봄 세션의 원리! 프로세스가 도입 이하로하지 않았지만, 그것은 명확하게 이해하고있다.

개요

봄 세션의 전체 실행 과정에 대한보다도 연구의 기타 세부 사항에 대해 너무 많이하지, 사후 학습은 기사를 보충하는 것입니다!

추천

출처www.cnblogs.com/ko88/p/11697979.html