diff --git a/spring-boot-auditing/.gitignore b/spring-boot-auditing/.gitignore new file mode 100644 index 0000000000..31ce405488 --- /dev/null +++ b/spring-boot-auditing/.gitignore @@ -0,0 +1,6 @@ +/target/ +.settings/ +.classpath +.project +*.iml +.idea \ No newline at end of file diff --git a/spring-boot-auditing/pom.xml b/spring-boot-auditing/pom.xml new file mode 100644 index 0000000000..9307db39ee --- /dev/null +++ b/spring-boot-auditing/pom.xml @@ -0,0 +1,198 @@ + + 4.0.0 + com.baeldung + spring-boot-auditing + 0.0.1-SNAPSHOT + war + spring-boot-auditing + This is simple boot application for Spring boot auditing test + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter-security + + + + io.dropwizard.metrics + metrics-core + + + + com.h2database + h2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter + + + com.jayway.jsonpath + json-path + test + + + org.springframework.boot + spring-boot-starter-mail + + + org.subethamail + subethasmtp + ${subethasmtp.version} + test + + + + org.webjars + bootstrap + ${bootstrap.version} + + + org.webjars + jquery + ${jquery.version} + + + + org.apache.tomcat + tomcat-servlet-api + ${tomee-servlet-api.version} + provided + + + + + + spring-boot + + + src/main/resources + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + + + + pl.project13.maven + git-commit-id-plugin + ${git-commit-id-plugin.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + + UTF-8 + 1.8 + 4.3.4.RELEASE + 2.2.1 + 3.1.1 + 3.3.7-1 + 3.1.7 + 8.5.11 + + + diff --git a/spring-boot-auditing/src/main/java/org/baeldung/Application.java b/spring-boot-auditing/src/main/java/org/baeldung/Application.java new file mode 100755 index 0000000000..bf7b7bd1a6 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/Application.java @@ -0,0 +1,13 @@ +package org.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) throws Throwable { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java b/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java new file mode 100755 index 0000000000..fecb8c5c0b --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java @@ -0,0 +1,18 @@ +package org.baeldung; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@Configuration +public class MvcConfig extends WebMvcConfigurerAdapter { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/home").setViewName("home"); + registry.addViewController("/").setViewName("home"); + registry.addViewController("/hello").setViewName("hello"); + registry.addViewController("/login").setViewName("login"); + } + +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java b/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java new file mode 100755 index 0000000000..199edce0bc --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java @@ -0,0 +1,34 @@ +package org.baeldung; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +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.WebSecurityConfigurerAdapter; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/", "/home").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .logout() + .permitAll(); + } + + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user").password("password").roles("USER"); + } +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java b/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java new file mode 100644 index 0000000000..bc36ac08b3 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java @@ -0,0 +1,36 @@ +package org.baeldung.auditing; + +import org.springframework.boot.actuate.audit.AuditEvent; +import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener; +import org.springframework.security.access.event.AbstractAuthorizationEvent; +import org.springframework.security.access.event.AuthorizationFailureEvent; +import org.springframework.security.web.FilterInvocation; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class ExposeAttemptedPathAuthorizationAuditListener extends AbstractAuthorizationAuditListener { + + public static final String AUTHORIZATION_FAILURE = "AUTHORIZATION_FAILURE"; + + @Override + public void onApplicationEvent(AbstractAuthorizationEvent event) { + if (event instanceof AuthorizationFailureEvent) { + onAuthorizationFailureEvent((AuthorizationFailureEvent) event); + } + } + + private void onAuthorizationFailureEvent(AuthorizationFailureEvent event) { + Map data = new HashMap<>(); + data.put("type", event.getAccessDeniedException().getClass().getName()); + data.put("message", event.getAccessDeniedException().getMessage()); + data.put("requestUrl", ((FilterInvocation)event.getSource()).getRequestUrl() ); + if (event.getAuthentication().getDetails() != null) { + data.put("details", event.getAuthentication().getDetails()); + } + publish(new AuditEvent(event.getAuthentication().getName(), AUTHORIZATION_FAILURE, + data)); + } +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java b/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java new file mode 100644 index 0000000000..5be8cebfd3 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java @@ -0,0 +1,25 @@ +package org.baeldung.auditing; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.actuate.audit.AuditEvent; +import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent; +import org.springframework.context.event.EventListener; +import org.springframework.security.web.authentication.WebAuthenticationDetails; +import org.springframework.stereotype.Component; + +@Component +public class LoginAttemptsLogger { + private static final Logger LOGGER = LoggerFactory.getLogger(LoginAttemptsLogger.class); + + @EventListener + public void auditEventHappened(AuditApplicationEvent auditApplicationEvent) { + AuditEvent auditEvent = auditApplicationEvent.getAuditEvent(); + LOGGER.debug("Principal " + auditEvent.getPrincipal() + " - " + auditEvent.getType()); + + WebAuthenticationDetails details = (WebAuthenticationDetails) auditEvent.getData().get("details"); + LOGGER.debug(" Remote IP address: " + details.getRemoteAddress()); + LOGGER.debug(" Session Id: " + details.getSessionId()); + LOGGER.debug(" Request URL: " + auditEvent.getData().get("requestUrl")); + } +} diff --git a/spring-boot-auditing/src/main/resources/application.properties b/spring-boot-auditing/src/main/resources/application.properties new file mode 100644 index 0000000000..cf09473b60 --- /dev/null +++ b/spring-boot-auditing/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.level.org.springframework=INFO \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/logback.xml b/spring-boot-auditing/src/main/resources/logback.xml new file mode 100644 index 0000000000..78913ee76f --- /dev/null +++ b/spring-boot-auditing/src/main/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/hello.html b/spring-boot-auditing/src/main/resources/templates/hello.html new file mode 100755 index 0000000000..46feef7e2c --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/hello.html @@ -0,0 +1,13 @@ + + + + Hello World! + + +

Hello [[${#httpServletRequest.remoteUser}]]!

+
+ +
+ + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/home.html b/spring-boot-auditing/src/main/resources/templates/home.html new file mode 100755 index 0000000000..fe4e8b337e --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/home.html @@ -0,0 +1,11 @@ + + + + Spring Security Example + + +

Welcome!

+ +

Click here to see a greeting.

+ + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/login.html b/spring-boot-auditing/src/main/resources/templates/login.html new file mode 100755 index 0000000000..a1785313f5 --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/login.html @@ -0,0 +1,20 @@ + + + + Spring Security Example + + +
+ Invalid username and password. +
+
+ You have been logged out. +
+
+
+
+
+
+ + \ No newline at end of file