Created Service/Repository/Entity for Users and Cribs

- In preparation for the save/load crib on the Galaxy Server
- In preparation for the register user on the ArkOne Server
- Also added a PasswordEncoder for encrypting passwords when saved to
the DB
This commit is contained in:
2022-07-12 08:45:39 -05:00
committed by Julia Butenhoff
parent 58b2c87cd8
commit 7a3723fc98
14 changed files with 246 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Build (w/o Tests)" type="MavenRunConfiguration" factoryName="Maven">
<MavenSettings>
<option name="myGeneralSettings" />
<option name="myRunnerSettings" />
<option name="myRunnerParameters">
<MavenRunnerParameters>
<option name="profiles">
<set />
</option>
<option name="goals">
<list>
<option value="clean" />
<option value="install" />
<option value="-DskipTests" />
<option value="-U" />
</list>
</option>
<option name="pomFileName" />
<option name="profilesMap">
<map />
</option>
<option name="resolveToWorkspace" value="false" />
<option name="workingDirPath" value="$PROJECT_DIR$" />
</MavenRunnerParameters>
</option>
</MavenSettings>
<method v="2" />
</configuration>
</component>

16
pom.xml
View File

@@ -54,6 +54,22 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -18,6 +18,9 @@ public class ArkOneController implements TcpHandler {
String xmlData = new String(data);
System.out.println("[ArkOne] New Request: " + xmlData);
//TODO: USE THAT FOR PASSWORD ENCRYPTION FOR STORING IN THE DB
//TODO: SEE THIS ARTICLE FOR ENCODING: https://www.baeldung.com/spring-security-registration-password-encoding-bcrypt
try {
connection.send(xmlData.toUpperCase().getBytes());
} catch (IOException e) {

View File

@@ -77,6 +77,7 @@ public class GalaxyServer {
} catch (Exception e) {
// File doesn't exist error caught here
System.out.println(e.getMessage());
e.printStackTrace();
// Set the error code
errorCode = HttpServletResponse.SC_NOT_FOUND;
@@ -161,7 +162,7 @@ public class GalaxyServer {
// Send successful
response = "<postcard result=\"0\" reason=\"Postcard Sent!\" cost=\"5\" />";
} else {
response = "<postcard result=\"1\" reason=\"Unable to send postcard at this time!\" cost=\"0\" />";
response = "<postcard result=\"1\" reason=\"Unable to send postcard at this time!\" cost=\"5\" />";
}
return new ResponseEntity<>(response, HttpStatus.OK);
}

View File

@@ -2,7 +2,6 @@ package com.icedberries.UBFunkeysServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(scanBasePackages = {"com.icedberries", "javagrinko.spring"})
public class UBFunkeysServerApplication {

View File

@@ -0,0 +1,19 @@
package com.icedberries.UBFunkeysServer.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.security.SecureRandom;
@Configuration
public class Password {
private static final Integer STRENGTH = 15;
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder(STRENGTH, new SecureRandom());
}
}

View File

@@ -0,0 +1,33 @@
package com.icedberries.UBFunkeysServer.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Cribs")
public class Crib {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String cribName;
private String username;
private String profileData;
}

View File

@@ -0,0 +1,35 @@
package com.icedberries.UBFunkeysServer.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer UUID;
private String username;
private String password;
private String securityQuestion;
private String securityAnswer;
}

View File

@@ -0,0 +1,10 @@
package com.icedberries.UBFunkeysServer.repository;
import com.icedberries.UBFunkeysServer.domain.Crib;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CribRepository extends CrudRepository<Crib, Integer> {
}

View File

@@ -0,0 +1,18 @@
package com.icedberries.UBFunkeysServer.repository;
import com.icedberries.UBFunkeysServer.domain.User;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
Boolean existsByUUID(Integer uuid);
@Query("select user from User user where user.UUID = :uuid")
Optional<User> findByUUID(@Param("uuid") Integer uuid);
}

View File

@@ -0,0 +1,10 @@
package com.icedberries.UBFunkeysServer.service;
import com.icedberries.UBFunkeysServer.domain.Crib;
public interface CribService {
Integer count();
void save(Crib crib);
}

View File

@@ -0,0 +1,14 @@
package com.icedberries.UBFunkeysServer.service;
import com.icedberries.UBFunkeysServer.domain.User;
import java.util.Optional;
public interface UserService {
Optional<User> findByUUID(Integer uuid);
Boolean existsByUUID(Integer uuid);
void save(User user);
}

View File

@@ -0,0 +1,24 @@
package com.icedberries.UBFunkeysServer.service.impl;
import com.icedberries.UBFunkeysServer.domain.Crib;
import com.icedberries.UBFunkeysServer.repository.CribRepository;
import com.icedberries.UBFunkeysServer.service.CribService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class CribServiceImpl implements CribService {
public final CribRepository cribRepository;
@Override
public Integer count() {
return Math.toIntExact(cribRepository.count());
}
@Override
public void save(Crib crib) {
cribRepository.save(crib);
}
}

View File

@@ -0,0 +1,32 @@
package com.icedberries.UBFunkeysServer.service.impl;
import com.icedberries.UBFunkeysServer.domain.User;
import com.icedberries.UBFunkeysServer.repository.UserRepository;
import com.icedberries.UBFunkeysServer.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
public final UserRepository userRepository;
@Override
public Optional<User> findByUUID(Integer uuid) {
return userRepository.findByUUID(uuid);
}
@Override
public Boolean existsByUUID(Integer uuid) {
return userRepository.existsByUUID(uuid);
}
@Override
public void save(User user) {
userRepository.save(user);
}
}