· 7 years ago · Jan 23, 2018, 08:06 AM
1# Security config
2security:
3 oauth2:
4 client:
5 clientId: ...
6 clientSecret: ...
7 accessTokenUri: https://www.googleapis.com/oauth2/v4/token
8 userAuthorizationUri: https://accounts.google.com/o/oauth2/v2/auth
9 tokenName: oauth_token
10 authenticationScheme: query
11 clientAuthenticationScheme: form
12 scope:
13 - openid
14 - email
15 - profile
16 - https://www.googleapis.com/auth/groups
17 autoApproveScopes: '.*'
18 resource:
19 userInfoUri: https://www.googleapis.com/oauth2/v3/userinfo
20
21@SpringBootApplication
22@EnableOAuth2Sso
23@RestController
24public class ServerApplication {
25
26 public static void main(String[] args) {
27 SpringApplication.run(ServerApplication.class, args);
28 }
29
30 @GetMapping("/whoami")
31 public Principal echoTheUsersEmailAddress(Principal principal) {
32 return principal;
33 }
34}
35
36{
37 "authorities": [
38 {
39 "authority": "ROLE_USER"
40 }
41 ],
42 "details": {
43 "remoteAddress": "...",
44 "sessionId": "...",
45 "tokenValue": "...",
46 "tokenType": "Bearer",
47 "decodedDetails": null
48 },
49 "authenticated": true,
50 "userAuthentication": {
51 "authorities": [
52 {
53 "authority": "ROLE_USER"
54 }
55 ],
56 "details": {
57 "sub": "...",
58 "name": "...",
59 "given_name": "...",
60 "family_name": "...",
61 "picture": "...",
62 "email": "...",
63 "email_verified": true,
64 "locale": "en",
65 "hd": "..."
66 },
67 "authenticated": true,
68 "principal": "unknown",
69 "credentials": "N/A",
70 "name": "unknown"
71 },
72 "clientOnly": false,
73 "credentials": "",
74 "principal": "unknown",
75 "oauth2Request": {
76 "clientId": "...",
77 "scope": [],
78 "requestParameters": {},
79 "resourceIds": [],
80 "authorities": [],
81 "approved": true,
82 "refresh": false,
83 "redirectUri": null,
84 "responseTypes": [],
85 "extensions": {},
86 "grantType": null,
87 "refreshTokenRequest": null
88 },
89 "name": "unknown"
90}
91
92@Bean
93public PrincipalExtractor principalExtractor() {
94 return new PrincipalExtractor() {
95 @Override
96 public Object extractPrincipal(Map<String, Object> map) {
97 for (Map.Entry<String, Object> entry : map.entrySet())
98 {
99 System.out.println(entry.getKey() + "/" + entry.getValue());
100 }
101 return null;
102 }
103
104 };
105}