Skip to content

Commit 97ae32b

Browse files
committed
Initial Commit
1 parent b0696f2 commit 97ae32b

File tree

8 files changed

+172
-2
lines changed

8 files changed

+172
-2
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
<artifactId>mysql-connector-java</artifactId>
3434
</dependency>
3535

36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-security</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>io.jsonwebtoken</groupId>
43+
<artifactId>jjwt</artifactId>
44+
<version>0.9.1</version>
45+
</dependency>
3646

3747
<dependency>
3848
<groupId>org.springframework.boot</groupId>

src/main/java/com/webservice/mobile/app/MobileAppWebServicesApplication.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
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;
7+
58
@SpringBootApplication
69
public class MobileAppWebServicesApplication {
710

811
public static void main(String[] args) {
12+
913
SpringApplication.run(MobileAppWebServicesApplication.class, args);
1014
}
1115

16+
@Bean
17+
public BCryptPasswordEncoder bCryptPasswordEncoder(){
18+
return new BCryptPasswordEncoder();
19+
}
20+
1221
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.webservice.mobile.app.security;
2+
3+
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.webservice.mobile.app.ui.model.request.UserLoginRequestModel;
6+
import io.jsonwebtoken.Jwts;
7+
import io.jsonwebtoken.SignatureAlgorithm;
8+
import org.springframework.security.authentication.AuthenticationManager;
9+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
10+
import org.springframework.security.core.Authentication;
11+
import org.springframework.security.core.AuthenticationException;
12+
import org.springframework.security.core.userdetails.User;
13+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
14+
15+
import javax.servlet.FilterChain;
16+
import javax.servlet.ServletException;
17+
import javax.servlet.http.HttpServletRequest;
18+
import javax.servlet.http.HttpServletResponse;
19+
import java.io.IOException;
20+
import java.util.ArrayList;
21+
import java.util.Date;
22+
23+
public class AuthenticationFilter extends UsernamePasswordAuthenticationFilter {
24+
private final AuthenticationManager authenticationManager;
25+
26+
public AuthenticationFilter(AuthenticationManager authenticationManager) {
27+
this.authenticationManager = authenticationManager;
28+
}
29+
30+
@Override
31+
public Authentication attemptAuthentication(HttpServletRequest request,
32+
HttpServletResponse response)
33+
throws AuthenticationException {
34+
35+
try {
36+
UserLoginRequestModel creds = new ObjectMapper()
37+
.readValue(request.getInputStream(),UserLoginRequestModel.class);
38+
39+
return authenticationManager.authenticate(
40+
new UsernamePasswordAuthenticationToken(creds.getEmail()
41+
,creds.getPassword()
42+
,new ArrayList<>())
43+
);
44+
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
47+
}
48+
49+
}
50+
51+
52+
53+
@Override
54+
protected void successfulAuthentication(HttpServletRequest request,
55+
HttpServletResponse response,
56+
FilterChain chain,
57+
Authentication authResult)
58+
throws IOException, ServletException {
59+
String userName = ((User)authResult.getPrincipal()).getUsername();
60+
61+
String token = Jwts.builder()
62+
.setSubject(userName)
63+
.setExpiration(new Date(System.currentTimeMillis()+SecurityConstants.EXPIRATION_TIME))
64+
.signWith(SignatureAlgorithm.HS512,SecurityConstants.TOKEN_SECRET)
65+
.compact();
66+
67+
response.addHeader(SecurityConstants.HEADER_STRING,SecurityConstants.TOKEN_PREFIX+token);
68+
}
69+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.webservice.mobile.app.security;
2+
3+
public class SecurityConstants {
4+
public static final long EXPIRATION_TIME = 864000000; //Validity 10 Days
5+
public static final String TOKEN_PREFIX = "Bearer";
6+
public static final String HEADER_STRING = "Authorization";
7+
public static final String SIGN_UP_URL = "/users";
8+
public static final String TOKEN_SECRET = "jf9i4jgu83nfl0";
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.webservice.mobile.app.security;
2+
3+
import com.webservice.mobile.app.service.UserService;
4+
import org.springframework.http.HttpMethod;
5+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
10+
11+
@EnableWebSecurity
12+
public class WebSecurity extends WebSecurityConfigurerAdapter {
13+
private final UserService userService;
14+
private final BCryptPasswordEncoder bCryptPasswordEncoder;
15+
16+
public WebSecurity(UserService userService, BCryptPasswordEncoder bCryptPasswordEncoder) {
17+
this.userService = userService;
18+
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
19+
}
20+
21+
@Override
22+
protected void configure(HttpSecurity httpSecurity)throws Exception{
23+
httpSecurity.csrf().disable().authorizeRequests()
24+
.antMatchers(HttpMethod.POST,SecurityConstants.SIGN_UP_URL)
25+
.permitAll().anyRequest().authenticated().and()
26+
.addFilter(new AuthenticationFilter(authenticationManager()));
27+
}
28+
29+
@Override
30+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
31+
auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder);
32+
}
33+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.webservice.mobile.app.service;
22

33
import com.webservice.mobile.app.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 userDTO);
78
}

src/main/java/com/webservice/mobile/app/service/impl/UserServiceImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
import com.webservice.mobile.app.shared.dto.UserDTO;
88
import org.springframework.beans.BeanUtils;
99
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.security.core.userdetails.User;
11+
import org.springframework.security.core.userdetails.UserDetails;
12+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
13+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1014
import org.springframework.stereotype.Service;
1115
import sun.rmi.runtime.Log;
1216

17+
import java.util.ArrayList;
18+
1319
@Service
1420
public class UserServiceImpl implements UserService {
1521

@@ -19,6 +25,9 @@ public class UserServiceImpl implements UserService {
1925
@Autowired
2026
Utils utils;
2127

28+
@Autowired
29+
BCryptPasswordEncoder bCryptPasswordEncoder;
30+
2231
@Override
2332
public UserDTO createUser(UserDTO userDTO) {
2433

@@ -31,12 +40,20 @@ public UserDTO createUser(UserDTO userDTO) {
3140

3241
String autoGeneratedPublicUserID = utils.generateUserId(30);
3342
userEntity.setUserId(autoGeneratedPublicUserID);
34-
userEntity.setEncryptedPassword("test");
43+
userEntity.setEncryptedPassword(bCryptPasswordEncoder.encode(userDTO.getPassword()));
3544

3645
UserEntity storedUSerDeatils =userRepository.save(userEntity);
3746
UserDTO returnValue = new UserDTO();
3847
BeanUtils.copyProperties(storedUSerDeatils,returnValue);
3948

4049
return returnValue;
4150
}
51+
52+
@Override
53+
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
54+
UserEntity userEntity= userRepository.findUserByEmail(email);
55+
if (userEntity ==null)throw new UsernameNotFoundException(email);
56+
57+
return new User(userEntity.getEmail(),userEntity.getEncryptedPassword(),new ArrayList<>());
58+
}
4259
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.webservice.mobile.app.ui.model.request;
2+
3+
public class UserLoginRequestModel {
4+
private String email;
5+
private String password;
6+
7+
public String getEmail() {
8+
return email;
9+
}
10+
11+
public void setEmail(String email) {
12+
this.email = email;
13+
}
14+
15+
public String getPassword() {
16+
return password;
17+
}
18+
19+
public void setPassword(String password) {
20+
this.password = password;
21+
}
22+
}

0 commit comments

Comments
 (0)