Spring Security Tips
Spring Security Tips
Authentication
SecurityContextHolder.getContext().getAuthentication();로 로그인 정보를 얻을 수 있다.- xml 상에서
<http pattern="" security="none" />으로 되어 있는 페이지에선authentication을 불러올 수 없다.(null을 가져온다.) security="none"으로 되어 있으면<intercept-url pattern="" access="" />도 동작하지 않는다.
AuthenticationProvider
- 직접 정한 사용자 정보를 return하고 싶을 때는
Authentication을 상속받은 VO를 만들고 이를 return해 주면된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package iot.smartshoes.lbs.webapp.dto;
import java.util.Collection;
import java.util.Date;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
public class Admin implements Authentication {
private static final long serialVersionUID = 3502340419866769028L;
private Collection<? extends GrantedAuthority> authorities;
@Override
public String getName() {
// 유저 아이디
return this.adminId;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// 유저의 권한
return this.authorities;
}
@Override
public Object getCredentials() {
// 유저의 패스워드
return this.password;
}
@Override
public Object getDetails() {
// ???
return null;
}
@Override
public Object getPrincipal() {
// 유저의 아이디
return this.adminId;
}
@Override
public boolean isAuthenticated() {
// 인증된 사용자인지의 값
// 항상 true면 된다.
return true;
}
@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
// 인증된 사용자인지를 변경하기 위한 함수
}
}
- 위와 같이 생성하면
AuthenticationProvider의 return 값으로 사용할 수 있다. - 또한 모든 페이지에서
SecurityContextHolder.getContext().getAuthentication();를 사용하여 가져올 수 있다.
This post is licensed under CC BY 4.0 by the author.