Skip to content

Commit 738d65b

Browse files
Merge pull request eugenp#5314 from freddyaott/master
[BAEL-1519] Guide to ScribeJava
2 parents 13cd077 + 465c571 commit 738d65b

13 files changed

Lines changed: 422 additions & 2 deletions

File tree

libraries-security/pom.xml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,30 @@
88

99
<parent>
1010
<groupId>com.baeldung</groupId>
11-
<artifactId>parent-modules</artifactId>
12-
<version>1.0.0-SNAPSHOT</version>
11+
<artifactId>parent-boot-1</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<relativePath>../parent-boot-1</relativePath>
1314
</parent>
1415

1516
<dependencies>
1617

18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.springframework.security.oauth</groupId>
25+
<artifactId>spring-security-oauth2</artifactId>
26+
<version>2.3.3.RELEASE</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>com.github.scribejava</groupId>
31+
<artifactId>scribejava-apis</artifactId>
32+
<version>${scribejava.version}</version>
33+
</dependency>
34+
1735
<dependency>
1836
<groupId>junit</groupId>
1937
<artifactId>junit</artifactId>
@@ -25,6 +43,9 @@
2543

2644
<properties>
2745
<junit.version>4.12</junit.version>
46+
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
47+
<scribejava.version>5.6.0</scribejava.version>
2848
</properties>
2949

