small changes

This commit is contained in:
Jacob Salvi 2020-02-25 15:03:41 +01:00
parent d9348bb6da
commit 423e17ecb8
8 changed files with 116 additions and 10 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="12" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View file

@ -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'
}

View file

@ -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);

View file

@ -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));
}
}
}

View file

@ -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<Room> getRooms() {
@ -69,8 +80,8 @@ public class User {
+ ", name='"
+ name
+ '\''
+ ", hashedPassword='"
+ hashedPassword
+ ", password='"
+ password
+ '\''
+ ", email='"
+ email

View file

@ -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<GrantedAuthority> authoritySet = Set.of(new SimpleGrantedAuthority("user"));
return new org.springframework.security.core.userdetails.User(
toReturn.getUsername(), toReturn.getPassword(), authoritySet);
} else {
throw new UsernameNotFoundException(username);
}
}
}

View file

@ -2,4 +2,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> {}
public interface UserRepository extends CrudRepository<User, Long> {
User findByUsername(String username);
}

View file

@ -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);
}
}