fixed migration to spring boot 2, plus now added properties in articles
This commit is contained in:
parent
79a50387d1
commit
b355af4d01
|
@ -1,15 +1,16 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-security-mvc-session</artifactId>
|
<artifactId>spring-security-mvc-session</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<name>spring-security-mvc-session</name>
|
<name>spring-security-mvc-session</name>
|
||||||
<packaging>war</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-boot-2</relativePath>
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
@ -35,20 +36,13 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.tomcat.embed</groupId>
|
<groupId>org.apache.tomcat.embed</groupId>
|
||||||
<artifactId>tomcat-embed-jasper</artifactId>
|
<artifactId>tomcat-embed-jasper</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- web -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>jstl</artifactId>
|
<artifactId>jstl</artifactId>
|
||||||
|
@ -56,11 +50,9 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- ops -->
|
<!-- ops -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.codahale.metrics</groupId>
|
<groupId>io.dropwizard.metrics</groupId>
|
||||||
<artifactId>metrics-core</artifactId>
|
<artifactId>metrics-core</artifactId>
|
||||||
<version>${codahale.metrics.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Test -->
|
<!-- Test -->
|
||||||
|
@ -69,57 +61,24 @@
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security</groupId>
|
||||||
|
<artifactId>spring-security-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>spring-security-mvc-session</finalName>
|
|
||||||
<resources>
|
|
||||||
<resource>
|
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<filtering>true</filtering>
|
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
|
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<version>${maven-war-plugin.version}</version>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.codehaus.cargo</groupId>
|
|
||||||
<artifactId>cargo-maven2-plugin</artifactId>
|
|
||||||
<version>${cargo-maven2-plugin.version}</version>
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<wait>true</wait>
|
<mainClass>com.baeldung.SpringSessionApplication</mainClass>
|
||||||
<container>
|
<layout>JAR</layout>
|
||||||
<containerId>jetty8x</containerId>
|
|
||||||
<type>embedded</type>
|
|
||||||
<systemProperties>
|
|
||||||
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
|
||||||
</systemProperties>
|
|
||||||
</container>
|
|
||||||
<configuration>
|
|
||||||
<properties>
|
|
||||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
|
||||||
</properties>
|
|
||||||
</configuration>
|
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<!-- various -->
|
|
||||||
<codahale.metrics.version>3.0.2</codahale.metrics.version>
|
|
||||||
|
|
||||||
<!-- Maven plugins -->
|
|
||||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.monitoring;
|
package com.baeldung.monitoring;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.security;
|
package com.baeldung.security;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.security;
|
package com.baeldung.security;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.baeldung.spring;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class MvcConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||||
|
registry.addViewController("/anonymous.html");
|
||||||
|
|
||||||
|
registry.addViewController("/login.html");
|
||||||
|
registry.addViewController("/homepage.html");
|
||||||
|
registry.addViewController("/sessionExpired.html");
|
||||||
|
registry.addViewController("/invalidSession.html");
|
||||||
|
registry.addViewController("/console.html");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Spring Boot supports configuring a ViewResolver with properties
|
||||||
|
*/
|
||||||
|
// @Bean
|
||||||
|
// public ViewResolver viewResolver() {
|
||||||
|
// final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||||
|
//
|
||||||
|
// bean.setViewClass(JstlView.class);
|
||||||
|
// bean.setPrefix("/WEB-INF/view/");
|
||||||
|
// bean.setSuffix(".jsp");
|
||||||
|
// }
|
||||||
|
}
|
|
@ -1,11 +1,9 @@
|
||||||
package org.baeldung.spring;
|
package com.baeldung.spring;
|
||||||
|
|
||||||
import org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
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.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
@ -13,9 +11,10 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||||
import org.springframework.security.web.session.HttpSessionEventPublisher;
|
import org.springframework.security.web.session.HttpSessionEventPublisher;
|
||||||
|
|
||||||
|
import com.baeldung.security.MySimpleUrlAuthenticationSuccessHandler;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
// @ImportResource({ "classpath:webSecurityConfig.xml" })
|
// @ImportResource({ "classpath:webSecurityConfig.xml" })
|
||||||
@EnableWebSecurity
|
|
||||||
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
public SecSecurityConfig() {
|
public SecSecurityConfig() {
|
||||||
|
@ -39,7 +38,7 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
.csrf().disable()
|
.csrf().disable()
|
||||||
.authorizeRequests()
|
.authorizeRequests()
|
||||||
.antMatchers("/anonymous*").anonymous()
|
.antMatchers("/anonymous*").anonymous()
|
||||||
.antMatchers("/login*").permitAll()
|
.antMatchers("/login*","/invalidSession*", "/sessionExpired*").permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
.and()
|
.and()
|
||||||
.formLogin()
|
.formLogin()
|
|
@ -1,12 +1,11 @@
|
||||||
package org.baeldung.web;
|
package com.baeldung.web;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import javax.servlet.http.HttpSessionEvent;
|
import javax.servlet.http.HttpSessionEvent;
|
||||||
import javax.servlet.http.HttpSessionListener;
|
import javax.servlet.http.HttpSessionListener;
|
||||||
|
|
||||||
import org.baeldung.monitoring.MetricRegistrySingleton;
|
import com.baeldung.monitoring.MetricRegistrySingleton;
|
||||||
|
|
||||||
import com.codahale.metrics.Counter;
|
import com.codahale.metrics.Counter;
|
||||||
|
|
||||||
public class SessionListenerWithMetrics implements HttpSessionListener {
|
public class SessionListenerWithMetrics implements HttpSessionListener {
|
|
@ -1,44 +0,0 @@
|
||||||
package org.baeldung.spring;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
||||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
|
||||||
import org.springframework.web.servlet.view.JstlView;
|
|
||||||
|
|
||||||
@EnableWebMvc
|
|
||||||
@Configuration
|
|
||||||
public class MvcConfig implements WebMvcConfigurer {
|
|
||||||
|
|
||||||
public MvcConfig() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
|
||||||
|
|
||||||
registry.addViewController("/anonymous.html");
|
|
||||||
|
|
||||||
registry.addViewController("/login.html");
|
|
||||||
registry.addViewController("/homepage.html");
|
|
||||||
registry.addViewController("/sessionExpired.html");
|
|
||||||
registry.addViewController("/invalidExpired.html");
|
|
||||||
registry.addViewController("/console.html");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ViewResolver viewResolver() {
|
|
||||||
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
|
||||||
|
|
||||||
bean.setViewClass(JstlView.class);
|
|
||||||
bean.setPrefix("/WEB-INF/view/");
|
|
||||||
bean.setSuffix(".jsp");
|
|
||||||
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
server.servlet.session.timeout=60s
|
||||||
|
|
||||||
|
spring.mvc.view.prefix=/WEB-INF/view/
|
||||||
|
spring.mvc.view.suffix=.jsp
|
||||||
|
|
||||||
|
## Secure Session Cookie configurations
|
||||||
|
#server.servlet.session.cookie.http-only=true
|
||||||
|
#server.servlet.session.cookie.secure=true
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringContextIntegrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,19 +0,0 @@
|
||||||
package org.baeldung;
|
|
||||||
|
|
||||||
import org.baeldung.spring.MvcConfig;
|
|
||||||
import org.baeldung.spring.SecSecurityConfig;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@ContextConfiguration(classes = { MvcConfig.class, SecSecurityConfig.class })
|
|
||||||
@WebAppConfiguration
|
|
||||||
public class SpringContextIntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
package org.baeldung;
|
|
||||||
|
|
||||||
import org.baeldung.spring.MvcConfig;
|
|
||||||
import org.baeldung.spring.SecSecurityConfig;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@ContextConfiguration(classes = { MvcConfig.class, SecSecurityConfig.class })
|
|
||||||
@WebAppConfiguration
|
|
||||||
public class SpringContextTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue