2015-02-09 17 views

Odpowiedz

6

Co na ten temat: https://github.com/spring-projects/spring-security-oauth/tree/master/tests/annotation/jdbc (to nie jest MySQL, ale to JDBC, więc transformacja jest banalna)?

+0

Thanks Dave. Wciąż jestem całkiem nowy z bezpieczeństwem wiosennym. Wiosenne uruchamianie ułatwia utworzenie usługi RESTful, ale nadal nie jestem pewien co do implementacji zabezpieczeń za pomocą oAuth2 i referencji bazy danych. Widziałem niektóre przed-wiosenne projekty rozruchowe, które tworzy tabele znaczników i odświeżania w bazie danych. Czy to już nie jest zwyczaj? https://github.com/royclarkson/spring-rest-service-oauth używa uwierzytelniania w pamięci. Czy jest to preferowane powyżej przechowywania żetonów w DB? –

+1

To łącze zawiera pliki SQL dla wspomnianego schematu (są one wykonywane podczas uruchamiania dla bazy danych w pamięci, ale można je wykonać samodzielnie dla mysql). Próbka Roya jest podobna (i równie minimalna), ale magazyn użytkownika w pamięci oczywiście nie będzie przydatny w produkcji dla większości systemów. –

0

Proszę odnieść się do https://github.com/royclarkson/spring-rest-service-oauth/ i wykonać następujące zmiany Wykorzystuje źródło danych podstawowy określony w application.properties,

@Configuration 
public class OAuth2ServerConfiguration { 

    private static final String RESOURCE_ID = "rest_api"; 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends 
      ResourceServerConfigurerAdapter { 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) { 
      resources.resourceId(RESOURCE_ID); 
     } 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      http.authorizeRequests() 
        .antMatchers("/users").hasRole("ADMIN") 
        .antMatchers("/review").authenticated() 
        .antMatchers("/logreview").authenticated() 
        .antMatchers("/oauth/token").authenticated() 
        .and() 
        .csrf() 
        .csrfTokenRepository(csrfTokenRepository()).and() 
        .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class) 
        ; 
      } 

      private Filter csrfHeaderFilter() { 
       return new OncePerRequestFilter() { 

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

         CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class 
           .getName()); 
         if (csrf != null) { 
          Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN"); 
          String token = csrf.getToken(); 
          if (cookie == null || token != null 
            && !token.equals(cookie.getValue())) { 
           cookie = new Cookie("XSRF-TOKEN", token); 
           cookie.setPath("/"); 
           response.addCookie(cookie); 
          } 
         } 
         filterChain.doFilter(request, response); 
        } 
       }; 
      } 
      private CsrfTokenRepository csrfTokenRepository() { 
       HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
       repository.setHeaderName("X-XSRF-TOKEN"); 
       return repository; 
      } 
     } 


    @Configuration 
    @EnableAuthorizationServer 
    protected static class AuthorizationServerConfiguration extends 
      AuthorizationServerConfigurerAdapter { 


     @Autowired 
     @Qualifier("authenticationManagerBean") 
     private AuthenticationManager authenticationManager; 

     @Autowired 
     DataSource dataSource; 


     @Override 
     public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
       throws Exception { 

      endpoints 
       .tokenStore(new JdbcTokenStore(dataSource)) 
       .authenticationManager(this.authenticationManager); 

     } 

     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
      clients 
       .jdbc(dataSource); 
     } 

     @Bean 
     @Primary 
     public DefaultTokenServices tokenServices() { 
      DefaultTokenServices tokenServices = new DefaultTokenServices(); 
      tokenServices.setSupportRefreshToken(true); 
      tokenServices.setAccessTokenValiditySeconds(300); 
      tokenServices.setRefreshTokenValiditySeconds(6000); 
      tokenServices.setTokenStore(new JdbcTokenStore(dataSource)); 
      return tokenServices; 
     } 


    } 
} 
+0

Witam, jestem nowy w tym, ale z pewnych powodów nie pracuję dla żądań POST. {"error": "access_denied", "error_description": "Nie znaleziono oczekiwanego tokenu CSRF. Czy twoja sesja wygasła?"} Działa dobrze z metodami GET –

+0

ma ten sam problem, czy znalazłeś rozwiązanie? –

Powiązane problemy