๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Web/Backend

[Spring] Spring Security๋ฅผ ํ†ตํ•œ Authentication(์ธ์ฆ) & Authorization(๊ถŒํ•œ ๋ถ€์—ฌ)

 ๐Ÿ’ก ์Šคํ”„๋ง์—์„œ ์ธ์ฆ๊ณผ ๊ถŒํ•œ ๋ถ€์—ฌ๋กœ ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ์„ ์ปจํŠธ๋กคํ•  ์ˆ˜ ์žˆ๋Š” Spring Security ํ”„๋ ˆ์ž„์›Œํฌ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž!

     Notion ์—์„œ ๋ณด๊ธฐ

 


๐Ÿ“Œ ์š”์•ฝ

  • Spring Security ์˜ ํ•ต์‹ฌ์€ ์ธ์ฆ๊ณผ ์ธ๊ฐ€

๐Ÿ“ Spring Security ์—์„œ ์ธ์ฆ๊ณผ ์ธ๊ฐ€

 

  • ๋‹ค๋ฅธ ๊ฒŒ์‹œ๊ธ€์—์„œ ์ •๋ฆฌํ•œ ์ธ์ฆ๊ณผ ์ธ๊ฐ€์— ๋Œ€ํ•ด ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด ์งง๊ฒŒ ์งš๊ณ  ๋„˜์–ด๊ฐ€์ž.
  • ์€ํ–‰์˜ ๊ธˆ๊ณ ๊ฐ€ ์žˆ๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ๊ธˆ๊ณ ๋ฅผ ์—ด์–ด๋ณธ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ์˜ ๊ณผ์ •์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
    1. ์‚ฌ์šฉ์ž๋Š” ์€ํ–‰์— ๊ฐ€์„œ ์ž์‹ ์ด ์–ด๋–ค ์‚ฌ๋žŒ์ธ์ง€ ์‹ ๋ถ„์ฆ์œผ๋กœ ์ž์‹ ์„ ์ฆ๋ช…ํ•œ๋‹ค.
    2. ์€ํ–‰์—์„œ ์‚ฌ์šฉ์ž์˜ ์‹ ๋ถ„์„ ํ™•์ธํ•œ๋‹ค.
    3. ์€ํ–‰์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ธˆ๊ณ ๋ฅผ ์—ด์–ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ธ์ง€๋ฅผ ํŒ๋‹จํ•œ๋‹ค.
    4. ๋งŒ์ผ ์ ์ ˆํ•œ ๊ถŒ๋ฆฌ๋‚˜ ๊ถŒํ•œ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž์˜ ๊ธˆ๊ณ ๋ฅผ ์—ด์–ด์ค€๋‹ค.

 

๐Ÿ‘Œ๐Ÿป ์ธ์ฆ(Authentication)

  • ํ•ด๋‹น ์‚ฌ์šฉ์ž๊ฐ€ ๋ณธ์ธ์ด ๋งž๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ
  • ์œ„์˜ ๊ณผ์ •์—์„œ 1๋ฒˆ์— ํ•ด๋‹น

 

๐Ÿ™†๐Ÿป‍โ™€๏ธ ์ธ๊ฐ€(Authorization)

  • ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ•œ ์ž์›์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์ ˆ์ฐจ
  • ์œ„์˜ ๊ณผ์ •์—์„œ 3๋ฒˆ์— ํ•ด๋‹น
  • Spring Security ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ธ์ฆ ์ ˆ์ฐจ๋ฅผ ๊ฑฐ์นœ ํ›„ → ์ธ๊ฐ€ ์ ˆ์ฐจ๋ฅผ ์ง„ํ–‰ ํ•˜๊ฒŒ๋˜๋ฉฐ,
  • ์ธ๊ฐ€ ๊ณผ์ •์—์„œ ํ•ด๋‹น ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์ด๋Ÿฌํ•œ ์ธ์ฆ๊ณผ ์ธ๊ฐ€๋ฅผ Principal ์„ ์•„์ด๋””๋กœ - Credential ์„ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์‚ฌ์šฉํ•˜๋Š” Credential ๊ธฐ๋ฐ˜์˜ ์ธ์ฆ ๋ฐฉ์‹ ์„ ์‚ฌ์šฉํ•œ๋‹ค.
    • Principal(์ ‘๊ทผ ์ฃผ์ฒด) : ๋ณดํ˜ธ ๋ฐ›๋Š” ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•˜๋Š” ๋Œ€์ƒ
    • Credential(๋น„๋ฐ€๋ฒˆํ˜ธ) : ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•˜๋Š” ๋Œ€์ƒ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ

