Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2011 the original author or authors.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
   * the License. You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  * specific language governing permissions and limitations under the License.
  */
 package org.springframework.security.oauth2.provider.authentication;
 
 
 
A pre-authemtication filter for OAuth2 protected resources. Extracts an OAuth2 token from the in coming request and uses it to populate the Spring Security context with an org.springframework.security.oauth2.provider.OAuth2Authentication (if used in conjunction with an OAuth2AuthenticationManager).

Author(s):
Dave Syer
 
 
 	private final static Log logger = LogFactory.getLog(OAuth2AuthenticationProcessingFilter.class);
 
 
 

Parameters:
authenticationEntryPoint the authentication entry point to set
 
 	public void setAuthenticationEntryPoint(AuthenticationEntryPoint authenticationEntryPoint) {
 		this. = authenticationEntryPoint;
 	}

Parameters:
authenticationManager the authentication manager to set (mandatory with no default)
 
 	public void setAuthenticationManager(AuthenticationManager authenticationManager) {
 		this. = authenticationManager;
 	}

    

Parameters:
authenticationDetailsSource The AuthenticationDetailsSource to use
 
     public void setAuthenticationDetailsSource(AuthenticationDetailsSource<HttpServletRequest,?> authenticationDetailsSource) {
         Assert.notNull(authenticationDetailsSource"AuthenticationDetailsSource required");
         this. = authenticationDetailsSource;
     }
 
 	public void afterPropertiesSet() {
 		Assert.state( != null"AuthenticationManager is required");
 	}
 
 	public void doFilter(ServletRequest reqServletResponse resFilterChain chainthrows IOException,
 
 		final boolean debug = .isDebugEnabled();
 		final HttpServletRequest request = (HttpServletRequestreq;
 		final HttpServletResponse response = (HttpServletResponseres;
 
 		try {
 
 			String tokenValue = parseToken(request);
 			if (tokenValue == null) {
 				if (debug) {
					.debug("No token in request, will continue chain.");
				}
			}
			else {
						tokenValue"");
				Authentication authResult = .authenticate(authentication);
				if (debug) {
					.debug("Authentication success: " + authResult);
				}
				SecurityContextHolder.getContext().setAuthentication(authResult);
			}
		}
		catch (OAuth2Exception failed) {
			SecurityContextHolder.clearContext();
			if (debug) {
				.debug("Authentication request failed: " + failed);
			}
			return;
		}
		chain.doFilter(requestresponse);
	}
	protected String parseToken(HttpServletRequest request) {
		// first check the header...
		String token = parseHeaderToken(request);
		// bearer type allows a request parameter as well
		if (token == null) {
			.debug("Token not found in headers. Trying request parameters.");
			if (token == null) {
				.debug("Token not found in request parameters.  Not an OAuth2 request.");
			}
		}
		return token;
	}

Parse the OAuth header parameters. The parameters will be oauth-decoded.

Parameters:
request The request.
Returns:
The parsed parameters, or null if no OAuth authorization header was supplied.
		@SuppressWarnings("unchecked")
		Enumeration<Stringheaders = request.getHeaders("Authorization");
		while (headers.hasMoreElements()) { // typically there is only one (most servers enforce that)
			String value = headers.nextElement();
				String authHeaderValue = value.substring(..length()).trim();
				int commaIndex = authHeaderValue.indexOf(',');
				if (commaIndex > 0) {
					authHeaderValue = authHeaderValue.substring(0, commaIndex);
				}
				return authHeaderValue;
			}
			else {
				// todo: support additional authorization schemes for different token types, e.g. "MAC" specified by
				// http://tools.ietf.org/html/draft-hammer-oauth-v2-mac-token
			}
		}
		return null;
	}
	public void init(FilterConfig filterConfigthrows ServletException {
	}
	public void destroy() {
	}
New to GrepCode? Check out our FAQ X