Spring-Security-Oauth2: Full authentication is required to access this resource

前端 未结 7 1971
面向向阳花
面向向阳花 2020-12-02 18:20

I am trying to use spring-security-oauth2.0 with Java based configuration. My configuration is done, but when i deploy application on tomcat and hit the

相关标签:
7条回答
  • 2020-12-02 19:04

    The client_id and client_secret, by default, should go in the Authorization header, not the form-urlencoded body.

    1. Concatenate your client_id and client_secret, with a colon between them: abc@gmail.com:12345678.
    2. Base 64 encode the result: YWJjQGdtYWlsLmNvbToxMjM0NTY3OA==
    3. Set the Authorization header: Authorization: Basic YWJjQGdtYWlsLmNvbToxMjM0NTY3OA==
    0 讨论(0)
  • 2020-12-02 19:10

    I had the same problem, but I solve this with the following class:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.authentication.AuthenticationManager;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.crypto.password.NoOpPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    
    @Configuration
    @EnableWebSecurity
    public class OAuthSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Bean
        @Override
        public AuthenticationManager authenticationManager() throws Exception {
            return super.authenticationManager();
        }
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return NoOpPasswordEncoder.getInstance();
        }
    }
    
    0 讨论(0)
  • 2020-12-02 19:10

    setting management.security.enabled=false in application.properties resolved the issue for me.

    0 讨论(0)
  • 2020-12-02 19:12

    By default Spring OAuth requires basic HTTP authentication. If you want to switch it off with Java based configuration, you have to allow form authentication for clients like this:

    @Configuration
    @EnableAuthorizationServer
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
      @Override
      public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.allowFormAuthenticationForClients();
      }
    }
    
    0 讨论(0)
  • 2020-12-02 19:12

    The reason is that by default the /oauth/token endpoint is protected through Basic Access Authentication.

    All you need to do is add the Authorization header to your request.

    You can easily test it with a tool like curl by issuing the following command:

    curl.exe --user abc@gmail.com:12345678 http://localhost:8081/dummy-project-web/oauth/token?grant_type=client_credentials

    0 讨论(0)
  • 2020-12-02 19:13

    With Spring OAuth 2.0.7-RELEASE the following command works for me

    curl -v -u abc@gmail.com:12345678 -d "grant_type=client_credentials" http://localhost:9999/uaa/oauth/token
    

    It works with Chrome POSTMAN too, just make sure you client and secret in "Basic Auth" tab, set method to "POST" and add grant type in "form data" tab.

    0 讨论(0)
提交回复
热议问题