Post

πŸ₯œ [Spring] Filter 와 μ „λ°˜μ μΈ 톡신 흐름

Preview

Cookie λŠ” Session 이 λ§Œλ“€μ–΄μ§ˆ λ•Œ μžλ™μœΌλ‘œ 생성이 λœλ‹€.

Session 은 Client κ°€ μš”μ²­ν•  λ•Œ λ§Œλ“€μ–΄μ§„λ‹€.

Cookie λŠ” 맡 λ°©μ‹μœΌλ‘œ μ €μž₯이 되며 value λŠ” String 으둜 λ§Œλ“€μ–΄μ§€κ²Œ λ˜μ–΄μžˆλ‹€.

μ΄λ•Œ μžλ™μœΌλ‘œ λ§Œλ“€μ–΄μ§€λŠ” Cookie μ—λŠ” JSESSIONID λΌλŠ”κ²Œ μ‘΄μž¬ν•˜λŠ”λ°

데이터 (VO …) λŠ” Session 에 μ €μž₯ν•˜κ³  μ‚¬μš©μžλ₯Ό κ΅¬λΆ„ν•˜λŠ” JSESSIONID λŠ” 쿠킀에 μ €μž₯ν•œλ‹€.

전체적인 흐름

image-25

  1. μ»¨ν…Œμ΄λ„ˆλŠ” μ²˜μŒμ— μ£Όλ¬Έμ„œ(DD)λ₯Ό 읽어듀이며 (web.xml) μ—¬κΈ°μ—λŠ” Servlet κ³Ό κ΄€λ ¨λœ 맡핑 정보가 λ‹΄κ²¨μžˆλ‹€.
  2. κ·Έ ν›„ Servlet이 λ¨Όμ € λ§Œλ“€μ–΄μ§€λŠ”κ²Œ μ•„λ‹ˆλΌ ServletContext κ°€ λ¨Όμ € μƒμ„±λœλ‹€.
  3. κ·Έ λ‹€μŒ Servlet이 λ§Œλ“€μ–΄μ§„λ‹€. (μƒμ„±μž 호좜)
  4. 0을 λ§Œλ“€κ³ 
  5. init()을 호좜, 0을 μΈμžκ°’μœΌλ‘œ λ„£λŠ”λ‹€.
  6. μ—¬κΈ°κΉŒμ§€ Ready on μƒνƒœμ΄λ‹€. (Clientκ°€ μš”μ²­ν•˜κΈ° μ „ μ„œλ²„ μ€€λΉ„ μƒνƒœμ΄λ‹€.)

  7. Client κ°€ μš”μ²­ν•˜λ©΄ request, response 객체가 λ§Œλ“€μ–΄μ§„λ‹€. λ˜ν•œ Session 이 λ§Œλ“€μ–΄μ§€κ³ 
  8. JSESSIONID κ°’λ§Œ μ €μž₯ν•  쿠킀도 λ§Œλ“€μ–΄μ§„λ‹€.
  9. κ·Έ λ‹€μŒ Service() κ°€ 호좜되고, doGet(), doPost()κ°€ ν˜ΈμΆœλœλ‹€. 이 λ•Œ request, response κ°€ μΈμžκ°’μœΌλ‘œ λ“€μ–΄κ°„λ‹€.
  10. μš”μ²­μ„ μˆ˜ν–‰ν•˜λ©΄(μ‘λ‹΅ν•˜λ©΄) req, res, thread κ°€ μ£½λŠ”λ‹€. μš”μ²­ν•  λ•Œ λ§ˆλ‹€ λ°˜λ³΅λœλ‹€.



Filter

image-26

μ„œλ²„λ‘œ μš”μ²­μ„ 보내고 응닡을 ν•  λ•Œ κ³΅ν†΅μ μœΌλ‘œ 처리(μ–‘λ°©ν–₯ 처리)ν•  뢀뢄에 κ΄€ν•œ λ‘œμ§μ„ filter 둜 μ²˜λ¦¬ν•œλ‹€.

예λ₯Ό λ“€μ–΄ ν•œκΈ€ 처리 등…

ν•œκΈ€ μ²˜λ¦¬λŠ” Client μ—μ„œ Server 둜 μš”μ²­ν•  λ•Œ , Server μ—μ„œ Client 둜 응닡할 λ•Œ λͺ¨λ‘ μ²˜λ¦¬ν•΄μ€˜μ•Ό ν•œλ‹€.

λ”°λΌμ„œ Controller μ—μ„œ μ²˜λ¦¬ν•΄μ£ΌλŠ” 것이 μ•„λ‹ˆλΌ Filter μ—μ„œ ν•΄μ£ΌλŠ” 것이 μ’‹λ‹€.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class EncodingFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		//κ³΅ν†΅μ μœΌλ‘œ μ²˜λ¦¬ν•΄μ•Ό ν•  둜직... λͺ¨λ“  μ„œλΈ”λ¦Ώ μ—μ„œ 
		//ν•œκΈ€μ²˜λ¦¬
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		//μ„œλ²„μƒμ—μ„œ λ‹€μŒ μ»΄ν¬λ„ŒνŠΈ ν”„λ‘œκ·Έλž¨μ— κ³„μ†μ μœΌλ‘œ ν•„ν„°λ§ν•œ 곡톡 둜직이 μ μš©λœλ‹€.
		//이 뢀뢄을 빠뜨리면 κ³΅ν†΅λ‘œμ§μ„ 필터링에 μ •μ˜λ§Œ ν–ˆκΈ° λ•Œλ¬Έμ— μ‹€μ§ˆμ μœΌλ‘œ μ²˜λ¦¬λ˜μ§€ μ•ŠλŠ”λ‹€. λ°˜λ“œμ‹œ λ„£μ–΄μ•Όν•œλ‹€.
		chain.doFilter(request, response);
	}
This post is licensed under CC BY 4.0 by the author.