50+
3051
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.scribejava;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
7+
@SpringBootApplication
8+
public class ScribejavaApplication {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(ScribejavaApplication.class, args);
12+
}
13+
14+
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.scribejava.api;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
5+
public class MyApi extends DefaultApi20 {
6+
7+
private MyApi() {
8+
}
9+
10+
private static class InstanceHolder {
11+
private static final MyApi INSTANCE = new MyApi();
12+
}
13+
14+
public static MyApi instance() {
15+
return InstanceHolder.INSTANCE;
16+
}
17+
18+
@Override
19+
public String getAccessTokenEndpoint() {
20+
return "http://localhost:8080/oauth/token";
21+
}
22+
23+
@Override
24+
protected String getAuthorizationBaseUrl() {
25+
return null;
26+
}
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.scribejava.controller;
2+
3+
import com.baeldung.scribejava.service.GoogleService;
4+
import com.github.scribejava.core.model.OAuth2AccessToken;
5+
import com.github.scribejava.core.model.OAuthRequest;
6+
import com.github.scribejava.core.model.Response;
7+
import com.github.scribejava.core.model.Verb;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import javax.servlet.http.HttpServletResponse;
14+
15+
@RestController
16+
public class GoogleController {
17+
18+
@Autowired
19+
private GoogleService service;
20+
21+
22+
@GetMapping(value ="/me/google")
23+
public void me(HttpServletResponse response){
24+
String auth = service.getService().getAuthorizationUrl();
25+
26+
response.setHeader("Location", auth);
27+
response.setStatus(302);
28+
29+
}
30+
31+
@GetMapping(value = "/auth/google")
32+
public String google(@RequestParam String code, HttpServletResponse servletResponse){
33+
34+
try {
35+
OAuth2AccessToken token = service.getService().getAccessToken(code);
36+
37+
OAuthRequest request = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
38+
service.getService().signRequest(token, request);
39+
Response response = service.getService().execute(request);
40+
return response.getBody();
41+
42+
}catch (Exception e){
43+
servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
44+
}
45+
46+
return null;
47+
}
48+
49+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.scribejava.controller;
2+
3+
import com.baeldung.scribejava.service.TwitterService;
4+
import com.github.scribejava.core.model.*;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
import java.util.Scanner;
12+
import java.util.concurrent.ExecutionException;
13+
14+
@RestController
15+
public class TwitterController {
16+
17+
@Autowired
18+
private TwitterService service;
19+
20+
21+
@GetMapping(value ="/me/twitter")
22+
public String me(HttpServletResponse servletResponse){
23+
try {
24+
OAuth1RequestToken requestToken = service.getService().getRequestToken();
25+
26+
String auth = service.getService().getAuthorizationUrl(requestToken);
27+
28+
Runtime runtime = Runtime.getRuntime();
29+
try {
30+
runtime.exec("rundll32 url.dll,FileProtocolHandler " + auth);
31+
} catch (IOException e) {
32+
servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
33+
return null;
34+
}
35+
36+
System.out.println("Insert twitter code:");
37+
Scanner in = new Scanner(System.in);
38+
39+
String oauthverifier = in.nextLine();
40+
41+
final OAuth1AccessToken accessToken = service.getService().getAccessToken(requestToken,oauthverifier);
42+
43+
OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json");
44+
service.getService().signRequest(accessToken, request);
45+
Response response = service.getService().execute(request);
46+
return response.getBody();
47+
48+
} catch (IOException | InterruptedException | ExecutionException e) {
49+
servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
50+
}
51+
52+
return null;
53+
}
54+
55+
56+
57+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.scribejava.controller;
2+
3+
import com.baeldung.scribejava.service.MyService;
4+
import com.github.scribejava.core.model.OAuth2AccessToken;
5+
import com.github.scribejava.core.model.OAuthRequest;
6+
import com.github.scribejava.core.model.Response;
7+
import com.github.scribejava.core.model.Verb;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import javax.servlet.http.HttpServletResponse;
14+
import java.security.Principal;
15+
16+
@RestController(value = "/user")
17+
public class UserController {
18+
19+
@Autowired
20+
private MyService service;
21+
22+
@GetMapping("/me/myapi")
23+
public String me(@RequestParam String username, @RequestParam String password, HttpServletResponse responsehttp) {
24+
25+
try {
26+
OAuth2AccessToken token = service.getService().getAccessTokenPasswordGrant(username, password);
27+
28+
OAuthRequest request = new OAuthRequest(Verb.GET, "http://localhost:8080/me");
29+
service.getService().signRequest(token, request);
30+
Response response = service.getService().execute(request);
31+
32+
return response.getBody();
33+
34+
} catch (Exception e) {
35+
responsehttp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
36+
}
37+
38+
return null;
39+
40+
}
41+
42+
@GetMapping("/me")
43+
public Principal user(Principal principal) {
44+
return principal;
45+
}
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.scribejava.oauth;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.HttpMethod;
7+
import org.springframework.security.authentication.AuthenticationManager;
8+
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
9+
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
10+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
11+
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
12+
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
13+
14+
15+
@Configuration
16+
@EnableAuthorizationServer
17+
public class AuthServiceConfig extends AuthorizationServerConfigurerAdapter {
18+
19+
@Autowired
20+
@Qualifier("authenticationManagerBean")
21+
private AuthenticationManager authenticationManager;
22+
23+
@Override
24+
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
25+
oauthServer.tokenKeyAccess("permitAll()")
26+
.checkTokenAccess("isAuthenticated()");
27+
}
28+
29+
@Override
30+
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
31+
clients.inMemory()
32+
.withClient("baeldung_api_key")
33+
.secret("baeldung_api_secret")
34+
.authorizedGrantTypes("password","refresh_token")
35+
.scopes("read","write").autoApprove(true);
36+
}
37+
38+
@Override
39+
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
40+
endpoints
41+
.authenticationManager(authenticationManager)
42+
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
43+
}
44+
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.scribejava.oauth;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.authentication.AuthenticationManager;
6+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
10+
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
11+
12+
@Configuration
13+
@EnableResourceServer
14+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
15+
16+
@Override
17+
protected void configure(HttpSecurity http) throws Exception {
18+
http
19+
.headers().frameOptions().disable()
20+
.and()
21+
.csrf().disable();
22+
}
23+
24+
@Override
25+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
26+
auth.inMemoryAuthentication()
27+
.withUser("baeldung")
28+
.password("scribejava")
29+
.roles("USER");
30+
}
31+
32+
@Override
33+
@Bean
34+
public AuthenticationManager authenticationManagerBean() throws Exception {
35+
return super.authenticationManagerBean();
36+
}
37+
38+
39+
@EnableResourceServer
40+
@Configuration
41+
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
42+
43+
@Override
44+
public void configure(HttpSecurity http) throws Exception {
45+
http
46+
.authorizeRequests()
47+
.antMatchers("/user/me").authenticated()
48+
.and()
49+
.csrf().disable();
50+
}
51+
}
52+
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.scribejava.service;
2+
3+
import com.github.scribejava.apis.GoogleApi20;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.core.oauth.OAuth20Service;
6+
import org.springframework.stereotype.Component;
7+
8+
import javax.annotation.PostConstruct;
9+
@Component
10+
public class GoogleService {
11+
12+
private OAuth20Service service;
13+
private final String API_KEY = "api_key";
14+
private final String API_SECRET = "api_secret";
15+
private final String SCOPE = "https://www.googleapis.com/auth/userinfo.email";
16+
private final String CALLBACK = "http://localhost:8080/auth/google";
17+
18+
@PostConstruct
19+
private void init(){
20+
this.service = new ServiceBuilder(API_KEY)
21+
.apiSecret(API_SECRET)
22+
.scope(SCOPE)
23+
.callback(CALLBACK)
24+
.build(GoogleApi20.instance());
25+
}
26+
27+
28+
public OAuth20Service getService() {
29+
return service;
30+
}
31+
}

0 commit comments

Comments
 (0)