Skip to content

Commit 09df991

Browse files
author
Hattinger04
committed
completly reworked hashing algorithm + way easier to install application (database)
1 parent 7e2c7ed commit 09df991

File tree

12 files changed

+137
-26
lines changed

12 files changed

+137
-26
lines changed

README.MD

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
This is the API Background where our frontend framework (= VueJS) sends request to.
44

5-
## Installation guide for developer (not using docker)
5+
## Installation guide for developer (not using docker) to edit program
66

77
- git clone https://github.com/xJHamster/BackendSpring
88
- open maven spring project in your IDE
99
- create mysql database called "springserver"
1010
- run ProjectWebsiteApplication
11-
- go to application.properties and change spring.sql.init.mode=never to spring.sql.init.mode=always
12-
- restart program
13-
- then undo changes in application.properties and restart again
11+
12+
You can apply some changes in settings.properties, but be careful! You might need to reinstall your database!
1413

1514
## The commands
1615

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.github.Hattinger04.configuration;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
6+
import javax.persistence.EntityManager;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.boot.CommandLineRunner;
11+
import org.springframework.context.annotation.PropertySource;
12+
import org.springframework.stereotype.Component;
13+
14+
import io.github.Hattinger04.role.Role;
15+
import io.github.Hattinger04.role.RoleRepository;
16+
import io.github.Hattinger04.user.model.User;
17+
import io.github.Hattinger04.user.model.UserRepository;
18+
19+
@Component
20+
@PropertySource("classpath:settings.properties")
21+
public class AdminUserCreator implements CommandLineRunner {
22+
23+
private final UserRepository userRepository;
24+
private final RoleRepository roleRepository;
25+
26+
private final CustomPasswordEncoder passwordEncoder;
27+
28+
// TODO: creating database here not working yet!
29+
// but a beginning is there
30+
private final EntityManager entityManager;
31+
32+
@Value("${database}")
33+
private String database;
34+
35+
@Autowired
36+
public AdminUserCreator(UserRepository userRepository, CustomPasswordEncoder passwordEncoder,
37+
RoleRepository roleRepository, EntityManager entityManager) {
38+
this.userRepository = userRepository;
39+
this.passwordEncoder = passwordEncoder;
40+
this.roleRepository = roleRepository;
41+
this.entityManager = entityManager;
42+
}
43+
44+
@Override
45+
public void run(String... args) {
46+
try {
47+
entityManager.getTransaction().begin();
48+
entityManager.createNativeQuery(String.format("CREATE DATABASE %s", database)).executeUpdate();
49+
entityManager.getTransaction().commit();
50+
} catch (Exception e) {
51+
// database already exists
52+
}
53+
if (roleRepository.count() == 0) {
54+
roleRepository.save(new Role("ADMIN"));
55+
roleRepository.save(new Role("DEV"));
56+
roleRepository.save(new Role("TEACHER"));
57+
roleRepository.save(new Role("USER"));
58+
}
59+
60+
if (userRepository.count() == 0) {
61+
User user = new User();
62+
user.setUsername("admin");
63+
user.setActive(true);
64+
user.setPassword(passwordEncoder.encode("admin"));
65+
Role userRole = roleRepository.findByRole("ADMIN");
66+
user.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
67+
userRepository.save(user);
68+
}
69+
}
70+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.github.Hattinger04.configuration;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.PropertySource;
5+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
6+
import org.springframework.security.crypto.password.PasswordEncoder;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@PropertySource("classpath:settings.properties")
11+
public class CustomPasswordEncoder implements PasswordEncoder {
12+
13+
@Value("${password.pepper}")
14+
private String pepper;
15+
16+
@Value("${password.rounds}")
17+
private int rounds;
18+
19+
20+
public CustomPasswordEncoder() {}
21+
22+
@Override
23+
public String encode(CharSequence rawPassword) {
24+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(rounds);
25+
String saltedPassword = pepper + rawPassword + pepper;
26+
return encoder.encode(saltedPassword);
27+
}
28+
29+
@Override
30+
public boolean matches(CharSequence rawPassword, String encodedPassword) {
31+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
32+
String saltedPassword = pepper + rawPassword + pepper;
33+
return encoder.matches(saltedPassword, encodedPassword);
34+
}
35+
}

src/main/java/io/github/Hattinger04/configuration/SecurityConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
import org.springframework.context.annotation.Configuration;
55
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
66
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
7-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
7+
import org.springframework.security.crypto.password.PasswordEncoder;
88
import org.springframework.web.servlet.config.annotation.CorsRegistry;
99
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
1010
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
1111
import org.springframework.web.servlet.resource.PathResourceResolver;
1212

1313
@Configuration
1414
public class SecurityConfiguration implements WebMvcConfigurer {
15-
15+
1616
@Bean
17-
public BCryptPasswordEncoder passwordEncoder() {
18-
return new BCryptPasswordEncoder();
17+
public PasswordEncoder passwordEncoder() {
18+
return new CustomPasswordEncoder();
1919
}
20+
2021
@Override
2122
public void addResourceHandlers(ResourceHandlerRegistry registry) {
2223
registry.addResourceHandler("/files/**")

src/main/java/io/github/Hattinger04/configuration/WebSecurityConfig.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.springframework.security.config.annotation.web.builders.WebSecurity;
1212
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1313
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
14-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1514

1615
import io.github.Hattinger04.user.model.MyUserDetailsService;
1716

@@ -21,14 +20,14 @@
2120
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
2221

2322
@Autowired
24-
private BCryptPasswordEncoder bCryptPasswordEncoder;
23+
private CustomPasswordEncoder encoder;
2524

2625
@Autowired
2726
private MyUserDetailsService userDetailsService;
2827

2928
@Override
3029
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
31-
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
30+
auth.userDetailsService(userDetailsService).passwordEncoder(encoder);
3231
}
3332

3433
@Bean

src/main/java/io/github/Hattinger04/hamster/HamsterController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.fasterxml.jackson.databind.JsonNode;
1919
import com.fasterxml.jackson.databind.ObjectMapper;
2020

21-
import io.github.Hattinger04.course.model.student.Student;
2221
import io.github.Hattinger04.hamster.model.Hamster;
2322
import io.github.Hattinger04.hamsterEvaluation.workbench.Workbench;
2423

src/main/java/io/github/Hattinger04/role/Role.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ public class Role {
2020
private int id;
2121
@Column(name = "role")
2222
private String role;
23+
24+
public Role(String role) {
25+
this.role = role;
26+
}
2327
}

src/main/java/io/github/Hattinger04/user/model/UserService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import java.util.List;
66

77
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
98
import org.springframework.stereotype.Service;
109

10+
import io.github.Hattinger04.configuration.CustomPasswordEncoder;
1111
import io.github.Hattinger04.role.Role;
1212
import io.github.Hattinger04.role.RoleRepository;
1313

@@ -16,14 +16,14 @@ public class UserService {
1616

1717
private UserRepository userRepository;
1818
private RoleRepository roleRepository;
19-
private BCryptPasswordEncoder bCryptPasswordEncoder;
19+
private CustomPasswordEncoder customPasswordEncoder;
2020

2121
@Autowired
2222
public UserService(UserRepository userRepository, RoleRepository roleRepository,
23-
BCryptPasswordEncoder bCryptPasswordEncoder) {
23+
CustomPasswordEncoder customPasswordEncoder) {
2424
this.userRepository = userRepository;
2525
this.roleRepository = roleRepository;
26-
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
26+
this.customPasswordEncoder = customPasswordEncoder;
2727
}
2828

2929
public User findUserByID(int id) {
@@ -36,7 +36,7 @@ public User findUserByUsername(String username) {
3636

3737
public boolean saveUser(User user) {
3838
try {
39-
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
39+
user.setPassword(customPasswordEncoder.encode(user.getPassword()));
4040
user.setActive(true);
4141
Role userRole = roleRepository.findByRole("USER");
4242
user.setRoles(new HashSet<Role>(Arrays.asList(userRole)));

src/main/resources/allLogs.log

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,11 @@ Dez. 23, 2022 3:04:13 PM io.github.Hattinger04.aop.LogAspect loginLog
396396
FEIN: User(id=null, username=admin, password=admin, active=null, roles=null) - [logged in]
397397
Dez. 23, 2022 5:45:52 PM io.github.Hattinger04.aop.LogAspect loginLog
398398
FEIN: User(id=null, username=admin, password=admin, active=null, roles=null) - [logged in]
399+
Dez. 24, 2022 9:06:04 PM io.github.Hattinger04.aop.LogAspect loginLog
400+
FEIN: User(id=null, username=admin, password=admin, active=null, roles=null) - [logged in]
401+
Dez. 24, 2022 9:06:23 PM io.github.Hattinger04.aop.LogAspect loginLog
402+
FEIN: User(id=null, username=admin, password=admin, active=null, roles=null) - [logged in]
403+
Dez. 24, 2022 9:22:14 PM io.github.Hattinger04.aop.LogAspect loginLog
404+
FEIN: User(id=null, username=admin, password=admin, active=null, roles=null) - [logged in]
405+
Dez. 24, 2022 9:33:53 PM io.github.Hattinger04.aop.LogAspect loginLog
406+
FEIN: User(id=null, username=admin, password=admin, active=null, roles=null) - [logged in]

src/main/resources/application.properties

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ spring.datasource.password=
44
spring.jpa.show-sql = true
55
spring.jpa.hibernate.ddl-auto = update
66
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
7-
# set to always to generate data.sql in database - but you have to start webserver one time before to create tables
8-
spring.sql.init.mode=never
97

108
server.address=0.0.0.0
119

@@ -20,4 +18,5 @@ server.ssl.key-store-password=Winter21!
2018
#logging.level.org.springframework.security=DEBUG
2119

2220
# all reqests should begin with /api:
23-
spring.mvc.servlet.path=/api
21+
spring.mvc.servlet.path=/api
22+
bcrypt.strength=15

0 commit comments

Comments
 (0)