· 6 years ago · Oct 26, 2018, 09:40 AM
1package pl.java.learning.todolist.infrastructure.config;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4import org.springframework.security.core.Authentication;
5import org.springframework.security.core.context.SecurityContextHolder;
6import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
7import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
8import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
9import org.springframework.web.context.annotation.RequestScope;
10import pl.java.learning.todolist.infrastructure.social.Facebook;
11
12@Configuration
13public class SocialConfig {
14
15 @Bean
16 @RequestScope
17 public Facebook facebook(OAuth2AuthorizedClientService clientService) {
18 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
19 String accessToken = null;
20 if (authentication.getClass().isAssignableFrom(OAuth2AuthenticationToken.class)) {
21 OAuth2AuthenticationToken oauthToken = (OAuth2AuthenticationToken) authentication;
22 String clientRegistrationId = oauthToken.getAuthorizedClientRegistrationId();
23 if (clientRegistrationId.equals("facebook")) {
24 OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(clientRegistrationId, oauthToken.getName());
25 accessToken = client.getAccessToken().getTokenValue();
26 }
27 }
28 return new Facebook(accessToken);
29 }
30}
31
32package pl.java.learning.todolist.infrastructure.social;
33
34import java.io.IOException;
35import org.springframework.http.HttpRequest;
36import org.springframework.http.client.ClientHttpRequestExecution;
37import org.springframework.http.client.ClientHttpRequestInterceptor;
38import org.springframework.http.client.ClientHttpResponse;
39import org.springframework.web.client.RestTemplate;
40
41public abstract class ApiBinding {
42 protected RestTemplate restTemplate;
43
44 public ApiBinding(String accessToken) {
45 this.restTemplate = new RestTemplate();
46 if (accessToken != null) {
47 this.restTemplate.getInterceptors().add(getBearerTokenInterceptor(accessToken));
48 } else {
49 this.restTemplate.getInterceptors().add(getNoTokenInterceptor());
50 }
51 }
52
53 private ClientHttpRequestInterceptor getBearerTokenInterceptor(String accessToken) {
54 return new ClientHttpRequestInterceptor() {
55 @Override
56 public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
57 request.getHeaders().add("Authorization", "Bearer " + accessToken);
58 return execution.execute(request, bytes);
59 }
60 };
61 }
62
63 private ClientHttpRequestInterceptor getNoTokenInterceptor() {
64 return new ClientHttpRequestInterceptor() {
65 @Override
66 public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
67 throw new IllegalStateException("Can't access the Facebook API without an access token");
68 }
69 };
70 }
71}
72
73package pl.java.learning.todolist.infrastructure.social;
74
75public class Facebook extends ApiBinding {
76 private static final String GRAPH_API_BASE_URL = "https://graph.facebook.com/v2.12";
77
78 public Facebook(String accessToken) {
79 super(accessToken);
80 }
81
82 public Profile getProfile() {
83 return restTemplate.getForObject(GRAPH_API_BASE_URL + "/me", Profile.class);
84 }
85
86}
87
88package pl.java.learning.todolist.infrastructure.social;
89
90import lombok.Data;
91
92@Data
93public class Profile {
94 private String id;
95 private String name;
96}
97
98<a href="/oauth2/authorization/facebook"><img src="/image/facebook.png" class="imgsize img-fluid m-10 " margin="10px" alt=""></a>
99
100<?xml version="1.0" encoding="UTF-8"?>
101<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
102 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
103 <modelVersion>4.0.0</modelVersion>
104
105 <groupId>pl.java.learning</groupId>
106 <artifactId>todo-list</artifactId>
107 <version>0.0.1-SNAPSHOT</version>
108 <packaging>jar</packaging>
109
110 <name>todo-list</name>
111 <description>To-do list project</description>
112
113 <parent>
114 <groupId>org.springframework.boot</groupId>
115 <artifactId>spring-boot-starter-parent</artifactId>
116 <version>2.0.4.RELEASE</version>
117 <relativePath/> <!-- lookup parent from repository -->
118 </parent>
119
120 <properties>
121 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
122 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
123 <java.version>1.8</java.version>
124 <swagger.version>2.9.2</swagger.version>
125 </properties>
126
127 <dependencies>
128 <dependency>
129 <groupId>org.springframework.boot</groupId>
130 <artifactId>spring-boot-starter-actuator</artifactId>
131 </dependency>
132 <dependency>
133 <groupId>org.springframework.boot</groupId>
134 <artifactId>spring-boot-starter-data-jpa</artifactId>
135 </dependency>
136 <dependency>
137 <groupId>org.springframework.boot</groupId>
138 <artifactId>spring-boot-starter-mail</artifactId>
139 </dependency>
140 <dependency>
141 <groupId>org.springframework.boot</groupId>
142 <artifactId>spring-boot-starter-security</artifactId>
143 </dependency>
144 <dependency>
145 <groupId>org.springframework.boot</groupId>
146 <artifactId>spring-boot-starter-web</artifactId>
147 </dependency>
148 <dependency>
149 <groupId>org.springframework.boot</groupId>
150 <artifactId>spring-boot-starter-thymeleaf</artifactId>
151 </dependency>
152 <dependency>
153 <groupId>org.flywaydb</groupId>
154 <artifactId>flyway-core</artifactId>
155 </dependency>
156
157 <dependency>
158 <groupId>org.springframework.boot</groupId>
159 <artifactId>spring-boot-devtools</artifactId>
160 <scope>runtime</scope>
161 </dependency>
162 <dependency>
163 <groupId>com.h2database</groupId>
164 <artifactId>h2</artifactId>
165 <scope>runtime</scope>
166 </dependency>
167 <dependency>
168 <groupId>org.projectlombok</groupId>
169 <artifactId>lombok</artifactId>
170 <optional>true</optional>
171 </dependency>
172 <dependency>
173 <groupId>io.springfox</groupId>
174 <artifactId>springfox-swagger2</artifactId>
175 <version>${swagger.version}</version>
176 </dependency>
177 <dependency>
178 <groupId>io.springfox</groupId>
179 <artifactId>springfox-swagger-ui</artifactId>
180 <version>${swagger.version}</version>
181 </dependency>
182 <dependency>
183 <groupId>org.springframework.boot</groupId>
184 <artifactId>spring-boot-starter-test</artifactId>
185 <scope>test</scope>
186 </dependency>
187 <dependency>
188 <groupId>org.springframework.security</groupId>
189 <artifactId>spring-security-test</artifactId>
190 <scope>test</scope>
191 </dependency>
192 <dependency>
193 <groupId>io.rest-assured</groupId>
194 <artifactId>rest-assured</artifactId>
195 <version>3.1.1</version>
196 <scope>test</scope>
197 </dependency>
198 <dependency>
199 <groupId>org.webjars</groupId>
200 <artifactId>bootstrap</artifactId>
201 <version>4.1.3</version>
202 </dependency>
203 <dependency>
204 <groupId>org.webjars</groupId>
205 <artifactId>jquery</artifactId>
206 <version>3.3.1</version>
207 </dependency>
208 <dependency>
209 <groupId>org.thymeleaf.extras</groupId>
210 <artifactId>thymeleaf-extras-springsecurity4</artifactId>
211 <version>3.0.2.RELEASE</version>
212 </dependency>
213 <dependency>
214 <groupId>org.springframework.security</groupId>
215 <artifactId>spring-security-oauth2-client</artifactId>
216 </dependency>
217 </dependencies>
218
219 <build>
220 <plugins>
221 <plugin>
222 <groupId>org.springframework.boot</groupId>
223 <artifactId>spring-boot-maven-plugin</artifactId>
224 </plugin>
225 </plugins>
226 </build>
227
228
229</project>