Skip to content

Commit 8daa90f

Browse files
tschimanslavisa-baeldung
authored andcommitted
BAEL-89 Adding spring session tutorial code.
1 parent 9399b42 commit 8daa90f

11 files changed

Lines changed: 297 additions & 0 deletions

File tree

‎spring-session/jetty-ex/pom.xml‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>jetty-ex</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.0.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-jetty</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-security</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.session</groupId>
33+
<artifactId>spring-session</artifactId>
34+
<version>1.2.1.RELEASE</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-redis</artifactId>
39+
</dependency>
40+
</dependencies>
41+
42+
<dependencyManagement>
43+
<dependencies>
44+
<dependency>
45+
<groupId>org.springframework.cloud</groupId>
46+
<artifactId>spring-cloud-dependencies</artifactId>
47+
<version>Brixton.RELEASE</version>
48+
<type>pom</type>
49+
<scope>import</scope>
50+
</dependency>
51+
</dependencies>
52+
</dependencyManagement>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<version>3.3</version>
64+
<configuration>
65+
<source>1.8</source>
66+
<target>1.8</target>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.spring.session.tomcatex;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@SpringBootApplication
9+
@RestController
10+
public class JettyWebApplication {
11+
public static void main(String[] args) {
12+
SpringApplication.run(JettyWebApplication.class, args);
13+
}
14+
15+
@RequestMapping
16+
public String helloJetty() {
17+
return "hello Jetty";
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.spring.session.tomcatex;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7+
import org.springframework.security.config.http.SessionCreationPolicy;
8+
9+
@Configuration
10+
@EnableWebSecurity
11+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
12+
13+
@Override
14+
protected void configure(HttpSecurity http) throws Exception {
15+
http
16+
.sessionManagement()
17+
.sessionCreationPolicy(SessionCreationPolicy.NEVER)
18+
.and()
19+
.authorizeRequests().anyRequest().hasRole("ADMIN").and()
20+
.httpBasic().disable();
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.spring.session.tomcatex;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
6+
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
7+
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
8+
import org.springframework.session.web.http.HttpSessionStrategy;
9+
10+
@Configuration
11+
@EnableRedisHttpSession
12+
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
13+
@Bean
14+
public HttpSessionStrategy httpSessionStrategy() {
15+
return new HeaderHttpSessionStrategy();
16+
}
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
server.port=8081
2+
3+
spring.redis.host=localhost
4+
spring.redis.port=6379

‎spring-session/pom.xml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>parent-modules</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>spring-session</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
16+
<modules>
17+
<module>jetty-ex</module>
18+
<module>tomcat-ex</module>
19+
</modules>
20+
</project>

‎spring-session/tomcat-ex/pom.xml‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>tomcat-ex</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.0.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-security</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.cloud</groupId>
29+
<artifactId>spring-cloud-starter-zuul</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.session</groupId>
33+
<artifactId>spring-session</artifactId>
34+
<version>1.2.1.RELEASE</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-redis</artifactId>
39+
</dependency>
40+
</dependencies>
41+
42+
<dependencyManagement>
43+
<dependencies>
44+
<dependency>
45+
<groupId>org.springframework.cloud</groupId>
46+
<artifactId>spring-cloud-dependencies</artifactId>
47+
<version>Brixton.RELEASE</version>
48+
<type>pom</type>
49+
<scope>import</scope>
50+
</dependency>
51+
</dependencies>
52+
</dependencyManagement>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<version>3.3</version>
64+
<configuration>
65+
<source>1.8</source>
66+
<target>1.8</target>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.spring.session.tomcatex;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
10+
@Configuration
11+
@EnableWebSecurity
12+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
13+
14+
@Autowired
15+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
16+
auth.inMemoryAuthentication()
17+
.withUser("user").password("password").roles("USER").and()
18+
.withUser("admin").password("password").roles("ADMIN");
19+
}
20+
21+
@Override
22+
protected void configure(HttpSecurity http) throws Exception {
23+
http
24+
.httpBasic().and()
25+
// .csrf().disable()
26+
.authorizeRequests()
27+
.antMatchers("/").permitAll()
28+
.antMatchers("/tomcat").hasRole("USER")
29+
.antMatchers("/tomcat/admin").hasRole("ADMIN")
30+
.anyRequest().authenticated();
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.spring.session.tomcatex;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
5+
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
6+
7+
@Configuration
8+
@EnableRedisHttpSession
9+
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.spring.session.tomcatex;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@SpringBootApplication
9+
@RestController
10+
public class TomcatWebApplication {
11+
public static void main(String[] args) {
12+
SpringApplication.run(TomcatWebApplication.class, args);
13+
}
14+
15+
@RequestMapping
16+
public String helloDefault() {
17+
return "hello default";
18+
}
19+
20+
@RequestMapping("/tomcat")
21+
public String helloTomcat() {
22+
return "hello tomcat";
23+
}
24+
25+
@RequestMapping("/tomcat/admin")
26+
public String helloTomcatAdmin() {
27+
return "hello tomcat admin";
28+
}
29+
}

0 commit comments

Comments
 (0)