1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38 @Component
39 public final class BootstrapDefaultConfig {
40
41
42 @Autowired
43 private ConfigurationManager configurationManager;
44
45
46
47
48 public BootstrapDefaultConfig() {
49 super();
50 }
51
52
53
54
55 @PostConstruct
56 public void createDefaultConfiguration() {
57 configureAuthentication("ROLE_ADMIN");
58 configurationManager.createDefaultConfigIfEmpty();
59 configurationManager.createDefaultLanguagesIfEmpty();
60 clearAuthentication();
61 }
62
63
64
65
66 private static void clearAuthentication() {
67 SecurityContextHolder.getContext().setAuthentication(null);
68 }
69
70
71
72
73
74
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 }