๐Ÿ“‚ Spring Security ์ฃผ์š” ๋ชจ๋“ˆ

 

SecurityContextHolder

  • SecurityContextHolder ๋Š” ๋ณด์•ˆ ์ฃผ์ฒด์˜ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜์—ฌ, ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ํ˜„์žฌ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ์— ๋Œ€ํ•œ ์„ธ๋ถ€ ์ •๋ณด๊ฐ€ ์ €์žฅ๋œ๋‹ค.

 

SecurityContext

  • SecurityContext ๋Š” Authentication ์„ ๋ณด๊ด€ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ, SecurityContext ๋ฅผ ํ†ตํ•ด Authentication ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

Authentication

  • Authentication ์€ ํ˜„์žฌ ์ ‘๊ทผํ•˜๋Š” ์ฃผ์ฒด์˜ ์ •๋ณด์™€ ๊ถŒํ•œ์„ ๋‹ด๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.
  • SecurityContext ์— ์ €์žฅ๋˜๋ฉฐ, SecurityContextHolder ๋ฅผ ํ†ตํ•ด SecurityContext์— ์ ‘๊ทผํ•˜๊ณ  → SecurityContext๋ฅผ ํ†ตํ•ด Authentication ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
public interface Authentication extends Principal, Serializable {
	//ํ˜„์žฌ ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ด
	Collection<? extends GrantedAuthority> getAuthorities();
	//credentials(์ฃผ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ)๋ฅผ ๊ฐ€์ ธ์˜ด 
	Object getCredentials();
	
	Object getDetails();
	//Principal ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ด
	Object getPrincipal();
	//์ธ์ฆ ์—ฌ๋ถ€๋ฅผ ๊ฐ€์ ธ์˜ด
	boolean isAuthenticated();
	//์ธ์ฆ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•จ
	void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}

 

UsernamePasswordAuthenticationToken

  • Authentication ์„ ๊ตฌํ˜„ํ•œ AbstractAuthenticationToken ์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ด๋‹ค.
    • username : Pricipal์˜ ์—ญํ• 
    • password : Credential์˜ ์—ญํ• ์„ ํ•œ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ์ƒ์„ฑ์ž๋Š” - ์ธ์ฆ ์ „์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ ,
  • ๋‘ ๋ฒˆ์งธ ์ƒ์„ฑ์ž๋Š” - ์ธ์ฆ์ด ์™„๋ฃŒ๋œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {

	private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
	//์ฃผ๋กœ ์‚ฌ์šฉ์ž ID์— ํ•ด๋‹นํ•จ
	private final Object principal;
	//์ฃผ๋กœ ์‚ฌ์šฉ์ž PW์— ํ•ด๋‹นํ•จ
	private Object credentials;
	//์ธ์ฆ ์™„๋ฃŒ ์ „์˜ ๊ฐ์ฒด ์ƒ์„ฑ
	public UsernamePasswordAuthenticationToken(Object principal, Object credentials) {
		super(null);
		this.principal = principal;
		this.credentials = credentials;
		setAuthenticated(false);
	}
	//์ธ์ฆ ์™„๋ฃŒ ํ›„์˜ ๊ฐ์ฒด ์ƒ์„ฑ
	public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
			Collection<? extends GrantedAuthority> authorities) {
		super(authorities);
		this.principal = principal;
		this.credentials = credentials;
		super.setAuthenticated(true); // must use super, as we override
	}
}

public abstract class AbstractAuthenticationToken implements Authentication, CredentialsContainer { }

๐Ÿ” Spring Security๋ž€?

 

๐Ÿ“ Spring Security์˜ ๊ฐœ๋…

  • Spring Security ๋Š” Spring ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ณด์•ˆ(์ธ์ฆ๊ณผ ๊ถŒํ•œ ๋ถ€์—ฌ)์„ ๋‹ด๋‹นํ•˜๋Š” ์Šคํ”„๋ง์˜ ํ•˜์œ„ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.
  • Spring Security๋Š” ์Šคํ”„๋ง์˜ DispatcherServlet ์•ž๋‹จ์— Filter ํ˜•ํƒœ๋กœ ์œ„์น˜ํ•œ๋‹ค.
  • Dispatcher๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์—, ์ด Filter๊ฐ€ ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„์„œ → ํด๋ผ์ด์–ธํŠธ์˜ ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ ๊ถŒํ•œ์„ ํ™•์ธํ•˜๊ณ  → ์—†๋Š” ๊ฒฝ์šฐ์—๋Š”, ์ธ์ฆ ์š”์ฒญ ํ™”๋ฉด์œผ๋กœ ์ž๋™ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•œ๋‹ค.

 

