View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
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    *   http://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   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.impl;
20  
21  import java.util.Collection;
22  
23  import javax.annotation.PostConstruct;
24  
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
27  import org.springframework.security.core.Authentication;
28  import org.springframework.security.core.GrantedAuthority;
29  import org.springframework.security.core.authority.AuthorityUtils;
30  import org.springframework.security.core.context.SecurityContextHolder;
31  import org.springframework.stereotype.Component;
32  
33  import com.hack23.cia.service.api.ConfigurationManager;
34  
35  /**
36   * The Class BootstrapDefaultConfig.
37   */
38  @Component
39  public final class BootstrapDefaultConfig {
40  
41  	/** The configuration manager. */
42  	@Autowired
43  	private ConfigurationManager configurationManager;
44  
45  	/**
46  	 * Instantiates a new bootstrap default config.
47  	 */
48  	public BootstrapDefaultConfig() {
49  		super();
50  	}
51  
52  	/**
53  	 * Creates the default configuration.
54  	 */
55  	@PostConstruct
56  	public void createDefaultConfiguration() {
57  		configureAuthentication("ROLE_ADMIN");
58  		configurationManager.createDefaultConfigIfEmpty();
59  		configurationManager.createDefaultLanguagesIfEmpty();
60  		clearAuthentication();
61  	}
62  
63  	/**
64  	 * Clear authentication.
65  	 */
66  	private static void clearAuthentication() {
67  		SecurityContextHolder.getContext().setAuthentication(null);
68  	}
69  
70  	/**
71  	 * Configure authentication.
72  	 *
73  	 * @param role
74  	 *            the role
75  	 */
76  	private static void configureAuthentication(final String role) {
77  		final Collection<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(role);
78  		final Authentication authentication = new UsernamePasswordAuthenticationToken("user", role, authorities);
79  		SecurityContextHolder.getContext().setAuthentication(authentication);
80  	}
81  
82  }