2016-06-09 6 views
0

Ich habe eine Implementierung in einer Feder Anwendung mit Oauth, aber einen Token erhalten kann:Oauth Ausgabe "nicht autorisiert" in Spring 4

OAuth2ServerConfigAuthorizationServerConfiguration.java

@Configuration 
public class OAuth2ServerConfigAuthorizationServerConfiguration { 

    private static final String RESOURCE_ID = "restservice"; 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends 
      ResourceServerConfigurerAdapter { 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) { 
      // @formatter:off 
      resources 
       .resourceId(RESOURCE_ID); 
      // @formatter:on 
     } 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      // @formatter:off 
      http 
       .authorizeRequests() 
        .antMatchers("/entuzona/**").authenticated(); 
      // @formatter:on 
     } 
} 

    @Configuration 
    @EnableAuthorizationServer 
    protected static class AuthorizationServerConfiguration extends 
      AuthorizationServerConfigurerAdapter { 

     private TokenStore tokenStore = new InMemoryTokenStore(); 

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

     @Override 
     public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
       throws Exception { 
      // @formatter:off 
      endpoints 
       .tokenStore(this.tokenStore) 
       .authenticationManager(this.authenticationManager); 
      // @formatter:on 
     } 

     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
      // @formatter:off 
      clients 
       .inMemory() 
        .withClient("clientapp") 
         .authorizedGrantTypes("password","refresh_token") 
         .authorities("USER") 
         .scopes("read", "write") 
         .resourceIds(RESOURCE_ID) 
         .secret("123456"); 
      // @formatter:on 
     } 

     @Bean 
     @Primary 
     public DefaultTokenServices tokenServices() { 
      DefaultTokenServices tokenServices = new DefaultTokenServices(); 
      tokenServices.setSupportRefreshToken(true); 
      tokenServices.setTokenStore(this.tokenStore); 
      return tokenServices; 
     } 

    } 
} 

OAuth2ClientConfig.java

@Configuration 
@ComponentScan("com.sprhib") 
@EnableWebSecurity 
@EnableWebMvcSecurity 
public class OAuth2ClientConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.inMemoryAuthentication().withUser("teste").password("teste").authorities("USER"); 
    } 

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

Ich mache Post und bekommt mit Postboten:

POS T Anfrage:

http://localhost:8080/entuzona/oauth/token 
grant_type: password 
username: teste 
password: teste 

Und mit einem GET:

http://localhost:8080/entuzona/oauth/token?grant_type=password&cliend_id=clientapp&client_secret=123456&username=teste&password=teste 

{ 
    "error": "unauthorized", 
    "error_description": "Full authentication is required to access this resource" 
} 

Wissen Sie, warum ich in beiden Fällen diese Störung erhalten? Shoul Ich habe etwas in den Header gelegt, um das Token zu bekommen?

Versionen.

<dependency> 
     <groupId>org.springframework.security.oauth</groupId> 
     <artifactId>spring-security-oauth2</artifactId> 
     <version>2.0.0.RELEASE</version> 
    </dependency> 

<properties> 
    <hibernate.version>4.2.0.Final</hibernate.version> 
    <mysql.connector.version>5.1.21</mysql.connector.version> 
    <spring.version>4.0.9.RELEASE</spring.version> 
    <spring.security.version>3.2.5.RELEASE</spring.security.version> 
</properties> 

Danke.

Antwort

0

Es fehlte diesen Code:

@Override public void configure (AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.allowFormAuthenticationForClients(); }

Verwandte Themen