UserDetails ๊ตฌํ
Spring Security ๊ธฐ๋ฅ์ ํ์ํ UserDetails๋ฅผ ์์๋ณด๊ณ ๊ตฌํํด๋ณด์.
Spring Security ๋ก๊ทธ์ธ ํํฐ๋ง
์คํ๋ง ์ํ๋ฆฌํฐ์ ๋ก๊ทธ์ธ ๊ณผ์ ์ WebSecurityConfigurerAdapter
์ ๊ตฌํํ ํด๋์ค์์ ์ ๋ฐ์ ์ธ ์ค์ ์ด ๊ฐ๋ฅํ๋ค.
์ด๋ http.authorizeRequests()
์ ๋ฉ์๋ ์ฒด์ธ ์ค loginPage()
์ ์ธ์๋ก ๊ฒฝ๋ก๋ฅผ ๋ฃ์ผ๋ฉด ํด๋น ๊ฒฝ๋ก์ ๋ก๊ทธ์ธ ์์ฒญ์ ์คํ๋ง ์ํ๋ฆฌํฐ๊ฐ ํํฐ๋งํ์ฌ ๋ก๊ทธ์ธ์ด ์งํ๋๋ค.
Spring Security Session
์คํ๋ง์ ๋ก๊ทธ์ธ์ ์งํ์ด ์ ์์ ์ผ๋ก ์๋ฃ๋๋ฉด ์คํ๋ง ์ํ๋ฆฌํฐ๋ฅผ ์ํ session์ ๋ง๋ค์ด์ฃผ์ด ๋ก๊ทธ์ธ ์ฒ๋ฆฌ๋ฅผ ํด์ค๋ค.
์ด session์ ์น ์๋ฒ์ ๊ฐ์ session ๊ณต๊ฐ ์ Security ContextHolder
๋ผ๋ ํค๊ฐ ์์ ์ ์ฅ๋๋ค.
Session์ ์ค๋ธ์ ํธ ํ์ -> Authentication
์ด๋, ์คํ๋ง ์ํ๋ฆฌํฐ ์ธ์
์ ๋ค์ด๊ฐ ์ ์๋ ์ค๋ธ์ ํธ ํ์
์ Authentication
ํ์
์ด๋ค.
๊ทธ๋ฌ๋ฏ๋ก, Authentication
ํ์
์์ ๋ก๊ทธ์ธ์ ํ User์ ๋ํ ์ ๋ณด๊ฐ ๋ค์ด ์์ด์ผ ํ๋๋ฐ, ์ฌ๊ธฐ์ ์ค์ํ ์ ์ Authentication๋ก ๊ฐ์ ์ ์๋ User์ ํ์
์ด ๊ทธ๋ฅ User ์ค๋ธ์ ํธ๊ฐ ์๋ UserDetails
ํ์
๋ง ๊ฐ๋ฅํ๋ค.
UserDetails ๊ตฌํ
์ด๋ฌํ ์์ ๊ณผ์ ๋๋ฌธ์ ์คํ๋ง ์ํ๋ฆฌํฐ์ ๋ก๊ทธ์ธ์์ UserDetails
๋ก User์ ๊ดํ ์ ๋ณด๋ฅผ ์ ๋ฌํด ์ค๋ค.
์ํ๋ ์ ๋ณด๋ฅผ ๋ด๊ธฐ ์ํด UserDetails๋ฅผ ํ์ฅํ ํด๋์ค๋ฅผ ์์ฑํ๊ณ ์ค๋ฒ๋ผ์ด๋ฉํ์ฌ ๊ตฌํํ๋ค.
์ ์ฒด ๊ตฌํ
package com.security.vividswan.auth;
import com.security.vividswan.model.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.ArrayList;
import java.util.Collection;
public class PrincipalDetails implements UserDetails {
private User user; // composition
PrincipalDetails(User user){
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> collect = new ArrayList<>();
collect.add(new GrantedAuthority() {
@Override
public String getAuthority() {
return user.getRole();
}
});
return collect;
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUsername();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
์ฝ๋ ์ค๋ช
private User user; // composition
PrincipalDetails(User user){
this.user = user;
}
์ํ๋ user ์ ๋ณด๋ฅผ ๋ด์์ฃผ๊ธฐ ์ํด User ๊ฐ์ฒด๋ฅผ ์ปดํฌ์ง์
ํ๋ค.
์ด ํด๋์ค๋ new ์์ฑ์๋ฅผ ํตํด Authentication
์์ ์ฌ์ฉํ ๊ฒ์ด๋ฏ๋ก ๋ฐ๋ก ์ด๋
ธํ
์ด์
์ ํด์ฃผ์ง ์๋๋ค.
์ด๋ฅผ ์ํด User
์ค๋ธ์ ํธ๊ฐ ๋ค์ด๊ฐ๋ ์์ฑ์๋ฅผ ๋ง๋ ๋ค.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> collect = new ArrayList<>();
collect.add(new GrantedAuthority() {
@Override
public String getAuthority() {
return user.getRole();
}
});
return collect;
}
User์ Role์ ๋ฃ์ด์ค์ผ ํ๋ getAuthorities()
๋ฉ์๋๋ค.
ํ ๋ช
์ User์ Role์ด ๋ณต์์ผ ์ ์์ผ๋ฏ๋ก Collection ํ์์ธ๋ฐ <GrantedAuthority>
์ ์ ๋ค๋ฆญ์ ๋ฐ๋ผ์ค์ผ ํ๋ค.
ํ์ํ ์ ๋ค๋ฆญ์ ๋ฐ๋ฅด๋ ArrayList๋ฅผ ๋ง๋ค์ด์ ๋ฆฌ์คํธ์ ์ค๋ธ์ ํธ๋ฅผ ๋ฃ์ด์ค์ผ ํ๋ค.
์ด๋ GrantedAuthority
ํ์์ ์งํค๊ธฐ ์ํด new GrantedAuthority()
๋ก ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค.getAuthority()
๋ฅผ ์ค๋ฒ๋ผ์ด๋ ํด์ค์ผ ํ๋๋ฐ ์ด๋ User์ Role์ String ํ์์ผ๋ก ๋ฆฌํดํ๋ฉด ๋ฆฌ์คํธ์ ์ค๋ธ์ ํธ ์ฝ์
์ด ๊ฐ๋ฅํ๋ค.
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUsername();
}
get๊ณผ ๊ด๋ จ๋ ๋ฉ์๋์ User์ ์ ๋ณด๋ฅผ return ํด์ค๋ค.
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
๊ฐ์
๋ User์ ๋ํ ๊ธฐ๊ฐ ๋ง๋ฃ, ์ ๊ธ ์ํ ๋ฑ๋ฑ์ ์ค์ ํ ์ ์๋ ๋ฉ์๋๋ค.
์์ ์์ ์์ ๋ชจ๋ true ์ฒ๋ฆฌํ์ง๋ง ํ์ํ ์กฐ๊ฑด๋ค์ ๋ง๋ค๊ณ ๋ถ๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Post ์์ฒญ ํ ์คํธ ์ฝ๋ ์์ฑ (0) | 2022.03.12 |
---|---|
์น ์๋ฒ์ ํฐ์ผ์ ์ฐจ์ด (0) | 2022.03.11 |
devtools ์ค์ (intellij) (0) | 2022.03.11 |
OAuth 2.0 ๋์ ๊ณผ์ (0) | 2022.03.10 |
์คํ๋ง ์ํ๋ฆฌํฐ Authentication (0) | 2022.03.10 |