Skip to content

Commit 0835302

Browse files
committed
add WebSecurity
1 parent 9e6709d commit 0835302

File tree

10 files changed

+131
-19
lines changed

10 files changed

+131
-19
lines changed

pom.xml

+24-8
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,39 @@
4444
<dependency>
4545
<groupId>mysql</groupId>
4646
<artifactId>mysql-connector-java</artifactId>
47-
47+
<version>8.0.33</version>
4848
</dependency>
49-
50-
49+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
5150
<dependency>
5251
<groupId>org.springframework.boot</groupId>
53-
<artifactId>spring-boot-starter-test</artifactId>
54-
<scope>test</scope>
52+
<artifactId>spring-boot-starter-security</artifactId>
5553
</dependency>
5654
<dependency>
57-
<groupId>org.springframework.boot</groupId>
58-
<artifactId>spring-boot-starter-data-jpa</artifactId>
55+
<groupId>io.jsonwebtoken</groupId>
56+
<artifactId>jjwt-api</artifactId>
57+
<version>0.11.5</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.jsonwebtoken</groupId>
61+
<artifactId>jjwt-impl</artifactId>
62+
<version>0.11.5</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>io.jsonwebtoken</groupId>
66+
<artifactId>jjwt-jackson</artifactId>
67+
<version>0.11.5</version>
5968
</dependency>
69+
70+
71+
72+
6073
<dependency>
6174
<groupId>org.springframework.boot</groupId>
62-
<artifactId>spring-boot-starter-data-jpa</artifactId>
75+
<artifactId>spring-boot-starter-test</artifactId>
76+
<scope>test</scope>
6377
</dependency>
78+
79+
6480
</dependencies>
6581

6682
<build>

src/main/java/com/appsdeveloperblog/app/ws/mobile_app_ws/MobileAppWsApplication.java

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
57

68
@SpringBootApplication
79
public class MobileAppWsApplication {
@@ -10,4 +12,8 @@ public static void main(String[] args) {
1012
SpringApplication.run(MobileAppWsApplication.class, args);
1113
}
1214

15+
@Bean
16+
public BCryptPasswordEncoder bCryptPasswordEncoder(){
17+
return new BCryptPasswordEncoder();
18+
}
1319
}

src/main/java/com/appsdeveloperblog/app/ws/mobile_app_ws/UserRepository.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
@Repository
88
public interface UserRepository extends CrudRepository<UserEntity,Long> {
9+
UserEntity findByEmail(String email);
910

1011
}

