1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.springframework.security.oauth2.provider;
15
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 import org.springframework.security.core.Authentication;
21 import org.springframework.security.core.GrantedAuthority;
22 import org.springframework.security.core.context.SecurityContextHolder;
23
24
25
26
27
28
29
30 public class DefaultSecurityContextAccessor implements SecurityContextAccessor {
31
32 @Override
33 public boolean isUser() {
34 Authentication authentication = getUserAuthentication();
35 return authentication != null;
36 }
37
38 @Override
39 public Set<GrantedAuthority> getAuthorities() {
40 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
41 if (authentication == null) {
42 return Collections.emptySet();
43 }
44 return Collections.unmodifiableSet(new HashSet<GrantedAuthority>(authentication.getAuthorities()));
45 }
46
47 private Authentication getUserAuthentication() {
48 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
49 if (authentication == null) {
50 return null;
51 }
52 if (authentication instanceof OAuth2Authentication) {
53 OAuth2Authentication oauth = (OAuth2Authentication) authentication;
54 return oauth.getUserAuthentication();
55 }
56 return authentication;
57 }
58
59 }