โ“Spring Security์˜ ํ•„์š”์„ฑ

  • API์— ๋Œ€ํ•œ ๊ถŒํ•œ ๊ธฐ๋Šฅ์ด ์—†์œผ๋ฉด, ์•„๋ฌด๋‚˜ ํšŒ์› ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ณ  ์ˆ˜์ • ๋ฐ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ, ์ด๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ์ธ์ฆ๋œ ์œ ์ €๋งŒ API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผํ•˜๋ฉฐ, ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๋ถ€์—ฌ๋ฅผ ํ†ตํ•ด ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ์„ ์ปจํŠธ๋กค ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์„ Spring Security๊ฐ€ ์ œ๊ณตํ•œ๋‹ค.
    • ์ด๋Ÿฌํ•œ ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๊ด€๋ฆด ์œ„ํ•œ Java ํ”„๋ ˆ์ž„์›Œํฌ์—๋Š” JAAS, Spring Security, Apache Shiro ๋“ฑ์ด ์žˆ๋‹ค.
    • ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋“ค์— ๋น„ํ•ด ์™œ?! Spring Security๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ผ๊นŒ?!
  • Spring Security๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๋ณด์•ˆ ์ฒ˜๋ฆฌ๋ฅผ ์ž์ฒด์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ์‰ฝ๊ฒŒ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐ŸŽž๏ธ Spring Security์˜ ๋™์ž‘ ๊ณผ์ •

 

 

 

   1. ์‚ฌ์šฉ์ž ์ธ์ฆ ์š”์ฒญ - HTTP ์š”์ฒญ ์ˆ˜์‹ (Http Request) ๋ฐ AuthenticationFilter ํ†ต๊ณผ

 

  • Spring Security๋Š” ์ผ๋ จ์˜ ํ•„ํ„ฐ๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • ์š”์ฒญ(Request)๋Š” ์ธ์ฆ(Authentication)๊ณผ ๊ถŒํ•œ ๋ถ€์—ฌ(Authorization)์„ ์œ„ํ•ด ์ด ํ•„ํ„ฐ๋“ค์„ ํ†ต๊ณผํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์ด ํ•„ํ„ฐ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ๊ณผ์ •์€, ํ•ด๋‹น ์š”์ฒญ๊ณผ ๊ด€๋ จ๋œ ์ธ์ฆ ํ•„ํ„ฐ๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์ง€์†๋œ๋‹ค.
    • ์š”์ฒญ๊ณผ ๊ด€๋ จ๋œ ์ธ์ฆ ํ•„ํ„ฐ : ์ธ์ฆ ๋ฉ”์ปค๋‹ˆ์ฆ˜/๋ชจ๋ธ์— ๊ธฐ๋ฐ˜ํ•œ ๊ด€๋ จ ํ•„ํ„ฐ
  • ํ•„ํ„ฐ ํ†ต๊ณผ ์˜ˆ์‹œ
    • HTTP Basic ์ธ์ฆ ์š”์ฒญ์€, BasicAuthenticationFilter ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ•„ํ„ฐ ์ฒด์ธ์„ ํ†ต๊ณผํ•œ๋‹ค.
    • HTTP Digest ์ธ์ฆ ์š”์ฒญ์€ DigestAuthenticationFilter ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ•„ํ„ฐ ์ฒด์ธ์„ ํ†ต๊ณผํ•œ๋‹ค.
    • ๋กœ๊ทธ์ธ form submit ์š”์ฒญ์€ UsernamePasswordAuthenticationFilter ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ•„ํ„ฐ ์ฒด์ธ์„ ํ†ต๊ณผํ•œ๋‹ค.
    • X509 ์ธ์ฆ ์š”์ฒญ์€ X509AuthenticationFilter ๋“ฑ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ•„ํ„ฐ ์ฒด์ธ์„ ํ†ต๊ณผํ•œ๋‹ค.
  • ํ•„ํ„ฐ ์ฒด์ธ ์ค‘ ์ธ์ฆ์„ ๋‹ด๋‹นํ•˜๋Š” ํ•„ํ„ฐ๋ฅผ AuthenticationFilter ๋ผ๊ณ  ํ•œ๋‹ค.
    • AuthenticationFilter ๋Š” ์‚ฌ์šฉ์ž์˜ ์„ธ์…˜ ID(JSESSIONID)๊ฐ€ Security Context ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
      • ์—ฌ๊ธฐ์„œ, Security Context ๋ž€, ์•„๋ž˜์˜ ๋ชจ๋“  ๋กœ์ง์„ ํ†ต๊ณผํ•œ ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด(์ธ์ฆ ๊ฐœ์ฒด)๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„์ด๋‹ค.
    • Security Context์— ์„ธ์…˜ ID๊ฐ€ ์—†๋‹ค๋ฉด, ์•„๋ž˜ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

   2. ์‚ฌ์šฉ์ž ์ž๊ฒฉ ์ฆ๋ช…(ID, PW)์„ ๊ธฐ๋ฐ˜์œผ๋กœ AuthenticationToken ์ƒ์„ฑ

 

  • ์ธ์ฆ ์š”์ฒญ(Request)์ด ๊ด€๋ จ AuthenticationFilter ์— ์˜ํ•ด ์ˆ˜์‹ ๋˜๋ฉด, ์ˆ˜์‹ ๋œ ์š”์ฒญ์—์„œ ์‚ฌ์šฉ์ž ์ด๋ฆ„ & ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฅผ ์ถ”์ถœํ•œ๋‹ค.
  • ์ถ”์ถœ๋œ ์ž๊ฒฉ ์ฆ๋ช…(credentials)์„ ๊ธฐ๋ฐ˜์œผ๋กœ, ์ธ์ฆ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋ฅผ UsernamePasswordAuthenticationToken ์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

   3. ์ธ์ฆ์„ ์œ„ํ•ด AuthenticationManager์— ์ƒ์„ฑ๋œ AuthenticationToken ์œ„์ž„

 

  • ๋งŒ๋“ค์–ด์ง„ UsernamePasswordAuthenticationToken ์€ AuthenticationManager ์˜ ์ธ์ฆ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
  • AuthenticationManager ๋Š” ๋‹จ์ˆœํ•œ ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฉฐ, ์‹ค์ œ ๊ตฌํ˜„์€ ProviderManager ์ด๋‹ค.
  • ProviderManager ์—๋Š” ์‚ฌ์šฉ์ž ์š”์ฒญ์„ ์ธ์ฆํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ AuthenticationProvider ๋ชฉ๋ก์ด ์žˆ๋‹ค.
    • ProviderManager ๋Š” ์ œ๊ณต๋œ ๊ฐ AuthenticationProvider ๋ฅผ ์‚ดํŽด๋ณด๊ณ , ์ธ์ฆ๋œ ๊ฐœ์ฒด(UsernamePasswordAuthenticationToken) ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉ์ž ์ธ์ฆ์„ ์‹œ๋„ํ•œ๋‹ค.

 

 

   4. AuthenticationProvider ๋ชฉ๋ก์œผ๋กœ ์ธ์ฆ ์‹œ๋„

 

  • AuthenticationProvider ๋Š” ์ œ๊ณต๋œ ์ธ์ฆ ๊ฐœ์ฒด๋กœ ์‚ฌ์šฉ์ž๋ฅผ ์ธ์ฆํ•œ๋‹ค.
  • ํ”„๋ ˆ์ž„์›Œํฌ์— ์ œ๊ณต๋˜๋Š” AuthenticationProvider ์ผ๋ถ€
    • CasAuthenticationProvider
      • Cas(Central Authentication Service) : ์ค‘์•™ ์ธ์ฆ ์„œ๋น„์Šค๋กœ, ์›น์šฉ ํ†ตํ•ฉ ์ธ์ฆ ํ”„๋กœํ† ์ฝœ์ด๋‹ค.
        • ์ž๊ฒฉ ์ •๋ณด๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์ œ๊ณตํ•จ์œผ๋กœ์จ ์‚ฌ์šฉ์ž๊ฐ€ ์—ฌ๋Ÿฌ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ 
        • ์•”ํ˜ธ์™€ ๊ฐ™์€ ์‚ฌ์šฉ์ž์˜ ๋ณด์•ˆ ์ž๊ฒฉ ์ •๋ณด์— ์ ‘๊ทผ ๊ถŒํ•œ์„ ์ œ๊ณตํ•˜์ง€ ์•Š์•„๋„, ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๋“ค์ด ์‚ฌ์šฉ์ž๋ฅผ ์ธ์ฆํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
    • JaasAuthenticationProvider
    • DaoAuthenticationProvider
    • OpenIDAuthenticationProvider
    • RememberMeAuthenticationProvider
    • LdapAuthenticationProvider

 

 

   5. PasswordEncoder(๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์ธํ„ฐํŽ˜์ด์Šค) ์—์„œ ํŒจ์Šค์›Œ๋“œ ๋น„๊ต

 

  • PasswordEncoder ๋Š” ํŒจ์Šค์›Œ๋“œ ์•”ํ˜ธํ™”์— ์‚ฌ์šฉ๋  PasswordEncoder ๊ตฌํ˜„์ฒด๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • SpringBoot 2.0 ๋ถ€ํ„ฐ๋Š” ์ธ์ฆ์„ ์œ„ํ•ด์„œ ๋ฐ˜๋“œ์‹œ PasswordEncoder ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
  • BCryptPasswordEncoder : bcrypt ๋ผ๋Š” ํ•ด์‹œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์•”ํ˜ธํ™”ํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์„ค๊ณ„๋œ ํด๋ž˜์Šค์ด๋ฉฐ, ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.
@Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

 

6 ~ 8. ์‚ฌ์šฉ์ž ์ •๋ณด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด DB์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ๋ฐ˜ํ™˜

 

  • ์ผ๋ถ€ AuthenticationProvider ๋Š” ์‚ฌ์šฉ์ž ์ด๋ฆ„(username)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉ์ž ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด UserDetailsService ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • Ex) DaoAuthenticationProvider
  • UserDetailsService ๋Š” Spring Security ์˜ ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฉฐ, ์ด๋ฅผ ๊ตฌํ˜„ํ•œ ์„œ๋น„์Šค๋Š” ์ง์ ‘ ๊ฐœ๋ฐœํ•ด์•ผ ํ•œ๋‹ค.(Customize)
  • ์ฆ‰, ์œ„์˜ ์ฝ”๋“œ์—์„œ loadUserByUsername ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•ด DB์™€ ๋น„๊ตํ•˜๋Š” ****๋กœ์ง์„ ์ง์ ‘ ์งœ์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    • DB์™€ ์—ฐ๊ฒฐ์„ ์œ„ํ•œ VO ๋กœ User ๋ฅผ ๊ฐœ๋ฐœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

    9. ์ธ์ฆ ๊ฐœ์ฒด ๋ฐ˜ํ™˜ ํ˜น์€ AuthenticationException ๋ฐœ์ƒ

 

  • AuthenticationProvider ์ธํ„ฐํŽ˜์ด์Šค์— ์˜ํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ธ์ฆ๋˜๋ฉด → ์™„์ „ํžˆ ์ฑ„์›Œ์ง„ ์ธ์ฆ ๊ฐœ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
  • ์ธ์ฆ์— ์‹คํŒจํ•˜๋ฉด, AuthenticationException ์ด ๋ฐœ์ƒํ•œ๋‹ค.
    • AuthenticationException ์ด ๋ฐœ์ƒํ•˜๋ฉด, ์ธ์ฆ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ง€์›ํ•˜๋Š” AuthenticationEntryPoint ์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋œ๋‹ค.
      • AuthenticationEntryPoint ๋Š”, ์ธ์ฆ ๊ณผ์ •์— ์‹คํŒจํ•˜๊ฑฐ๋‚˜ ์ธ์ฆ ํ—ค๋”(Authorization) ๋ฅผ ๋ณด๋‚ด์ง€ ์•Š๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ → 401(UnAuthorized) ๋ผ๋Š” ์‘๋‹ต๊ฐ’์„ ๋ฐ›๊ฒŒ๋˜๋Š”๋ฐ, ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.

 

 

   10. ์ธ์ฆ ์™„๋ฃŒ

 

  • AuthenticationManager ๋Š” ํš๋“ํ•œ ์™„์ „ํžˆ ์ฑ„์›Œ์ง„ ์ธ์ฆ ๊ฐœ์ฒด๋ฅผ ๊ด€๋ จ ์ธ์ฆ ํ•„ํ„ฐ(AuthenticationFilter) ๋กœ ๋‹ค์‹œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

   11. SecurityContext ์— ์ธ์ฆ ๊ฐœ์ฒด ์„ค์ •(Authentication ์ €์žฅ ๋ฐ ์ธ์ฆ ์™„๋ฃŒ ์ฒ˜๋ฆฌ)

 

  • ๊ด€๋ จ AuthenticationFilter ๋Š” ์ดํ›„์— ํ•„ํ„ฐ ์‚ฌ์šฉ์„ ์œ„ํ•ด ํš๋“ํ•œ ์ธ์ฆ ๊ฐœ์ฒด๋ฅผ SecurityContext ์— ์ €์žฅํ•œ๋‹ค.
SecurityContextHolder.getContext().setAuthentication(authentication);
  • ์ดํ›„, SecurityContext ์— ์ธ์ฆ ๊ฐœ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  → ์ธ์ฆ ๋กœ์ง์ด ์ˆ˜ํ–‰๋˜๊ฑฐ๋‚˜ ์ˆ˜ํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ”– ์ฐธ๊ณ  ์ž๋ฃŒ