src/main/java/com/appsdeveloperblog/app/ws/mobile_app_ws/io/entity/UserEntity.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class UserEntity implements Serializable {
3030

3131
private String emailVerificationToken;
3232

33-
@Column(nullable = false,columnDefinition = "boolean default false")
34-
private Boolean emailVerificationStatus;
33+
@Column(nullable = false)
34+
private Boolean emailVerificationStatus=false;
3535

3636
public long getId() {
3737
return id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.appsdeveloperblog.app.ws.mobile_app_ws.security;
2+
3+
import com.appsdeveloperblog.app.ws.mobile_app_ws.service.UserService;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.http.HttpMethod;
6+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
10+
import org.springframework.security.web.SecurityFilterChain;
11+
12+
@EnableWebSecurity
13+
public class WebSecurity{
14+
15+
private final UserService userDetailsService;
16+
private final BCryptPasswordEncoder bCryptPasswordEncoder;
17+
18+
public WebSecurity(UserService userService, BCryptPasswordEncoder bCryptPasswordEncoder) {
19+
this.userDetailsService = userService;
20+
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
21+
}
22+
23+
@Bean
24+
protected SecurityFilterChain configure(HttpSecurity http) throws Exception{
25+
26+
//Configure AuthenticationManagerBuilder
27+
AuthenticationManagerBuilder authenticationManagerBuilder=
28+
http.getSharedObject(AuthenticationManagerBuilder.class);
29+
30+
authenticationManagerBuilder.userDetailsService(userDetailsService)
31+
.passwordEncoder(bCryptPasswordEncoder);
32+
33+
34+
35+
http.csrf().disable()
36+
.authorizeRequests().antMatchers(HttpMethod.POST,"/users").permitAll()
37+
.anyRequest().authenticated();
38+
return http.build();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.appsdeveloperblog.app.ws.mobile_app_ws.service;
22

33
import com.appsdeveloperblog.app.ws.mobile_app_ws.shared.dto.UserDto;
4+
import org.springframework.security.core.userdetails.UserDetailsService;
45

5-
public interface UserService {
6+
public interface UserService extends UserDetailsService {
67
UserDto createUser(UserDto user);
78
}

src/main/java/com/appsdeveloperblog/app/ws/mobile_app_ws/service/impl/UserServiceImpl.java

+27-7
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,50 @@
33
import com.appsdeveloperblog.app.ws.mobile_app_ws.UserRepository;
44
import com.appsdeveloperblog.app.ws.mobile_app_ws.io.entity.UserEntity;
55
import com.appsdeveloperblog.app.ws.mobile_app_ws.service.UserService;
6+
import com.appsdeveloperblog.app.ws.mobile_app_ws.shared.Utils;
67
import com.appsdeveloperblog.app.ws.mobile_app_ws.shared.dto.UserDto;
78
import org.springframework.beans.BeanUtils;
89
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.security.core.userdetails.UserDetails;
11+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
12+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
913
import org.springframework.stereotype.Service;
1014

1115
@Service
1216
public class UserServiceImpl implements UserService {
1317

1418
@Autowired
1519
UserRepository userRepository;
20+
21+
@Autowired
22+
Utils utils;
23+
24+
@Autowired
25+
BCryptPasswordEncoder bCryptPasswordEncoder;
26+
1627
@Override
1728
public UserDto createUser(UserDto user) {
1829

19-
UserEntity userEntity=new UserEntity();
20-
BeanUtils.copyProperties(user,userEntity);
30+
if (userRepository.findByEmail(user.getEmail()) != null) throw new RuntimeException("Record already exists");
2131

22-
userEntity.setEncryptedPassword("test");
23-
userEntity.setUserId("testUserId");
32+
UserEntity userEntity = new UserEntity();
33+
BeanUtils.copyProperties(user, userEntity);
2434

25-
UserEntity storedUserDetails=userRepository.save(userEntity);
35+
String publicUserId=utils.generateUserId(30);
2636

27-
UserDto returnValue=new UserDto();
28-
BeanUtils.copyProperties(storedUserDetails,returnValue);
37+
userEntity.setEncryptedPassword(bCryptPasswordEncoder.encode(user.getPassword()));
38+
userEntity.setUserId(publicUserId);
39+
40+
UserEntity storedUserDetails = userRepository.save(userEntity);
41+
42+
UserDto returnValue = new UserDto();
43+
BeanUtils.copyProperties(storedUserDetails, returnValue);
2944

3045
return returnValue;
3146
}
47+
48+
@Override
49+
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
50+
return null;
51+
}
3252
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.appsdeveloperblog.app.ws.mobile_app_ws.shared;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import java.security.SecureRandom;
6+
import java.util.Random;
7+
8+
@Component
9+
public class Utils {
10+
private final Random RANDOM = new SecureRandom();
11+
private String ALPHABET = "012345789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
12+
private int ITERATIONS = 1000;
13+
private final int KEY_LENGTH = 256;
14+
15+
public String generateUserId(int length) {
16+
return generateRandomString(length);
17+
}
18+
19+
private String generateRandomString(int length) {
20+
StringBuilder returnValue = new StringBuilder(length);
21+
for (int i = 0; i < length; i++) {
22+
returnValue.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length())));
23+
}
24+
return new String(returnValue);
25+
}
26+
}

src/main/java/com/appsdeveloperblog/app/ws/mobile_app_ws/shared/dto/UserDto.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class UserDto implements Serializable {
1212
private String password;
1313
private String encryptedPassword;
1414
private String emailVerificationToken;
15-
private Boolean emailVerificationStatus;
15+
private Boolean emailVerificationStatus=false;
1616

1717
public long getId() {
1818
return id;

src/main/resources/application.properties

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ spring.datasource.username=root
33
spring.datasource.password=root
44
spring.datasource.url=jdbc:mysql://localhost:3306/photo_app
55
spring.jpa.hibernate.ddl-auto=update
6+
spring.jpa.open-in-view=false
7+

0 commit comments

Comments
 (0)