forked from Baeldung/spring-security-oauth
-
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.
- Loading branch information
Showing
7 changed files
with
113 additions
and
16 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
19 changes: 19 additions & 0 deletions
19
...security-oauth-resource/src/main/java/org/baeldung/config/CustomAccessTokenConverter.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,19 @@ | ||
package org.baeldung.config; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CustomAccessTokenConverter extends DefaultAccessTokenConverter { | ||
|
||
@Override | ||
public OAuth2Authentication extractAuthentication(Map<String, ?> claims) { | ||
OAuth2Authentication authentication = super.extractAuthentication(claims); | ||
authentication.setDetails(claims); | ||
return authentication; | ||
} | ||
|
||
} |
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
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
61 changes: 61 additions & 0 deletions
61
...y-oauth-resource/src/test/java/org/baeldung/test/AuthenticationClaimsIntegrationTest.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,61 @@ | ||
package org.baeldung.test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.baeldung.config.ResourceServerApplication; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import com.jayway.restassured.RestAssured; | ||
import com.jayway.restassured.response.Response; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = ResourceServerApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) | ||
public class AuthenticationClaimsIntegrationTest { | ||
|
||
@Autowired | ||
private JwtTokenStore tokenStore; | ||
|
||
@Test | ||
public void whenTokenDontContainIssuer_thenSuccess() { | ||
final String tokenValue = obtainAccessToken("fooClientIdPassword", "john", "123"); | ||
final OAuth2Authentication auth = tokenStore.readAuthentication(tokenValue); | ||
System.out.println(tokenValue); | ||
System.out.println(auth); | ||
assertTrue(auth.isAuthenticated()); | ||
System.out.println(auth.getDetails()); | ||
|
||
Map<String, Object> details = (Map<String, Object>) auth.getDetails(); | ||
assertTrue(details.containsKey("organization")); | ||
System.out.println(details.get("organization")); | ||
} | ||
|
||
private String obtainAccessToken(String clientId, String username, String password) { | ||
final Map<String, String> params = new HashMap<String, String>(); | ||
params.put("grant_type", "password"); | ||
params.put("client_id", clientId); | ||
params.put("username", username); | ||
params.put("password", password); | ||
final Response response = RestAssured.given() | ||
.auth() | ||
.preemptive() | ||
.basic(clientId, "secret") | ||
.and() | ||
.with() | ||
.params(params) | ||
.when() | ||
.post("http://localhost:8081/spring-security-oauth-server/oauth/token"); | ||
return response.jsonPath() | ||
.getString("access_token"); | ||
} | ||
|
||
} |
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
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