2015-08-07 3 views
2

Я борюсь с собственным проектом, пытаясь настроить безопасность весеннего отдыха и oauth. Кто-то может сказать, что мне не нужен oauth для этого простого проекта, но я хочу практиковать его.безопасность весеннего отдыха oauth неподдерживаемый тип гранта

Я хочу получить токен обновления и доступа от конечной точки, но я получаю сообщение об ошибке: неподдерживаемый тип гранта: пароль. Я искал в Интернете, но не смог найти решение для моей конкретной проблемы.

curl -u client:123456 http://localhost:8080/artwork/oauth/token -d 'grant_type=password&username=rest&password=rest' -X POST -H "Content-Type:application/x-www-form-urlencoded" -v 

Но когда я называю его с типом гранта: client_credentials он возвращает мне маркер доступа, но, как я уже говорил, я потребуется обновление маркера тоже.

Это вариант az, который я полностью не понимаю полностью, но я читаю об этом.

@Autowired 
private CustomUsersDetailService userDetailsService; 

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.inMemoryAuthentication() 
    .withUser("rest") 
     .password("rest") 
     .roles("REST") 
     .and() 
    .withUser("historian") 
     .password("historian") 
     .roles("HIS"); 
} 

@Override 
@Bean 
public AuthenticationManager authenticationManagerBean() throws Exception { 
    return super.authenticationManagerBean(); 
} 

private static final String RESOURCE_ID = "restservice"; 

@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("/oauth/token").permitAll() 
     .and() 
     .authorizeRequests().antMatchers("/**").access("hasRole('ROLE_HIS') or hasRole('ROLE_REST')");       
    } 

} 

@Configuration 
@EnableAuthorizationServer 
protected static class AuthorizationServerConfiguration extends 
     AuthorizationServerConfigurerAdapter { 

    private TokenStore tokenStore = new InMemoryTokenStore(); 

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

    @Autowired 
    private CustomUsersDetailService userDetailsService; 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
      throws Exception { 
     endpoints 
      .tokenStore(this.tokenStore); 

    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients 
      .inMemory() 
       .withClient("client") 
        .authorizedGrantTypes("password","refresh_token","client_credentials","authorization_code") 
        .authorities("ROLE_REST") 
        .scopes("read", "write") 
        .resourceIds(RESOURCE_ID) 
        .secret("123456"); 
    } 

код от переднего конца, только метод пост:

$scope.post = function() { 

     var config = { 
       method: 'POST', 
       url: 'http://localhost:8080/artwork/oauth/token', 
       headers: { 
        'Content-Type': 'application/x-www-form-urlencoded', 
        'Authorization' : "Basic " + Base64.encode("client:123456") 
       }, 
       params: {      
        grant_type: "password", 
        username: "rest", 
        password: "rest" 
       } 
     } 

     $http(config).success(onsuc).error(error); 
    }; 

Если вы видите что-то ничего, дайте мне знать. Спасибо!

+0

Вы смогли найти ответ на свой вопрос? Сейчас я столкнулся с той же проблемой. – Maksim

+0

Я тоже хотел бы знать, у меня такая же проблема @Maksim –

ответ

2

Вы можете определить составной символ, который должен содержать RefreshTokenGranter. Этот образец может помочь вам.

@Configuration 
@EnableAuthorizationServer 
public class SecurityOauth2Config extends AuthorizationServerConfigurerAdapter { 

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

     endpoints 
      .tokenServices(authorizationServerTokenServices()) 
      .tokenStore(tokenStore()) 
      .tokenGranter(tokenGranter()) 
      .requestFactory(oAuth2RequestFactory()) 
      .setClientDetailsService(clientDetailsJdbcService()); 

    } 


    @Bean 
    public TokenGranter tokenGranter() { 

     ClientDetailsService clientDetails = clientDetailsJdbcService(); 
     AuthorizationServerTokenServices tokenServices = authorizationServerTokenServices(); 
     AuthenticationManager authenticationManager = authenticationManagerOauth2User(); 

     OAuth2RequestFactory oAuth2RequestFactory = oAuth2RequestFactory(); 

     List<TokenGranter> tokenGranters = new ArrayList<TokenGranter>(); 

     tokenGranters.add(new RefreshTokenGranter(tokenServices, clientDetails, oAuth2RequestFactory)); 
     tokenGranters.add(new ImplicitTokenGranter(tokenServices, clientDetails, oAuth2RequestFactory)); 
     tokenGranters.add(new ClientCredentialsTokenGranter(tokenServices, clientDetails, oAuth2RequestFactory)); 

     tokenGranters.add(new ResourceOwnerPasswordTokenGranter(authenticationManager, tokenServices, 
        clientDetails, oAuth2RequestFactory)); 

     return new CompositeTokenGranter(tokenGranters); 
    } 
... 
Смежные вопросы