Adding more then one client to the Spring OAuth2 Auth Server

前端 未结 2 1497
不知归路
不知归路 2021-02-04 03:33

I have Spring OAuth Authorization server and I want to add support for more then one client(id). I configured clients like this:

clients
            .inMemory().         


        
2条回答
  •  终归单人心
    2021-02-04 03:59

    For inMemorybuilder with configuration (you will have to define your own configuration):

     @Override
        public void configure ( ClientDetailsServiceConfigurer clients ) throws Exception {
            // @formatter:off
            InMemoryClientDetailsServiceBuilder inMemoryBuilder = clients.inMemory ();
            for (String clientKey: authServerProperties.getClient ().keySet ()) {
                OAuthClientProperties client = authServerProperties.getClient ().get ( clientKey );
                inMemoryBuilder
                    .withClient ( client.getClientId () )
                    .secret ( client.getClientSecret () )
                    .scopes ( client.getScopes () == null ? new String[] {"openid"} : client.getScopes () )
                    .authorizedGrantTypes ( client.getAuthorizedGrandTypes () == null ? "client_credentials" : client.getAuthorizedGrandTypes () );
            }
    
            // @formatter:on
        }
    

    with two additional classes:

    @ConfigurationProperties ( prefix = "my-authorization-server" )
    public class AuthServerProperties 
    
        private final Map client = new HashMap<> ();
    
        ...
    
        public Map getClient () {
            return client;
        }
    
        ...
    
    }
    
    
    public class OAuthClientProperties {
    
        private String clientId;
    
        private String clientSecret;
    
        private String[] scopes;
    
        private String authorizedGrandTypes;
    
        public String getClientId () {
            return clientId;
        }
    
        public void setClientId ( String clientId ) {
            this.clientId = clientId;
        }
    
        public String getClientSecret () {
            return clientSecret;
        }
    
        public void setClientSecret ( String clientSecret ) {
            this.clientSecret = clientSecret;
        }
    
        public String[] getScopes () {
            return scopes;
        }
    
        public void setScopes ( String[]  scopes ) {
            this.scopes = scopes;
        }
    
        public String getAuthorizedGrandTypes () {
            return authorizedGrandTypes;
        }
    
        public void setAuthorizedGrandTypes ( String authorizedGrandTypes ) {
            this.authorizedGrandTypes = authorizedGrandTypes;
        }
    
    }
    

    and finally, in properties you would have something like this:

    my-authorization-server.client.foo.client-id=foo-client
    my-authorization-server.client.foo.client-secret=foo-client-supersecret
    my-authorization-server.client.foo.scopes=read
    
    my-authorization-server.client.bar.client-id=bar-client
    my-authorization-server.client.bar.client-secret=bar-client-verysupersecret
    my-authorization-server.client.bar.scopes=read,write
    

提交回复
热议问题