oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/BearerTokenAuthenticationFilter.java
@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		final boolean debug = this.logger.isDebugEnabled();

		String token;

		try {
			token = this.bearerTokenResolver.resolve(request);
		} catch ( OAuth2AuthenticationException invalid ) {
			this.authenticationEntryPoint.commence(request, response, invalid);
			return;
		}

		if (token == null) {
			filterChain.doFilter(request, response);
			return;
		}

		BearerTokenAuthenticationToken authenticationRequest = new BearerTokenAuthenticationToken(token);

		authenticationRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));

		try {
			AuthenticationManager authenticationManager = this.authenticationManagerResolver.resolve(request);
			Authentication authenticationResult = authenticationManager.authenticate(authenticationRequest);

			SecurityContext context = SecurityContextHolder.createEmptyContext();
			context.setAuthentication(authenticationResult);
			SecurityContextHolder.setContext(context);

			filterChain.doFilter(request, response);
		} catch (AuthenticationException failed) {
			SecurityContextHolder.clearContext();

			if (debug) {
				this.logger.debug("Authentication request for failed: " + failed);
			}

			this.authenticationEntryPoint.commence(request, response, failed);
		}
	}
@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		final boolean debug = this.logger.isDebugEnabled();

		String token;

		try {
			token = this.bearerTokenResolver.resolve(request);
		} catch ( OAuth2AuthenticationException invalid ) {
			this.authenticationEntryPoint.commence(request, response, invalid);
			return;
		}

		if (token == null) {
			filterChain.doFilter(request, response);
			return;
		}

		BearerTokenAuthenticationToken authenticationRequest = new BearerTokenAuthenticationToken(token);

		authenticationRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));

		try {
			AuthenticationManager authenticationManager = this.authenticationManagerResolver.resolve(request);
			Authentication authenticationResult = authenticationManager.authenticate(authenticationRequest);

			SecurityContext context = SecurityContextHolder.createEmptyContext();
			context.setAuthentication(authenticationResult);
			SecurityContextHolder.setContext(context);

			filterChain.doFilter(request, response);
		} catch (AuthenticationException failed) {
			SecurityContextHolder.clearContext();

			if (debug) {
				this.logger.debug("Authentication request for failed: " + failed);
			}

			this.authenticationEntryPoint.commence(request, response, failed);
		}
	}
@ Override protected void doFilterInternal ( HttpServletRequest request , HttpServletResponse response , FilterChain filterChain ) throws ServletException , IOException { final boolean debug = this . logger . isDebugEnabled ( ) ; String token ; try { token = this . bearerTokenResolver . resolve ( request ) ; } catch ( OAuth2AuthenticationException invalid ) { this . authenticationEntryPoint . commence ( request , response , invalid ) ; return ; } if ( token == null ) { filterChain . doFilter ( request , response ) ; return ; } BearerTokenAuthenticationToken authenticationRequest = new BearerTokenAuthenticationToken ( token ) ; authenticationRequest . setDetails ( this . authenticationDetailsSource . buildDetails ( request ) ) ; try { AuthenticationManager authenticationManager = this . authenticationManagerResolver . resolve ( request ) ; Authentication authenticationResult = authenticationManager . authenticate ( authenticationRequest ) ; SecurityContext context = SecurityContextHolder . createEmptyContext ( ) ; context . setAuthentication ( authenticationResult ) ; SecurityContextHolder . setContext ( context ) ; filterChain . doFilter ( request , response ) ; } catch ( AuthenticationException failed ) { SecurityContextHolder . clearContext ( ) ; if ( debug ) { this . logger . debug ( "Authentication request for failed: " + failed ) ; } this . authenticationEntryPoint . commence ( request , response , failed ) ; } }
Extract any <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer Token</a> from
the request and attempt an authentication.

@param request
@param response
@param filterChain
@throws ServletException
@throws IOException
Extract any <a href = https : // tools . ietf . org / html / rfc6750#section - 1 . 2 target = _blank > Bearer Token< / a > from the request and attempt an authentication .