Skip to content

Commit fd2f3f8

Browse files
committed
update spring oauth
1 parent 03e182d commit fd2f3f8

File tree

23 files changed

+745
-40
lines changed

23 files changed

+745
-40
lines changed

spring-oauth/src/main/java/org/baeldung/web/RedditController.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

spring-security-login-and-registration/src/main/resources/persistence.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
################### DataSource Configuration ##########################
22
jdbc.driverClassName=com.mysql.jdbc.Driver
3-
jdbc.url=jdbc:mysql://localhost:3306/registration_02?createDatabaseIfNotExist=true
3+
jdbc.url=jdbc:mysql://localhost:3606/registration_02?createDatabaseIfNotExist=true
44
jdbc.user=tutorialuser
55
jdbc.pass=tutorialmy5ql
66
init-db=false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>spring-oauth</name>
3+
<name>spring-security-oauth</name>
44
<comment></comment>
55
<projects>
66
</projects>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>org.baeldung</groupId>
4-
<artifactId>spring-oauth</artifactId>
4+
<artifactId>spring-security-oauth</artifactId>
55
<version>0.1-SNAPSHOT</version>
66

7-
<name>spring-oauth</name>
7+
<name>spring-security-oauth</name>
88
<packaging>war</packaging>
99

1010
<dependencies>
@@ -251,7 +251,7 @@
251251
</dependencies>
252252

253253
<build>
254-
<finalName>spring-oauth</finalName>
254+
<finalName>spring-security-oauth</finalName>
255255
<resources>
256256
<resource>
257257
<directory>src/main/resources</directory>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.baeldung.config;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
import org.springframework.security.access.AccessDeniedException;
7+
import org.springframework.security.authentication.AnonymousAuthenticationToken;
8+
import org.springframework.security.authentication.InsufficientAuthenticationException;
9+
import org.springframework.security.core.Authentication;
10+
import org.springframework.security.core.context.SecurityContextHolder;
11+
import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
12+
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
13+
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
14+
import org.springframework.security.oauth2.client.token.AccessTokenProvider;
15+
import org.springframework.security.oauth2.client.token.AccessTokenRequest;
16+
import org.springframework.security.oauth2.client.token.ClientTokenServices;
17+
import org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport;
18+
import org.springframework.security.oauth2.common.OAuth2AccessToken;
19+
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
20+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
21+
22+
public class MyAccessTokenProviderChain extends OAuth2AccessTokenSupport implements AccessTokenProvider {
23+
24+
private final List<AccessTokenProvider> chain;
25+
26+
private ClientTokenServices clientTokenServices;
27+
28+
public MyAccessTokenProviderChain(List<? extends AccessTokenProvider> chain) {
29+
this.chain = chain == null ? Collections.<AccessTokenProvider> emptyList() : Collections.unmodifiableList(chain);
30+
}
31+
32+
public void setClientTokenServices(ClientTokenServices clientTokenServices) {
33+
this.clientTokenServices = clientTokenServices;
34+
}
35+
36+
public boolean supportsResource(OAuth2ProtectedResourceDetails resource) {
37+
for (AccessTokenProvider tokenProvider : chain) {
38+
if (tokenProvider.supportsResource(resource)) {
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
44+
45+
public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource) {
46+
for (AccessTokenProvider tokenProvider : chain) {
47+
if (tokenProvider.supportsRefresh(resource)) {
48+
return true;
49+
}
50+
}
51+
return false;
52+
}
53+
54+
public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails resource, AccessTokenRequest request) throws UserRedirectRequiredException, AccessDeniedException {
55+
System.out.println("Obtain new token=====");
56+
OAuth2AccessToken accessToken = null;
57+
OAuth2AccessToken existingToken = null;
58+
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
59+
System.out.println("The authentication is ---- " + auth);
60+
if (auth instanceof AnonymousAuthenticationToken) {
61+
if (!resource.isClientOnly()) {
62+
throw new InsufficientAuthenticationException("Authentication is required to obtain an access token (anonymous not allowed)");
63+
}
64+
}
65+
66+
if (resource.isClientOnly() || (auth != null && auth.isAuthenticated())) {
67+
existingToken = request.getExistingToken();
68+
System.out.println("checking existing token =====");
69+
if (existingToken == null && clientTokenServices != null) {
70+
System.out.println("get existing token from clientTokenServices ==== ");
71+
existingToken = clientTokenServices.getAccessToken(resource, auth);
72+
}
73+
74+
if (existingToken != null) {
75+
if (existingToken.isExpired()) {
76+
System.out.println("expired token");
77+
if (clientTokenServices != null) {
78+
clientTokenServices.removeAccessToken(resource, auth);
79+
}
80+
OAuth2RefreshToken refreshToken = existingToken.getRefreshToken();
81+
if (refreshToken != null) {
82+
System.out.println("let's refresh it");
83+
accessToken = refreshAccessToken(resource, refreshToken, request);
84+
}
85+
} else {
86+
System.out.println("use existing because not expired yet");
87+
accessToken = existingToken;
88+
}
89+
}
90+
}
91+
92+
if (accessToken == null) {
93+
System.out.println("no token so let get it");
94+
accessToken = obtainNewAccessTokenInternal(resource, request);
95+
96+
if (accessToken == null) {
97+
throw new IllegalStateException("An OAuth 2 access token must be obtained or an exception thrown.");
98+
}
99+
}
100+
101+
if (clientTokenServices != null && auth != null && auth.isAuthenticated()) {
102+
clientTokenServices.saveAccessToken(resource, auth, accessToken);
103+
}
104+
105+
return accessToken;
106+
}
107+
108+
protected OAuth2AccessToken obtainNewAccessTokenInternal(OAuth2ProtectedResourceDetails details, AccessTokenRequest request) throws UserRedirectRequiredException, AccessDeniedException {
109+
110+
if (request.isError()) {
111+
throw OAuth2Exception.valueOf(request.toSingleValueMap());
112+
}
113+
114+
for (AccessTokenProvider tokenProvider : chain) {
115+
if (tokenProvider.supportsResource(details)) {
116+
System.out.println("we will use this provider to get it => " + tokenProvider.getClass().getName());
117+
return tokenProvider.obtainAccessToken(details, request);
118+
}
119+
}
120+
121+
throw new OAuth2AccessDeniedException("Unable to obtain a new access token for resource '" + details.getId() + "'. The provider manager is not configured to support it.", details);
122+
}
123+
124+
public OAuth2AccessToken refreshAccessToken(OAuth2ProtectedResourceDetails resource, OAuth2RefreshToken refreshToken, AccessTokenRequest request) throws UserRedirectRequiredException {
125+
for (AccessTokenProvider tokenProvider : chain) {
126+
if (tokenProvider.supportsRefresh(resource)) {
127+
System.out.println("we will use this provider to refresh it => " + tokenProvider.getClass().getName());
128+
return tokenProvider.refreshAccessToken(resource, refreshToken, request);
129+
}
130+
}
131+
throw new OAuth2AccessDeniedException("Unable to obtain a new access token for resource '" + resource.getId() + "'. The provider manager is not configured to support it.", resource);
132+
}
133+
134+
}

0 commit comments

Comments
 (0)