-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
376 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,37 @@ | ||
# !/bin/bash | ||
#!/bin/bash | ||
|
||
# This script will build the app and run it in a docker container | ||
|
||
# Function to perform housekeeping tasks | ||
cleanup() { | ||
echo "Performing cleanup..." | ||
# Stop and remove the app container if it exists | ||
docker stop app-test | ||
docker rm app-test | ||
# Stop and remove the postgresql container if it exists | ||
docker stop postgres_container | ||
docker rm postgres_container | ||
# Remove image if exists | ||
docker rmi myapp-img:v1 | ||
# Remove volume if exists | ||
docker volume rm postgresql-data | ||
} | ||
|
||
# Trap the EXIT signal to perform cleanup | ||
trap cleanup EXIT | ||
|
||
# Getting local ip address from ifconfig | ||
LOCAL_IP=$(ifconfig | grep 'inet ' | grep -Fv 127.0.0.1 | awk '{print $2}') | ||
# To run a local containerized postgresql db, run the following command: | ||
# docker run -d -p 5432:5432 --name postgres_container -e POSTGRES_PASSWORD=Password123 -v postgresql-data:/var/lib/postgresql/data postgres | ||
# Stop and remove the app container if it exists | ||
docker stop app-test | ||
docker rm app-test | ||
# Remove image if exists | ||
docker rmi myapp-img:v1 | ||
# Run a postgresql container with a volume to persist data | ||
docker run -d -p 5432:5432 --name postgres_container -e POSTGRES_PASSWORD=Password123 -v postgresql-data:/var/lib/postgresql/data postgres | ||
# Run any previous migrations and tag the latest version with Liquibase | ||
mvn liquibase:rollback -Dliquibase.rollbackCount=999 -Dliquibase.url=jdbc:postgresql://${LOCAL_IP}:5432/postgres -Dliquibase.username=postgres -Dliquibase.password=Password123 -Dliquibase.changeLogFile=db/changelog/changelog_version-3.3.xml | ||
# jdbc:postgresql://hostname:5432/MY_TEST_DATABASE | ||
mvn liquibase:tag -Dliquibase.tag=v3.2 -Dliquibase.url=jdbc:postgresql://${LOCAL_IP}:5432/postgres -Dliquibase.username=postgres -Dliquibase.password=Password123 -Dliquibase.changeLogFile=db/changelog/changelog_version-3.3.xml | ||
# Build the app and create a docker image from local dockerfile | ||
mvn clean package | ||
docker build -t myapp-img:v1 . | ||
docker run -e USER_NAME=postgres -e PASSWORD=Password123 -e CHANGELOG_VERSION=changelog_version-3.3.xml -e DB_URL=jdbc:postgresql://${LOCAL_IP}:5432/postgres -t -d -p 80:8086 --name app-test myapp-img:v1 | ||
docker run -d -e USER_NAME=postgres -e PASSWORD=Password123 -e CHANGELOG_VERSION=changelog_version-3.3.xml -e DB_URL=jdbc:postgresql://${LOCAL_IP}:5432/postgres -t -p 80:8086 --name app-test myapp-img:v1 | ||
docker ps -a | ||
echo "Waiting for 10 seconds for the app to start" | ||
sleep 10 | ||
# Print the logs of the app from the container | ||
docker logs app-test | ||
docker logs -f app-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package net.codejava; | ||
|
||
public @interface EnableWebSecurity { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package net.codejava; | ||
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; | ||
|
||
|
||
@Configuration | ||
public class PasswordEncoderConfig { | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package net.codejava; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private UserDetailsService userDetailsService; | ||
|
||
@Autowired | ||
private PasswordEncoder passwordEncoder; | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManagerBean() throws Exception { | ||
return super.authenticationManagerBean(); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http | ||
.authorizeRequests() | ||
.antMatchers("/login").permitAll() | ||
// .anyRequest().permitAll() | ||
.anyRequest().authenticated() | ||
.and() | ||
.formLogin() | ||
// .loginPage("/login") | ||
// .loginProcessingUrl("/login") // This should match the form action in your login.html file | ||
.usernameParameter("username") | ||
.passwordParameter("password") | ||
.defaultSuccessUrl("/", true) // This is the URL to redirect to after a successful login | ||
.successForwardUrl("/") | ||
.failureUrl("/login?error=true") | ||
.permitAll() | ||
.and() | ||
.logout() | ||
.logoutUrl("/logout") // This is the URL to send the user to once they have logged out | ||
.invalidateHttpSession(true) | ||
.permitAll(); | ||
} | ||
|
||
@Autowired | ||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { | ||
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.codejava; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "\"user\"") | ||
public class User { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String username; | ||
private String password; | ||
|
||
// getters and setters methods | ||
// getter for id | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
// setter for id | ||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
// getter for username | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
// setter for username | ||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
// getter for password | ||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
// setter for password{ | ||
public void setPassword(String password){ | ||
this.password = password; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package net.codejava; | ||
|
||
import org.hibernate.exception.SQLGrammarException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import java.util.ArrayList; | ||
|
||
@Service | ||
public class UserDetailsServiceImpl implements UserDetailsService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class); | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private PasswordEncoder passwordEncoder; | ||
|
||
public void createUser(User user) { | ||
user.setPassword(passwordEncoder.encode(user.getPassword())); | ||
userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) { | ||
try { | ||
User user = userRepository.findByUsername(username); | ||
if (user == null) { | ||
throw new UsernameNotFoundException("User not found with username: " + username); | ||
} | ||
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), new ArrayList<>()); | ||
} catch (SQLGrammarException e) { | ||
logger.error("SQL error when trying to find user by username: " + username, e); | ||
throw new RuntimeException("An error occurred while trying to find user by username: " + username); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package net.codejava; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
User findByUsername(String username); | ||
} |
Oops, something went wrong.