Projekt

Obecné

Profil

« Předchozí | Další » 

Revize ccdbd512

Přidáno uživatelem stepanekp před asi 3 roky(ů)

Moving user credentials to separate file and creating service

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/AppController.java
7 7
import cz.zcu.fav.kiv.antipatterndetectionapp.model.QueryResult;
8 8
import cz.zcu.fav.kiv.antipatterndetectionapp.service.AntiPatternService;
9 9
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
10
import cz.zcu.fav.kiv.antipatterndetectionapp.service.UserAccountService;
10 11
import org.slf4j.Logger;
11 12
import org.slf4j.LoggerFactory;
12 13
import org.springframework.beans.factory.annotation.Autowired;
......
46 47
    @Autowired
47 48
    private AntiPatternManager antiPatternManager;
48 49

  
50
    @Autowired
51
    private UserAccountService userAccountService;
52

  
49 53
    /**
50 54
     *  This method is called by the GET method and initializes
51 55
     *  the main page of the application (index). Loads all projects
......
352 356
                               @RequestParam(value = "passInput", required = false) String passInput,
353 357
                               RedirectAttributes redirectAttrs) {
354 358

  
355
        if(nameInput.equals("admin") && passInput.equals("test")){
356
            session.setAttribute("user", "admin");
359
        if(userAccountService.checkCredentials(nameInput, passInput)){
360
            session.setAttribute("user", nameInput);
357 361
            return "redirect:/";
358 362
        }
359 363

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/UserAccountService.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.service;
2

  
3
public interface UserAccountService {
4

  
5
    boolean checkCredentials(String username, String password);
6

  
7
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/UserAccountServiceImpl.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.service;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.controller.AppController;
4
import org.slf4j.Logger;
5
import org.slf4j.LoggerFactory;
6
import org.springframework.stereotype.Service;
7

  
8
import java.io.InputStream;
9
import java.util.Properties;
10

  
11
@Service
12
public class UserAccountServiceImpl implements UserAccountService{
13

  
14
    private final String accountDetailsFile = "account.properties";
15

  
16
    private final Logger LOGGER = LoggerFactory.getLogger(AppController.class);
17

  
18
    @Override
19
    public boolean checkCredentials(String username, String password) {
20
        try (InputStream input = this.getClass().getClassLoader().getResourceAsStream(accountDetailsFile)) {
21

  
22
            Properties prop = new Properties();
23

  
24
            if (input == null) {
25
                LOGGER.error("Unable to open file: " + accountDetailsFile);
26
                return false;
27
            }
28

  
29
            // load a properties file
30
            prop.load(input);
31

  
32
            //get the property value and print it out
33
            String accountUsername = prop.getProperty("account.user.name");
34
            String accountPassword = prop.getProperty("account.user.password");
35

  
36
            if(username.equals(accountUsername) && password.equals(accountPassword))
37
                return true;
38

  
39
        } catch (Exception ex) {
40
            ex.printStackTrace();
41
        }
42

  
43
        return false;
44
    }
45
}
src/main/resources/account.properties
1
account.user.name=
2
account.user.password=

Také k dispozici: Unified diff