From 423e17ecb8d1cd5a54899f4185702502daff3a5e Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Tue, 25 Feb 2020 15:03:41 +0100 Subject: [PATCH] small changes --- .idea/misc.xml | 2 +- build.gradle | 4 +- .../smarthut/SmarthutApplication.java | 2 + .../smarthut/models/SecurityService.java | 42 +++++++++++++++++++ .../sa4/sanmarinoes/smarthut/models/User.java | 25 +++++++---- .../models/UserDetailsServiceImpl.java | 25 +++++++++++ .../smarthut/models/UserRepository.java | 4 +- .../smarthut/models/UserService.java | 22 ++++++++++ 8 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityService.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserDetailsServiceImpl.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserService.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 56d4a95..8f27022 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/build.gradle b/build.gradle index c65502c..e6c5a36 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { group = 'ch.usi.inf.sa4.sanmarinoes.' version = '0.0.1-SNAPSHOT' -sourceCompatibility = '11' +sourceCompatibility = "11" repositories { mavenCentral() @@ -16,7 +16,9 @@ dependencies { compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final' implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.security:spring-security-web' implementation 'org.postgresql:postgresql' + implementation('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json' } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java index f215fe7..242f03f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java @@ -2,8 +2,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication +@EnableJpaRepositories("ch.usi.inf.sa4.sanmarinoes.smarthut.models") public class SmarthutApplication { public static void main(String[] args) { SpringApplication.run(SmarthutApplication.class, args); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityService.java new file mode 100644 index 0000000..5ff2e3b --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityService.java @@ -0,0 +1,42 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; + +public class SecurityService { + @Autowired private AuthenticationManager manager; + + @Autowired private UserDetailsService service; + + private Logger logger = LoggerFactory.getLogger(SecurityService.class); + + public String loggedUser() { + Object details = SecurityContextHolder.getContext().getAuthentication().getDetails(); + if (details instanceof UserDetails) { + return ((UserDetails) details).getUsername(); + } else { + return null; + } + } + + public void autoLogin(String username, String password) { + UserDetails userDetails = service.loadUserByUsername(username); + UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = + new UsernamePasswordAuthenticationToken( + userDetails, password, userDetails.getAuthorities()); + + manager.authenticate(usernamePasswordAuthenticationToken); + + if (usernamePasswordAuthenticationToken.isAuthenticated()) { + SecurityContextHolder.getContext() + .setAuthentication(usernamePasswordAuthenticationToken); + logger.debug(String.format("Auto login %s successfully!", username)); + } + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java index 92202e2..7378306 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java @@ -15,8 +15,11 @@ public class User { /** The full name of the user */ @Column private String name; + /** The full name of the user */ + @Column private String username; + /** A properly salted way to store the password TODO: define the implementation of salt */ - @Column private String hashedPassword; + @Column private String password; /** The user's email TODO: validate email in setters */ @Column private String email; @@ -37,6 +40,14 @@ public class User { return name; } + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + public void setName(String name) { this.name = name; } @@ -49,12 +60,12 @@ public class User { this.email = email; } - public String getHashedPassword() { - return hashedPassword; + public String getPassword() { + return password; } - public void setHashedPassword(String hashedPassword) { - this.hashedPassword = hashedPassword; + public void setPassword(String password) { + this.password = password; } public Set getRooms() { @@ -69,8 +80,8 @@ public class User { + ", name='" + name + '\'' - + ", hashedPassword='" - + hashedPassword + + ", password='" + + password + '\'' + ", email='" + email diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserDetailsServiceImpl.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserDetailsServiceImpl.java new file mode 100644 index 0000000..c1d9ff1 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserDetailsServiceImpl.java @@ -0,0 +1,25 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.Set; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.*; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +public class UserDetailsServiceImpl implements UserDetailsService { + @Autowired private UserRepository repository; + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + User toReturn = repository.findByUsername(username); + if (toReturn != null) { + Set authoritySet = Set.of(new SimpleGrantedAuthority("user")); + return new org.springframework.security.core.userdetails.User( + toReturn.getUsername(), toReturn.getPassword(), authoritySet); + } else { + throw new UsernameNotFoundException(username); + } + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java index a0a0f1f..c2bf0c2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java @@ -2,4 +2,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import org.springframework.data.repository.CrudRepository; -public interface UserRepository extends CrudRepository {} +public interface UserRepository extends CrudRepository { + User findByUsername(String username); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserService.java new file mode 100644 index 0000000..3ae7b93 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserService.java @@ -0,0 +1,22 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + @Autowired private UserRepository userRepository; + + @Autowired private BCryptPasswordEncoder bCryptPasswordEncoder; + + public void save(User user) { + user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); + userRepository.save(user); + } + + public User findByUsername(String username) { + return userRepository.findByUsername(username); + } +}