headz68
Mar 21st, 2012, 12:23 PM
I have a requirement from a customer to use an Oauth2 client not only for resource access but also as a single sign on mechanism. I'm using Spring 3.1.1, Security 3.1.0, and OAuth 1.0.0.M6. The sample application Tonr helped me with a general configuration of an authorization_code resource.
At first, I thought it should be as simple as changing my security configuration to
intercept-url pattern="/**" effectively triggering the OAuth2ClientContextFilter for all requestes. The filter fires correctly and the exception is caught.
...
try {
chain.doFilter(servletRequest, servletResponse);
}
catch (Exception ex) {
OAuth2ProtectedResourceDetails resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(ex);
...
The problem is the in memory OAuth2ProtectedResourceDetails is never created. Instead, a runtime exception is thrown, redirecting the user to the configured application login page and not to the protected resource's login page. The expected exception is AccessTokenRequiredException but ex is type org.springframework.security.access.AccessDeniedEx ception, causing it to just be re thrown.
protected OAuth2ProtectedResourceDetails checkForResourceThatNeedsAuthorization(Exception ex)
throws ServletException, IOException {
Throwable[] causeChain = throwableAnalyzer.determineCauseChain(ex);
AccessTokenRequiredException ase = (AccessTokenRequiredException) throwableAnalyzer.getFirstThrowableOfType(
AccessTokenRequiredException.class, causeChain);
OAuth2ProtectedResourceDetails resourceThatNeedsAuthorization;
if (ase != null) {
resourceThatNeedsAuthorization = ase.getResource();
if (resourceThatNeedsAuthorization == null) {
throw new OAuth2AccessDeniedException(ase.getMessage());
}
}
else {
// Rethrow ServletExceptions and RuntimeExceptions as-is
...
What I'm trying to do is authenticate a user on the resource server and upon successful authentication, create a user on our client user system (for stats and other functions), using a OAuth2RestTemplate to retrieve user data from the resource server.
Any suggestions??
At first, I thought it should be as simple as changing my security configuration to
intercept-url pattern="/**" effectively triggering the OAuth2ClientContextFilter for all requestes. The filter fires correctly and the exception is caught.
...
try {
chain.doFilter(servletRequest, servletResponse);
}
catch (Exception ex) {
OAuth2ProtectedResourceDetails resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(ex);
...
The problem is the in memory OAuth2ProtectedResourceDetails is never created. Instead, a runtime exception is thrown, redirecting the user to the configured application login page and not to the protected resource's login page. The expected exception is AccessTokenRequiredException but ex is type org.springframework.security.access.AccessDeniedEx ception, causing it to just be re thrown.
protected OAuth2ProtectedResourceDetails checkForResourceThatNeedsAuthorization(Exception ex)
throws ServletException, IOException {
Throwable[] causeChain = throwableAnalyzer.determineCauseChain(ex);
AccessTokenRequiredException ase = (AccessTokenRequiredException) throwableAnalyzer.getFirstThrowableOfType(
AccessTokenRequiredException.class, causeChain);
OAuth2ProtectedResourceDetails resourceThatNeedsAuthorization;
if (ase != null) {
resourceThatNeedsAuthorization = ase.getResource();
if (resourceThatNeedsAuthorization == null) {
throw new OAuth2AccessDeniedException(ase.getMessage());
}
}
else {
// Rethrow ServletExceptions and RuntimeExceptions as-is
...
What I'm trying to do is authenticate a user on the resource server and upon successful authentication, create a user on our client user system (for stats and other functions), using a OAuth2RestTemplate to retrieve user data from the resource server.
Any suggestions??