Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,38 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

151 changes: 151 additions & 0 deletions spring-boot-3/spring-security/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<groupId>io.thoughtcode.spring-boot3</groupId>
<artifactId>spring-security</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>spring-security</name>
<description>Spring Boot 3 - Spring Security Demo</description>

<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<aerogear.version>1.0.0</aerogear.version>
<geoip2.version>4.0.0</geoip2.version>
<uap-java.version>1.5.3</uap-java.version>
<guava.version>31.1-jre</guava.version>
<passay.version>1.6.2</passay.version>
</properties>

<dependencies>
<!-- Web Server - Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<!-- Validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.passay</groupId>
<artifactId>passay</artifactId>
<version>${passay.version}</version>
</dependency>

<!-- Mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<!-- Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>

<!-- Database -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<!-- 2FA -->
<dependency>
<groupId>org.jboss.aerogear</groupId>
<artifactId>aerogear-otp-java</artifactId>
<version>${aerogear.version}</version>
<scope>compile</scope>
</dependency>

<!-- Maxmind GeoIP2 -->
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>${geoip2.version}</version>
</dependency>

<!-- User Agent parser -->
<dependency>
<groupId>com.github.ua-parser</groupId>
<artifactId>uap-java</artifactId>
<version>${uap-java.version}</version>
</dependency>

<!-- Google Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<!-- Testing Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Miscellaneous -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.thoughtcode.springboot3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.thoughtcode.springboot3.config;

import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl;
import org.springframework.util.ResourceUtils;

import com.maxmind.geoip2.DatabaseReader;

import io.thoughtcode.springboot3.config.auth.CustomAuthenticationProvider;
import io.thoughtcode.springboot3.config.auth.CustomRememberMeServices;
import io.thoughtcode.springboot3.config.auth.DifferentLocationChecker;
import io.thoughtcode.springboot3.config.dto.ActiveUserStore;
import io.thoughtcode.springboot3.repository.UserRepository;
import io.thoughtcode.springboot3.service.UserService;
import ua_parser.Parser;

@Configuration
public class AppConfig {

@Bean
ActiveUserStore activeUserStore() {
return new ActiveUserStore();
}

@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(11);
}

// @formatter:off
@Bean
DaoAuthenticationProvider authProvider(final UserRepository repository,
final UserService userService,
final PasswordEncoder encoder,
final DifferentLocationChecker locationChecker) {

final CustomAuthenticationProvider authProvider = new CustomAuthenticationProvider(repository);

authProvider.setUserDetailsService(userService);
authProvider.setPasswordEncoder(encoder);
authProvider.setPostAuthenticationChecks(locationChecker);

return authProvider;
}
// @formatter:on

@Bean
DatabaseReader geoip2Reader() throws IOException {
return new DatabaseReader.Builder(ResourceUtils.getFile("classpath:maxmind-geoip2/GeoLite2-City.mmdb")).build();
}

@Bean
Parser uaParser() throws IOException {
return new Parser();
}

@Bean
SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}

@Bean
RememberMeServices rememberMeServices(final UserDetailsService service, final UserRepository userRepository) {
return new CustomRememberMeServices("theKey", service, userRepository, new InMemoryTokenRepositoryImpl());
}
}
Loading