0

Я пишу фильтр, который перехватит вызов Restful API, извлечет токен-носитель и вызовет сервер авторизации для проверки.Spring Boot Oauth2 Проверка маркера доступа для учетных данных владельца ресурса Грант

Я не смог найти его в Spring Boot, который делает это из коробки, но я уверен, что есть более чистый способ сделать это. вот что у меня есть (псевдо-код):

public class SOOTokenValidationFilter extends OncePerRequestFilter { 

@Override 
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 
     throws ServletException, IOException { 

    String xAuth = request.getHeader("Authorization"); 

    // validate the value in xAuth 
    if(isValid(xAuth) == false){ 
     throw new SecurityException(); 
    } 

    // Create our Authentication and set it in Spring 
     Authentication auth = new Authentication(); 
     SecurityContextHolder.getContext().setAuthentication(auth);    

    filterChain.doFilter(request, response); 

} 
private boolean isValid (String token){ 

    // make a call to SSO passing the access token and 
    // return true if validated 
    return true; 
} 

}

ответ

1

уроков, Spring Security oauth2 документация неадекватна, забыть о попытке использовать рамки без полного прочесывать исходный код. С другой стороны, код хорошо написан и легко подходит к Dave Syer.

1. Here is my config: 

protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable();     
     http.authorizeRequests() 
     .antMatchers("/") 
     .permitAll() 
     .and()  
     .addFilterBefore(getOAuth2AuthenticationProcessingFilter(), UsernamePasswordAuthenticationFilter.class) 
     .exceptionHandling();       
    } 
2. Here is my getOAuth2AuthenticationProcessingFilter method 
private OAuth2AuthenticationProcessingFilter getOAuth2AuthenticationProcessingFilter(){  

     // configure token Extractor 
     BearerTokenExtractor tokenExtractor = new BearerTokenExtractor(); 
     // configure Auth manager 
     OAuth2Authenti`enter code here`cationManager manager = new OAuth2AuthenticationManager(); 
     // configure RemoteTokenServices with your client Id and auth server endpoint 
     manager.setTokenServices(remoteTokenServices); 

     OAuth2AuthenticationProcessingFilter filter = new OAuth2AuthenticationProcessingFilter(); 
     filter.setTokenExtractor(tokenExtractor);   
     filter.setAuthenticationManager(manager); 
     return filter; 

    } 

и все это люди.