Merge pull request #138 from Doha2012/master
modify spring security oauth
This commit is contained in:
commit
33f0d77c71
|
@ -2,7 +2,6 @@ package org.baeldung.config;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -20,7 +19,6 @@ import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedExc
|
||||||
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
|
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
|
||||||
import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException;
|
import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException;
|
||||||
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
|
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
|
||||||
import org.springframework.security.oauth2.client.token.AccessTokenProvider;
|
|
||||||
import org.springframework.security.oauth2.client.token.AccessTokenRequest;
|
import org.springframework.security.oauth2.client.token.AccessTokenRequest;
|
||||||
import org.springframework.security.oauth2.client.token.DefaultRequestEnhancer;
|
import org.springframework.security.oauth2.client.token.DefaultRequestEnhancer;
|
||||||
import org.springframework.security.oauth2.client.token.RequestEnhancer;
|
import org.springframework.security.oauth2.client.token.RequestEnhancer;
|
||||||
|
@ -34,7 +32,7 @@ import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.client.ResponseExtractor;
|
import org.springframework.web.client.ResponseExtractor;
|
||||||
|
|
||||||
public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider implements AccessTokenProvider {
|
public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider {
|
||||||
|
|
||||||
private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator();
|
private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator();
|
||||||
|
|
||||||
|
@ -42,31 +40,6 @@ public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAcc
|
||||||
|
|
||||||
private RequestEnhancer authorizationRequestEnhancer = new DefaultRequestEnhancer();
|
private RequestEnhancer authorizationRequestEnhancer = new DefaultRequestEnhancer();
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setAuthorizationRequestEnhancer(RequestEnhancer authorizationRequestEnhancer) {
|
|
||||||
this.authorizationRequestEnhancer = authorizationRequestEnhancer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setScopePrefix(String scopePrefix) {
|
|
||||||
this.scopePrefix = scopePrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setStateKeyGenerator(StateKeyGenerator stateKeyGenerator) {
|
|
||||||
this.stateKeyGenerator = stateKeyGenerator;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean supportsResource(OAuth2ProtectedResourceDetails resource) {
|
|
||||||
return resource instanceof AuthorizationCodeResourceDetails && "authorization_code".equals(resource.getGrantType());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource) {
|
|
||||||
return supportsResource(resource);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String obtainAuthorizationCode(OAuth2ProtectedResourceDetails details, AccessTokenRequest request) throws UserRedirectRequiredException, UserApprovalRequiredException, AccessDeniedException, OAuth2AccessDeniedException {
|
public String obtainAuthorizationCode(OAuth2ProtectedResourceDetails details, AccessTokenRequest request) throws UserRedirectRequiredException, UserApprovalRequiredException, AccessDeniedException, OAuth2AccessDeniedException {
|
||||||
|
|
||||||
|
@ -127,15 +100,6 @@ public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAcc
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
|
|
||||||
return new ResponseExtractor<ResponseEntity<Void>>() {
|
|
||||||
public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
|
|
||||||
return new ResponseEntity<Void>(response.getHeaders(), response.getStatusCode());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails details, AccessTokenRequest request) throws UserRedirectRequiredException, UserApprovalRequiredException, AccessDeniedException, OAuth2AccessDeniedException {
|
public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails details, AccessTokenRequest request) throws UserRedirectRequiredException, UserApprovalRequiredException, AccessDeniedException, OAuth2AccessDeniedException {
|
||||||
|
|
||||||
|
@ -287,9 +251,4 @@ public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAcc
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected UserApprovalRequiredException getUserApprovalSignal(AuthorizationCodeResourceDetails resource, AccessTokenRequest request) {
|
|
||||||
String message = String.format("Do you approve the client '%s' to access your resources with scope=%s", resource.getClientId(), resource.getScope());
|
|
||||||
return new UserApprovalRequiredException(resource.getUserAuthorizationUri(), Collections.singletonMap(OAuth2Utils.USER_OAUTH_APPROVAL, message), resource.getClientId(), resource.getScope());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package org.baeldung.web;
|
package org.baeldung.web;
|
||||||
|
|
||||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||||
|
import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException;
|
||||||
|
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
@ -14,11 +16,19 @@ public class RedditController {
|
||||||
private OAuth2RestTemplate redditRestTemplate;
|
private OAuth2RestTemplate redditRestTemplate;
|
||||||
|
|
||||||
@RequestMapping("/info")
|
@RequestMapping("/info")
|
||||||
public String getInfo(Model model) throws Exception {
|
public String getInfo(Model model) {
|
||||||
|
try {
|
||||||
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", String.class);
|
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", String.class);
|
||||||
JsonNode node = new ObjectMapper().readTree(result);
|
JsonNode node = new ObjectMapper().readTree(result);
|
||||||
String name = node.get("name").asText();
|
String name = node.get("name").asText();
|
||||||
model.addAttribute("info", name);
|
model.addAttribute("info", name);
|
||||||
|
} catch (UserApprovalRequiredException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (UserRedirectRequiredException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Exception e) {
|
||||||
|
model.addAttribute("error", e.getLocalizedMessage());
|
||||||
|
}
|
||||||
return "reddit";
|
return "reddit";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
<head>
|
<head>
|
||||||
|
@ -6,7 +5,12 @@
|
||||||
<title>Spring Security OAuth</title>
|
<title>Spring Security OAuth</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<c:choose>
|
||||||
|
<c:when test="${info != null}">
|
||||||
<h1>Your Reddit Info</h1>
|
<h1>Your Reddit Info</h1>
|
||||||
<b>Your reddit username is </b>${info}
|
<b>Your reddit username is </b>${info}
|
||||||
|
</c:when>
|
||||||
|
<c:otherwise> Sorry, error occurred.</c:otherwise>
|
||||||
|
</c:choose>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -1,12 +0,0 @@
|
||||||
|
|
||||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
||||||
<head>
|
|
||||||
|
|
||||||
<title>Spring Security OAuth</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Test</h1>
|
|
||||||
<b>Test </b>${info}
|
|
||||||
</body>
|
|
||||||
</html>
|
|
Loading…
Reference in New Issue