forked from spring-projects/spring-authorization-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue spring-projectsgh-538 Issue spring-projectsgh-499 Issue spring-projectsgh-106
- Loading branch information
Showing
15 changed files
with
1,072 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,137 @@ | ||
= Messages Sample | ||
= Samples | ||
|
||
This sample integrates `spring-security-oauth2-client` and `spring-security-oauth2-resource-server` with *Spring Authorization Server*. | ||
[[messages-sample]] | ||
== Messages Sample | ||
|
||
The messages sample integrates `spring-security-oauth2-client` and `spring-security-oauth2-resource-server` with *Spring Authorization Server*. | ||
|
||
The username is `user1` and the password is `password`. | ||
|
||
== Run the Sample | ||
[[run-messages-sample]] | ||
=== Run the Sample | ||
|
||
* Run Authorization Server -> `./gradlew -b samples/default-authorizationserver/samples-default-authorizationserver.gradle bootRun` | ||
* Run Resource Server -> `./gradlew -b samples/messages-resource/samples-messages-resource.gradle bootRun` | ||
* Run Client -> `./gradlew -b samples/messages-client/samples-messages-client.gradle bootRun` | ||
* Go to `http://127.0.0.1:8080` | ||
|
||
[[federated-identity-sample]] | ||
== Federated Identity Sample | ||
|
||
The federated identity sample builds on the messages sample above, adding social login and federated identity features to *Spring Authorization Server* using custom configuration. | ||
|
||
[[google-login]] | ||
=== Login with Google | ||
|
||
This section shows how to configure Spring Security using Google as an Authentication Provider. | ||
|
||
[[google-initial-setup]] | ||
==== Initial setup | ||
|
||
To use Google's OAuth 2.0 authentication system for login, you must set up a project in the Google API Console to obtain OAuth 2.0 credentials. | ||
|
||
NOTE: https://developers.google.com/identity/protocols/OpenIDConnect[Google's OAuth 2.0 implementation] for authentication conforms to the | ||
https://openid.net/connect/[OpenID Connect 1.0] specification and is https://openid.net/certification/[OpenID Certified]. | ||
|
||
Follow the instructions on the https://developers.google.com/identity/protocols/OpenIDConnect[OpenID Connect] page, starting in the section, "Setting up OAuth 2.0". | ||
|
||
After completing the "Obtain OAuth 2.0 credentials" instructions, you should have a new OAuth Client with credentials consisting of a Client ID and a Client Secret. | ||
|
||
[[google-redirect-uri]] | ||
==== Setting the redirect URI | ||
|
||
The redirect URI is the path in the application that the end-user's user-agent is redirected back to after they have authenticated with Google | ||
and have granted access to the OAuth Client _(created in the previous step)_ on the Consent page. | ||
|
||
In the "Set a redirect URI" sub-section, ensure that the *Authorized redirect URIs* field is set to `http://localhost:9000/login/oauth2/code/google-idp`. | ||
|
||
TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`. | ||
The *_registrationId_* is a unique identifier for the `ClientRegistration`. | ||
|
||
[[google-application-config]] | ||
==== Configure application.yml | ||
|
||
Now that you have a new OAuth Client with Google, you need to configure the application to use the OAuth Client for the _authentication flow_. To do so: | ||
|
||
. Go to `application.yml` and set the following configuration: | ||
+ | ||
[source,yaml] | ||
---- | ||
spring: | ||
security: | ||
oauth2: | ||
client: | ||
registration: <1> | ||
google-idp: <2> | ||
provider: google | ||
client-id: google-client-id | ||
client-secret: google-client-secret | ||
---- | ||
+ | ||
.OAuth Client properties | ||
==== | ||
<1> `spring.security.oauth2.client.registration` is the base property prefix for OAuth Client properties. | ||
<2> Following the base property prefix is the ID for the `ClientRegistration`, such as google-idp. | ||
==== | ||
|
||
. Replace the values in the `client-id` and `client-secret` property with the OAuth 2.0 credentials you created earlier. | ||
Alternatively, you can set the following environment variables in the Spring Boot application: | ||
* `GOOGLE_CLIENT_ID` | ||
* `GOOGLE_CLIENT_SECRET` | ||
|
||
[[github-login]] | ||
=== Login with GitHub | ||
|
||
This section shows how to configure Spring Security using Github as an Authentication Provider. | ||
|
||
[[github-register-application]] | ||
==== Register OAuth application | ||
|
||
To use GitHub's OAuth 2.0 authentication system for login, you must https://github.com/settings/applications/new[Register a new OAuth application]. | ||
|
||
When registering the OAuth application, ensure the *Authorization callback URL* is set to `http://localhost:9000/login/oauth2/code/github-idp`. | ||
|
||
The Authorization callback URL (redirect URI) is the path in the application that the end-user's user-agent is redirected back to after they have authenticated with GitHub | ||
and have granted access to the OAuth application on the _Authorize application_ page. | ||
|
||
TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`. | ||
The *_registrationId_* is a unique identifier for the `ClientRegistration`. | ||
|
||
[[github-application-config]] | ||
==== Configure application.yml | ||
|
||
Now that you have a new OAuth application with GitHub, you need to configure the application to use the OAuth application for the _authentication flow_. To do so: | ||
|
||
. Go to `application.yml` and set the following configuration: | ||
+ | ||
[source,yaml] | ||
---- | ||
spring: | ||
security: | ||
oauth2: | ||
client: | ||
registration: <1> | ||
github-idp: <2> | ||
provider: github | ||
client-id: github-client-id | ||
client-secret: github-client-secret | ||
---- | ||
+ | ||
.OAuth Client properties | ||
==== | ||
<1> `spring.security.oauth2.client.registration` is the base property prefix for OAuth Client properties. | ||
<2> Following the base property prefix is the ID for the `ClientRegistration`, such as github-idp. | ||
==== | ||
|
||
. Replace the values in the `client-id` and `client-secret` property with the OAuth 2.0 credentials you created earlier. | ||
Alternatively, you can set the following environment variables in the Spring Boot application: | ||
* `GITHUB_CLIENT_ID` | ||
* `GITHUB_CLIENT_SECRET` | ||
|
||
[[run-federated-identity-sample]] | ||
=== Run the Sample | ||
|
||
* Run Authorization Server -> `./gradlew -b samples/federated-identity-authorizationserver/samples-federated-identity-authorizationserver.gradle bootRun` | ||
* Run Resource Server -> `./gradlew -b samples/messages-resource/samples-messages-resource.gradle bootRun` | ||
* Run Client -> `./gradlew -b samples/messages-client/samples-messages-client.gradle bootRun` | ||
* Go to `http://127.0.0.1:8080` |
19 changes: 19 additions & 0 deletions
19
...erated-identity-authorizationserver/samples-federated-identity-authorizationserver.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apply plugin: 'io.spring.convention.spring-sample-boot' | ||
|
||
dependencies { | ||
compile 'org.springframework.boot:spring-boot-starter-web' | ||
compile 'org.springframework.boot:spring-boot-starter-security' | ||
compile 'org.springframework.boot:spring-boot-starter-oauth2-client' | ||
compile 'org.springframework.boot:spring-boot-starter-thymeleaf' | ||
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' | ||
compile 'org.webjars:webjars-locator-core' | ||
compile 'org.webjars:bootstrap:3.4.1' | ||
compile 'org.webjars:jquery:3.4.1' | ||
compile 'org.springframework.boot:spring-boot-starter-jdbc' | ||
compile project(':spring-security-oauth2-authorization-server') | ||
runtimeOnly 'com.h2database:h2' | ||
|
||
testCompile 'org.springframework.boot:spring-boot-starter-test' | ||
testCompile 'org.springframework.security:spring-security-test' | ||
testCompile 'net.sourceforge.htmlunit:htmlunit' | ||
} |
32 changes: 32 additions & 0 deletions
32
...orizationserver/src/main/java/sample/FederatedIdentityAuthorizationServerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2020-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package sample; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author Steve Riesenberg | ||
* @since 0.2.3 | ||
*/ | ||
@SpringBootApplication | ||
public class FederatedIdentityAuthorizationServerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(FederatedIdentityAuthorizationServerApplication.class, args); | ||
} | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
...d-identity-authorizationserver/src/main/java/sample/config/AuthorizationServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright 2020-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package sample.config; | ||
|
||
import java.util.UUID; | ||
|
||
import com.nimbusds.jose.jwk.JWKSet; | ||
import com.nimbusds.jose.jwk.RSAKey; | ||
import com.nimbusds.jose.jwk.source.JWKSource; | ||
import com.nimbusds.jose.proc.SecurityContext; | ||
import sample.jose.Jwks; | ||
import sample.security.FederatedIdentityConfigurer; | ||
import sample.security.FederatedIdentityIdTokenCustomizer; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
import org.springframework.security.config.Customizer; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.security.oauth2.core.ClientAuthenticationMethod; | ||
import org.springframework.security.oauth2.core.oidc.OidcScopes; | ||
import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationConsentService; | ||
import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService; | ||
import org.springframework.security.oauth2.server.authorization.JwtEncodingContext; | ||
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService; | ||
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; | ||
import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer; | ||
import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository; | ||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; | ||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; | ||
import org.springframework.security.oauth2.server.authorization.config.ClientSettings; | ||
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
/** | ||
* @author Steve Riesenberg | ||
* @since 0.2.3 | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
public class AuthorizationServerConfig { | ||
|
||
@Bean | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception { | ||
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http); | ||
http.apply(new FederatedIdentityConfigurer()); | ||
return http.formLogin(Customizer.withDefaults()).build(); | ||
} | ||
|
||
@Bean | ||
public OAuth2TokenCustomizer<JwtEncodingContext> idTokenCustomizer() { | ||
return new FederatedIdentityIdTokenCustomizer(); | ||
} | ||
|
||
// @formatter:off | ||
@Bean | ||
public RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) { | ||
RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString()) | ||
.clientId("messaging-client") | ||
.clientSecret("{noop}secret") | ||
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC) | ||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) | ||
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) | ||
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) | ||
.redirectUri("http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc") | ||
.redirectUri("http://127.0.0.1:8080/authorized") | ||
.scope(OidcScopes.OPENID) | ||
.scope("message.read") | ||
.scope("message.write") | ||
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()) | ||
.build(); | ||
|
||
// Save registered client in db as if in-memory | ||
JdbcRegisteredClientRepository registeredClientRepository = new JdbcRegisteredClientRepository(jdbcTemplate); | ||
registeredClientRepository.save(registeredClient); | ||
|
||
return registeredClientRepository; | ||
} | ||
// @formatter:on | ||
|
||
@Bean | ||
public OAuth2AuthorizationService authorizationService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) { | ||
return new JdbcOAuth2AuthorizationService(jdbcTemplate, registeredClientRepository); | ||
} | ||
|
||
@Bean | ||
public OAuth2AuthorizationConsentService authorizationConsentService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) { | ||
return new JdbcOAuth2AuthorizationConsentService(jdbcTemplate, registeredClientRepository); | ||
} | ||
|
||
@Bean | ||
public JWKSource<SecurityContext> jwkSource() { | ||
RSAKey rsaKey = Jwks.generateRsa(); | ||
JWKSet jwkSet = new JWKSet(rsaKey); | ||
return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet); | ||
} | ||
|
||
@Bean | ||
public ProviderSettings providerSettings() { | ||
return ProviderSettings.builder().issuer("http://localhost:9000").build(); | ||
} | ||
|
||
@Bean | ||
public EmbeddedDatabase embeddedDatabase() { | ||
// @formatter:off | ||
return new EmbeddedDatabaseBuilder() | ||
.generateUniqueName(true) | ||
.setType(EmbeddedDatabaseType.H2) | ||
.setScriptEncoding("UTF-8") | ||
.addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql") | ||
.addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql") | ||
.addScript("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql") | ||
.build(); | ||
// @formatter:on | ||
} | ||
|
||
} |
Oops, something went wrong.