Skip to content

Commit

Permalink
Make OAuth2ClientAuthenticationProcessingFilter behave more like a re…
Browse files Browse the repository at this point in the history
…source server
  • Loading branch information
dsyer committed Aug 27, 2014
1 parent 0b2e90a commit c7561fb
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
Expand All @@ -30,7 +32,8 @@
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetailsSource;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.util.Assert;
Expand All @@ -48,6 +51,8 @@ public class OAuth2ClientAuthenticationProcessingFilter extends AbstractAuthenti

private ResourceServerTokenServices tokenServices;

private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new OAuth2AuthenticationDetailsSource();

/**
* Reference to a CheckTokenServices that can validate an OAuth2AccessToken
*
Expand All @@ -65,10 +70,11 @@ public void setTokenServices(ResourceServerTokenServices tokenServices) {
public void setRestTemplate(OAuth2RestOperations restTemplate) {
this.restTemplate = restTemplate;
}

public OAuth2ClientAuthenticationProcessingFilter(String defaultFilterProcessesUrl) {
super(defaultFilterProcessesUrl);
setAuthenticationManager(new OAuth2AuthenticationManager());
setAuthenticationManager(new NoopAuthenticationManager());
setAuthenticationDetailsSource(authenticationDetailsSource);
}

@Override
Expand All @@ -84,6 +90,10 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
try {
OAuth2Authentication result = tokenServices.loadAuthentication(accessToken.getValue());
if (authenticationDetailsSource!=null) {
request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, accessToken.getValue());
result.setDetails(authenticationDetailsSource.buildDetails(request));
}
return result;
}
catch (InvalidTokenException e) {
Expand All @@ -104,5 +114,15 @@ protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServle
super.unsuccessfulAuthentication(request, response, failed);
}
}

private static class NoopAuthenticationManager implements AuthenticationManager {

@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
throw new UnsupportedOperationException("No authentication should be done with this AuthenticationManager");
}

}

}

0 comments on commit c7561fb

Please sign in to comment.