View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.security.oauth2.config.annotation.builders;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import javax.sql.DataSource;
22  
23  import org.springframework.security.crypto.password.PasswordEncoder;
24  import org.springframework.security.oauth2.provider.ClientDetails;
25  import org.springframework.security.oauth2.provider.ClientDetailsService;
26  import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
27  import org.springframework.util.Assert;
28  
29  /**
30   * @author Dave Syer
31   * 
32   */
33  public class JdbcClientDetailsServiceBuilder extends ClientDetailsServiceBuilder<JdbcClientDetailsServiceBuilder> {
34  
35  	private Set<ClientDetails> clientDetails = new HashSet<ClientDetails>();
36  
37  	private DataSource dataSource;
38  
39  	private PasswordEncoder passwordEncoder; // for writing client secrets
40  
41  	public JdbcClientDetailsServiceBuilder dataSource(DataSource dataSource) {
42  		this.dataSource = dataSource;
43  		return this;
44  	}
45  
46  	public JdbcClientDetailsServiceBuilder passwordEncoder(PasswordEncoder passwordEncoder) {
47  		this.passwordEncoder = passwordEncoder;
48  		return this;
49  	}
50  
51  	@Override
52  	protected void addClient(String clientId, ClientDetails value) {
53  		clientDetails.add(value);
54  	}
55  
56  	@Override
57  	protected ClientDetailsService performBuild() {
58  		Assert.state(dataSource != null, "You need to provide a DataSource");
59  		JdbcClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);
60  		if (passwordEncoder != null) {
61  			// This is used to encode secrets as they are added to the database (if it isn't set then the user has top
62  			// pass in pre-encoded secrets)
63  			clientDetailsService.setPasswordEncoder(passwordEncoder);
64  		}
65  		for (ClientDetails client : clientDetails) {
66  			clientDetailsService.addClientDetails(client);
67  		}
68  		return clientDetailsService;
69  	}
70  
71  }