1-
21package uk .co .aquaq .kdb .security ;
32import org .springframework .beans .factory .annotation .Autowired ;
43import org .springframework .beans .factory .annotation .Value ;
1211import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1312import org .springframework .web .cors .CorsConfiguration ;
1413import org .springframework .web .servlet .config .annotation .*;
14+ import org .w3c .dom .Document ;
15+ import org .w3c .dom .Element ;
16+ import org .w3c .dom .Node ;
17+ import org .w3c .dom .NodeList ;
18+ import org .xml .sax .SAXException ;
19+
20+ import javax .xml .parsers .DocumentBuilder ;
21+ import javax .xml .parsers .DocumentBuilderFactory ;
22+ import javax .xml .parsers .ParserConfigurationException ;
23+ import java .io .BufferedReader ;
24+ import java .io .File ;
25+ import java .io .FileReader ;
26+ import java .io .IOException ;
27+ import java .util .HashMap ;
28+ import java .util .Map ;
1529
1630
1731@ Configuration
@@ -26,12 +40,47 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
2640 @ Value ("${basic.authentication.password}" )
2741 String password ;
2842
43+ @ Value ("${authentication.path}" )
44+ String passwordFile ;
45+
46+
2947 @ Autowired
3048 public void configureGlobal (AuthenticationManagerBuilder auth ) throws Exception {
49+ if (passwordFile !=null && !passwordFile .isEmpty ()) {
50+ NodeList nList = getNodeList ();
51+ addAuthenticationsFromXml (auth , nList );
52+ }
3153 auth .inMemoryAuthentication ()
3254 .withUser (user ).password (password ).authorities ("ROLE_USER" );
3355 }
3456
57+ private void addAuthenticationsFromXml (AuthenticationManagerBuilder auth , NodeList nList ) throws Exception {
58+ for (int temp = 0 ; temp < nList .getLength (); temp ++) {
59+ Node nNode = nList .item (temp );
60+ if (nNode .getNodeType () == Node .ELEMENT_NODE ) {
61+ Element eElement = (Element ) nNode ;
62+ String username =eElement .getElementsByTagName ("username" ).item (0 )
63+ .getTextContent ();
64+ String password =eElement .getElementsByTagName ("password" ).item (0 )
65+ .getTextContent ();
66+
67+ auth .inMemoryAuthentication ()
68+ .withUser (username ).password (password ).authorities ("ROLE_USER" );
69+ }
70+ }
71+ }
72+
73+ private NodeList getNodeList () throws ParserConfigurationException , SAXException , IOException {
74+ File file = new File (passwordFile );
75+ Map <String , String > authMap =new HashMap <>();
76+ DocumentBuilder dBuilder = DocumentBuilderFactory .newInstance ()
77+ .newDocumentBuilder ();
78+ Document doc = dBuilder .parse (file );
79+ doc .getDocumentElement ().normalize ();
80+
81+ return doc .getElementsByTagName ("user" );
82+ }
83+
3584 protected void configure (HttpSecurity http ) throws Exception {
3685 http .csrf ().disable ()
3786 .authorizeRequests ()
0 commit comments