Skip to content

Commit 51aa7b6

Browse files
Multiple users (#14)
* Commmitting changes for multiple users to log in with basic authentication * committing default props for users
1 parent 6cddeae commit 51aa7b6

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/main/java/uk/co/aquaq/kdb/security/SecurityConfiguration.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package uk.co.aquaq.kdb.security;
32
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.beans.factory.annotation.Value;
@@ -12,6 +11,21 @@
1211
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1312
import org.springframework.web.cors.CorsConfiguration;
1413
import 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()

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ basic.authentication.user=user
2222
basic.authentication.password=pass
2323

2424
springfox.documentation.swagger.v2.path=/kdb-rest-service-documentation
25-
25+
authentication.path=\\Path\to\user\file\\users.xml
2626

0 commit comments

Comments
 (0)