[BAEL-2315] spring-5-reactive-oauth & spring-5-security-oauth | WebClient and OAuth2 support (#6053)

* Cleaned shared properties in spring-5-security-oauth module, and made some final tunings on authorization service and the resource server

* Added and modified example for webclient-oauth2

* Cleaned authorization service and resource service for webclient and oauth2 article

*  Added examples for auth code with client and with login
cleaned properties and packages

*  Added examples fow webclient + oauth2
This commit is contained in:
Ger Roza
2019-01-02 22:13:35 -02:00
committed by maibin
parent 8311bde4bb
commit bbb6c93598
36 changed files with 777 additions and 22 deletions

View File

@@ -2,7 +2,9 @@ package com.baeldung.oauth2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@PropertySource("classpath:default-application.properties")
@SpringBootApplication
public class SpringOAuthApplication {

View File

@@ -3,10 +3,12 @@ package com.baeldung.oauth2extractors;
import org.apache.logging.log4j.util.Strings;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@PropertySource("classpath:default-application.properties")
@SpringBootApplication
@Controller
public class ExtractorsApplication {

View File

@@ -0,0 +1,17 @@
package com.baeldung.webclient.authorizationserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
@EnableAuthorizationServer
@PropertySource("classpath:webclient-authorization-application.properties")
@SpringBootApplication
public class AuthorizationServerApplication {
public static void main(String[] args) {
SpringApplication.run(AuthorizationServerApplication.class, args);
}
}

View File

@@ -0,0 +1,26 @@
package com.baeldung.webclient.authorizationserver.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login", "/user")
.permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.webclient.resourceserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
@EnableResourceServer
@PropertySource("webclient-resources-application.properties")
@SpringBootApplication
public class ResourceServerApplication {
public static void main(String[] args) {
SpringApplication.run(ResourceServerApplication.class, args);
}
}

View File

@@ -0,0 +1,29 @@
package com.baeldung.webclient.resourceserver.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
@Configuration
public class AuthorizationConfigs {
@Value("${oauth.authserver.client-id}")
String clientId;
@Value("${oauth.authserver.client-secret}")
String clientSecret;
@Value("${oauth.authserver.check-token-endpoint}")
String checkTokenEndpoint;
@Bean
public ResourceServerTokenServices tokenSvc() {
RemoteTokenServices remoteService = new RemoteTokenServices();
remoteService.setCheckTokenEndpointUrl(checkTokenEndpoint);
remoteService.setClientId(clientId);
remoteService.setClientSecret(clientSecret);
return remoteService;
}
}

View File

@@ -0,0 +1,23 @@
package com.baeldung.webclient.resourceserver.web;
import java.security.Principal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ResourceRestController {
@GetMapping("/retrieve-resource")
public String retrieveResource() {
return "This is the resource!";
}
@GetMapping("/user")
@ResponseBody
public Principal user(Principal user) {
return user;
}
}

View File

@@ -1,5 +1,3 @@
server.port=8081
logging.level.root=INFO
logging.level.com.baeldung.dsl.ClientErrorLoggingFilter=DEBUG

View File

@@ -0,0 +1 @@
server.port=8081

View File

@@ -0,0 +1,13 @@
server.port=8085
security.oauth2.client.client-id=bael-client-id
security.oauth2.client.client-secret=bael-secret
security.oauth2.client.scope=read,write
security.oauth2.authorization.check-token-access=isAuthenticated()
spring.security.user.name=bael-user
spring.security.user.password=bael-password
security.oauth2.client.registered-redirect-uri=http://localhost:8080/login/oauth2/code/bael, http://localhost:8080/authorize/oauth2/code/bael
security.oauth2.client.use-current-uri=false

View File

@@ -0,0 +1,6 @@
server.port=8084
#spring.security.oauth2.resourceserver.jwt.issuer-uri=localhost:8085
oauth.authserver.client-id=bael-client-id
oauth.authserver.client-secret=bael-secret
oauth.authserver.check-token-endpoint=http://localhost:8085/oauth/check_token