Skip to content

Instantly share code, notes, and snippets.

@dogeared
Last active July 7, 2020 09:33
Show Gist options
  • Save dogeared/7436f9b5f51298e0c98a364817c018c8 to your computer and use it in GitHub Desktop.
Save dogeared/7436f9b5f51298e0c98a364817c018c8 to your computer and use it in GitHub Desktop.
spring boot 1.5.x with spring security 4 vs. spring boot 2.1.3 with spring security 5
@EnableOAuth2Sso
@RestController
@SpringBootApplication
public class OAuth2DemoApplication_1_5 {
@Value("#{ @environment['security.oauth2.resource.server'] }")
private String resourceServerUrl;
private OAuth2ProtectedResourceDetails resource;
public OAuth2DemoApplication_1_5(OAuth2ProtectedResourceDetails resource) {
this.resource = resource;
}
public static void main(String[] args) {
SpringApplication.run(OAuth2DemoApplication_1_5.class, args);
}
@GetMapping("/")
String home(@AuthenticationPrincipal OAuth2Authentication authentication) {
return "Hello " + authentication.getName();
}
@GetMapping("/api")
String api(@AuthenticationPrincipal OAuth2Authentication authentication) {
return tokenRelayTemplate(authentication).getForObject(resourceServerUrl + "/api", String.class);
}
private OAuth2RestTemplate tokenRelayTemplate(OAuth2Authentication authentication) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
OAuth2ClientContext context = new DefaultOAuth2ClientContext(new DefaultOAuth2AccessToken(details.getTokenValue()));
return new OAuth2RestTemplate(resource, context);
}
}
@RestController
@SpringBootApplication
public class OAuth2DemoApplication_2_1 {
@Value("#{ @environment['spring.security.oauth2.resource.server'] }")
private String resourceServerUrl;
private OAuth2AuthorizedClientService oAuth2AuthorizedClientService;
public OAuth2DemoApplication_2_1(OAuth2AuthorizedClientService oAuth2AuthorizedClientService) {
this.oAuth2AuthorizedClientService = oAuth2AuthorizedClientService;
}
public static void main(String[] args) {
SpringApplication.run(OAuth2DemoApplication_2_1.class, args);
}
@GetMapping("/")
String home(@AuthenticationPrincipal OidcUser user) {
return "Hello " + user.getFullName();
}
@GetMapping("/api")
String api(@AuthenticationPrincipal OAuth2AuthenticationToken oauthToken) {
OAuth2AuthorizedClient client = oAuth2AuthorizedClientService.loadAuthorizedClient(
oauthToken.getAuthorizedClientRegistrationId(), oauthToken.getName()
);
return tokenRelayTemplate(client.getAccessToken().getTokenValue())
.getForObject(resourceServerUrl + "/api", String.class);
}
private RestTemplate tokenRelayTemplate(String accessToken) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(getBearerTokenInterceptor(accessToken));
return restTemplate;
}
private ClientHttpRequestInterceptor getBearerTokenInterceptor(String accessToken) {
return new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(
HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution
) throws IOException {
request.getHeaders().add("Authorization", "Bearer " + accessToken);
return execution.execute(request, bytes);
}
};
}
}
@RestController
@SpringBootApplication
public class OAuth2DemoApplication_2_1 {
@Value("#{ @environment['spring.security.oauth2.resource.server'] }")
private String resourceServerUrl;
private WebClient webClient;
public OAuth2DemoApplication_2_1(WebClient webClient) {
this.webClient = webClient;
}
public static void main(String[] args) {
SpringApplication.run(OAuth2DemoApplication_2_1.class, args);
}
@GetMapping("/")
String home(@AuthenticationPrincipal OidcUser user) {
return "Hello " + user.getFullName();
}
@GetMapping("/api")
String api() {
// thanks to WebClientConfig, the access token will be
// included in the request automatically
return this.webClient
.get()
.uri(this.resourceServerUrl + "/api")
.retrieve()
.bodyToMono(String.class)
.block();
}
}
@Configuration
public class WebClientConfig {
@Bean
WebClient webClient(
ClientRegistrationRepository clientRegistrations, OAuth2AuthorizedClientRepository authorizedClients
) {
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 =
new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
oauth2.setDefaultOAuth2AuthorizedClient(true);
oauth2.setDefaultClientRegistrationId("okta");
return WebClient.builder()
.apply(oauth2.oauth2Configuration())
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment