1

Ich arbeite an einer REST-API, die auf localhost hört und Spring Security einschließen möchte. Password Grant und Client Credentials Grant funktionieren einwandfrei und ich kann gesicherte Daten von/smarthouse und/smarthouse2 überprüfen.Berechtigungscode gewähren

Obwohl, wenn ich versuche, Autorisierung Code Grant durch Postbote zu verwenden, gibt es mir den gleichen Fehler und ich habe überall überprüft. Mein Projekt ist hier: https://github.com/sharjak/Smarthouse. Die Aktion geschieht im Ordner Demo-Anwendung.

Authorization code in Postman

Mein Code für die Autorisierung und Ressourcen-Server:

@Configuration 
public class OAuth2ServerConfig { 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      http 
        .anonymous().disable() 
        .csrf().disable() 
        .authorizeRequests() 
        .anyRequest() 
        .authenticated().and() 
        .formLogin(); 
      } 
     } 

    @Configuration 
    @EnableAuthorizationServer 
    protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { 

     @Autowired 
     private TokenStore tokenStore; 

     @Autowired 
     private AuthenticationManager authenticationManager; 

     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 

      clients.inMemory().withClient("my-trusted-client") 
        .authorizedGrantTypes("password","authorization_code","refresh_token", "implicit") 
        .authorities("ROLE_CLIENT","ROLE_TRUSTED_CLIENT","ROLE_USER") 
        .scopes("read", "write", "trust") 
        .resourceIds("oauth2-resource") 
        .secret("secret") 
        .accessTokenValiditySeconds(6000) 
        .and() 

        .withClient("my-client") 
        .authorizedGrantTypes("authorization_code", "implicit") 
        .authorities("ROLE_CLIENT", "ROLE_USER") 
        .scopes("read","trust", "write") 
        .resourceIds("oauth2-resource") 
        .accessTokenValiditySeconds(6000) 
        .and() 

        .withClient("my-client-with-secret") 
        .authorizedGrantTypes("client_credentials","password") 
        .authorities("ROLE_CLIENT", "ROLE_USER") 
        .scopes("read", "trust", "write") 
        .resourceIds("oauth2-resource") 
        .secret("secret") 
        .accessTokenValiditySeconds(6000); 
     } 

     @Bean 
     public TokenStore tokenStore() { 
      return new InMemoryTokenStore(); 
     } 

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

     @Override 
     public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
      oauthServer.checkTokenAccess("permitAll()"); 
     } 
    } 
} 

-Code für WebSecurity Server:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private ClientDetailsService clientDetailsService; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http 
       .anonymous().disable() 
       .csrf().disable() 
       .authorizeRequests() 
       .antMatchers("/smarthouse", "smarthouse2", "/user").permitAll() 
       .and() 
       .formLogin(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication() 
       .withUser("admin").password("password").roles("ADMIN") 
       .and() 
       .withUser("sander").password("Sander123").roles("USER"); 
    } 

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

    @Bean 
    public TokenStore tokenStore() { 
     return new InMemoryTokenStore(); 
    } 

    @Bean 
    @Autowired 
    public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){ 
     TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler(); 
     handler.setTokenStore(tokenStore); 
     handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService)); 
     handler.setClientDetailsService(clientDetailsService); 
     return handler; 
    } 

    @Bean 
    @Autowired 
    public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception { 
     TokenApprovalStore store = new TokenApprovalStore(); 
     store.setTokenStore(tokenStore); 
     return store; 
    } 
} 

Stacktrace wenn ich versuche, mit einem Benutzer anmelden:

org.springframework.security.authentication.InsufficientAuthenticationException: User must be authenticated with Spring Security before authorization can be completed. 
     at org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(AuthorizationEndpoint.java:138) 

Ich bin ein ziemlich Anfänger, aber es scheint wie ein kleines Problem zu beheben. Kann mir jemand helfen?

+0

Also, was Sie wollen, ist ein Zugangstoken mit Postman bekommen? –

+0

Ja, das Problem ist, wenn ich mich mit dem Benutzer anmelde, gibt es mir nicht den Autorisierungscode, aber sagt mir, dass der Benutzer authentifiziert werden muss. Wenn ich versuche, mich vom Browser aus anzumelden, wird ein Fehler angezeigt. Das Authentifizierungsobjekt wurde im Sicherheitskontext nicht gefunden. –

Antwort

0

Sie müssen nur von WebSecurityConfig zu Ihrem configure Methode ändern, was folgt:

http 
    .authorizeRequests() 
     .antMatchers("/login", "/favicon.ico", 
      "/oauth/confirm_access", "/oauth/token", "/smarthouse", 
      "smarthouse2", "/user").permitAll() 
     .anyRequest().authenticated().and() 
    .csrf().disable(); 

Warum wollen Sie den anonymen Zugriff deaktivieren? Ein weiterer Punkt ist, dass die Reihenfolge der Matcher als wichtig erklärt wird.

Ich habe dein Repo geklont und es funktioniert für mich.

+0

Wenn ich nicht anonym deaktivieren, gibt es mir einen Fehler Zugriff verweigert, Benutzer ist anonym. Ich habe es versucht, aber es gibt mir immer noch den gleichen Fehler: Ein Authentifizierungsobjekt wurde im SecurityContext nicht gefunden. Es scheint eine Endlosschleife zu sein, in der der Benutzer authentifiziert wird, dann wird er auf die Autorisierungsseite umgeleitet, er kann jedoch keinen Benutzernamen und kein Kennwort abrufen und leitet dann zurück zur Anmeldeseite. –

+0

Nur die Lösung gefunden, hatte ich falsche Konfiguration in application.yml. Musste security.oauth2.resource.filer-order löschen: 3 und dann funktionierte es! –

Verwandte Themen