Skip to content

Commit 678c7da

Browse files
committed
Turned application into a Spring Boot application
1 parent 8970124 commit 678c7da

17 files changed

Lines changed: 224 additions & 289 deletions

File tree

session-handling-spring-security/pom.xml

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,78 @@
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111
<artifactId>session-handling-spring-security</artifactId>
12-
<packaging>war</packaging>
12+
<packaging>jar</packaging>
1313
<name>Session Handling - Spring Security</name>
1414

15-
<description>Session Handling with Spring Security sample project. Requires a server like Apache Tomcat 8 (with
16-
Servlet 3.1 support) or the Maven Jetty plugin. Two users with two different roles exist: user/user and
17-
admin/admin. After launching, open the web application in your browser at
18-
http://localhost:8080/session-handling-spring-security
15+
<description>Session Handling with Spring Security sample project. Start via the main method in the Application
16+
class. After launching, open the web application in your browser at http://localhost:8080.
1917
</description>
2018

19+
<properties>
20+
<start-class>de.dominikschadow.javasecurity.Application</start-class>
21+
</properties>
22+
2123
<dependencies>
2224
<dependency>
23-
<groupId>javax.servlet</groupId>
24-
<artifactId>javax.servlet-api</artifactId>
25-
</dependency>
26-
<dependency>
27-
<groupId>com.sun.faces</groupId>
28-
<artifactId>jsf-api</artifactId>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
2927
</dependency>
3028
<dependency>
31-
<groupId>com.sun.faces</groupId>
32-
<artifactId>jsf-impl</artifactId>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-security</artifactId>
3331
</dependency>
3432
<dependency>
35-
<groupId>org.springframework.security</groupId>
36-
<artifactId>spring-security-web</artifactId>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-data-jpa</artifactId>
3735
</dependency>
3836
<dependency>
39-
<groupId>org.springframework.security</groupId>
40-
<artifactId>spring-security-config</artifactId>
37+
<groupId>org.webjars</groupId>
38+
<artifactId>bootstrap</artifactId>
4139
</dependency>
4240
<dependency>
43-
<groupId>org.springframework</groupId>
44-
<artifactId>spring-jdbc</artifactId>
45-
</dependency>
46-
<dependency>
47-
<groupId>commons-dbcp</groupId>
48-
<artifactId>commons-dbcp</artifactId>
41+
<groupId>org.webjars</groupId>
42+
<artifactId>webjars-locator</artifactId>
4943
</dependency>
5044
<dependency>
5145
<groupId>com.h2database</groupId>
5246
<artifactId>h2</artifactId>
53-
</dependency>
54-
<dependency>
55-
<groupId>org.slf4j</groupId>
56-
<artifactId>slf4j-api</artifactId>
57-
</dependency>
58-
<dependency>
59-
<groupId>org.slf4j</groupId>
60-
<artifactId>slf4j-log4j12</artifactId>
61-
</dependency>
62-
<dependency>
63-
<groupId>commons-logging</groupId>
64-
<artifactId>commons-logging</artifactId>
47+
<scope>runtime</scope>
6548
</dependency>
6649
</dependencies>
6750

6851
<build>
6952
<finalName>${project.artifactId}</finalName>
70-
<defaultGoal>jetty:run-war</defaultGoal>
53+
<defaultGoal>spring-boot:run</defaultGoal>
7154
<plugins>
7255
<plugin>
73-
<groupId>org.eclipse.jetty</groupId>
74-
<artifactId>jetty-maven-plugin</artifactId>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
<executions>
59+
<execution>
60+
<goals>
61+
<goal>build-info</goal>
62+
</goals>
63+
<configuration>
64+
<additionalProperties>
65+
<versions.spring-boot>${project.parent.parent.version}</versions.spring-boot>
66+
</additionalProperties>
67+
</configuration>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
<plugin>
72+
<groupId>com.spotify</groupId>
73+
<artifactId>docker-maven-plugin</artifactId>
7574
<configuration>
76-
<webApp>
77-
<contextPath>/session-handling-spring-security</contextPath>
78-
</webApp>
75+
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
76+
<dockerDirectory>src/main/docker</dockerDirectory>
77+
<resources>
78+
<resource>
79+
<targetPath>/</targetPath>
80+
<directory>${project.build.directory}</directory>
81+
<include>${project.build.finalName}.jar</include>
82+
</resource>
83+
</resources>
7984
</configuration>
8085
</plugin>
8186
</plugins>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM openjdk:8-jre-alpine
2+
MAINTAINER Dominik Schadow <dominikschadow@gmail.com>
3+
4+
VOLUME /tmp
5+
6+
ADD session-handling-spring-security.jar app.jar
7+
8+
RUN sh -c 'touch /app.jar'
9+
10+
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "app.jar"]

session-handling-spring-security/src/main/java/de/dominikschadow/javasecurity/sessionhandling/spring/SecurityWebApplicationInitializer.java renamed to session-handling-spring-security/src/main/java/de/dominikschadow/javasecurity/Application.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
package de.dominikschadow.javasecurity.sessionhandling.spring;
18+
package de.dominikschadow.javasecurity;
1919

20-
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
2122

2223
/**
23-
* Activates the Spring Security configuration.
24+
* Starter class for the Spring Boot application.
2425
*
2526
* @author Dominik Schadow
2627
*/
27-
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
28-
public SecurityWebApplicationInitializer() {
29-
super(SecurityConfig.class);
28+
@SpringBootApplication
29+
public class Application {
30+
public static void main(String[] args) {
31+
SpringApplication.run(Application.class, args);
3032
}
3133
}

session-handling-spring-security/src/main/java/de/dominikschadow/javasecurity/sessionhandling/spring/SecurityConfig.java renamed to session-handling-spring-security/src/main/java/de/dominikschadow/javasecurity/sessionhandling/config/WebSecurityConfig.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
package de.dominikschadow.javasecurity.sessionhandling.spring;
18+
package de.dominikschadow.javasecurity.sessionhandling.config;
1919

2020
import org.springframework.beans.factory.annotation.Autowired;
2121
import org.springframework.context.annotation.Bean;
22-
import org.springframework.context.annotation.ComponentScan;
23-
import org.springframework.context.annotation.Configuration;
24-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2522
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2623
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
2724
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -32,20 +29,19 @@
3229

3330
import javax.sql.DataSource;
3431

35-
import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.H2;
36-
3732
/**
3833
* Spring Security configuration for the session handling sample project.
3934
*
4035
* @author Dominik Schadow
4136
*/
42-
@Configuration
4337
@EnableWebSecurity
4438
@EnableGlobalMethodSecurity(prePostEnabled = true)
45-
@ComponentScan("de.dominikschadow.javasecurity.sessionhandling.greetings")
46-
public class SecurityConfig extends WebSecurityConfigurerAdapter {
39+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
40+
@Autowired
41+
private DataSource dataSource;
42+
4743
@Autowired
48-
public void configAuthentication(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
44+
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
4945
auth.jdbcAuthentication().dataSource(dataSource)
5046
.passwordEncoder(passwordEncoder())
5147
.usersByUsernameQuery("select username, password, active from users where username = ?")
@@ -57,15 +53,15 @@ protected void configure(HttpSecurity http) throws Exception {
5753
// @formatter:off
5854
http
5955
.authorizeRequests()
60-
.antMatchers("/*", "/javax.faces.resource/**").permitAll()
56+
.antMatchers("/*").permitAll()
6157
.antMatchers("/user/**").hasAnyRole("USER", "ADMIN")
6258
.antMatchers("/admin/**").hasRole("ADMIN")
6359
.and()
6460
.formLogin()
6561
.and()
6662
.logout()
67-
.logoutUrl("/logout")
68-
.logoutSuccessUrl("/logout.xhtml");
63+
.logoutSuccessUrl("/")
64+
.permitAll();
6965
// @formatter:on
7066
}
7167

@@ -79,9 +75,4 @@ protected void configure(HttpSecurity http) throws Exception {
7975
public PasswordEncoder passwordEncoder() {
8076
return new BCryptPasswordEncoder(10);
8177
}
82-
83-
@Bean
84-
public DataSource dataSource() {
85-
return new EmbeddedDatabaseBuilder().setType(H2).addScript("schema.sql").addScripts("data.sql").build();
86-
}
8778
}

session-handling-spring-security/src/main/java/de/dominikschadow/javasecurity/sessionhandling/users/GreetingBean.java renamed to session-handling-spring-security/src/main/java/de/dominikschadow/javasecurity/sessionhandling/controller/GreetingController.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,37 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
package de.dominikschadow.javasecurity.sessionhandling.users;
18+
package de.dominikschadow.javasecurity.sessionhandling.controller;
1919

2020
import de.dominikschadow.javasecurity.sessionhandling.greetings.GreetingService;
21-
22-
import javax.faces.bean.ManagedBean;
23-
import javax.faces.bean.ManagedProperty;
24-
import javax.faces.bean.RequestScoped;
21+
import org.springframework.stereotype.Controller;
22+
import org.springframework.ui.Model;
23+
import org.springframework.web.bind.annotation.GetMapping;
2524

2625
/**
27-
* Greeting ManagedBean which integrates a Spring Bean to return the user/ admin greeting to the calling JSF page.
26+
* Greeting controller to return the user/ admin greeting to the caller.
2827
*
2928
* @author Dominik Schadow
3029
*/
31-
@ManagedBean
32-
@RequestScoped
33-
public class GreetingBean {
34-
@ManagedProperty(value = "#{greetingServiceImpl}")
30+
@Controller
31+
public class GreetingController {
3532
private GreetingService greetingService;
3633

37-
public String greetUser() {
38-
return greetingService.greetUser();
34+
public GreetingController(GreetingService greetingService) {
35+
this.greetingService = greetingService;
3936
}
4037

41-
public String greetAdmin() {
42-
return greetingService.greetAdmin();
38+
@GetMapping("user/user")
39+
public String greetUser(Model model) {
40+
model.addAttribute("greeting", greetingService.greetUser());
41+
42+
return "user/user";
4343
}
4444

45-
public void setGreetingService(GreetingService greetingService) {
46-
this.greetingService = greetingService;
45+
@GetMapping("admin/admin")
46+
public String greetAdmin(Model model) {
47+
model.addAttribute("greeting", greetingService.greetAdmin());
48+
49+
return "admin/admin";
4750
}
4851
}

session-handling-spring-security/src/main/java/de/dominikschadow/javasecurity/sessionhandling/users/UserBean.java renamed to session-handling-spring-security/src/main/java/de/dominikschadow/javasecurity/sessionhandling/controller/IndexController.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22
* Copyright (C) 2017 Dominik Schadow, dominikschadow@gmail.com
33
*
44
* This file is part of the Java Security project.
5-
*
5+
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
9-
*
9+
*
1010
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
11+
*
1212
* Unless required by applicable law or agreed to in writing, software
1313
* distributed under the License is distributed on an "AS IS" BASIS,
1414
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
package de.dominikschadow.javasecurity.sessionhandling.users;
19-
20-
import org.springframework.web.context.request.RequestContextHolder;
18+
package de.dominikschadow.javasecurity.sessionhandling.controller;
2119

22-
import javax.faces.bean.ManagedBean;
23-
import javax.faces.bean.RequestScoped;
20+
import org.springframework.stereotype.Controller;
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.RequestMapping;
2423

2524
/**
26-
* Simple ManagedBean to log out the current user and to return the current session id.
25+
* Index controller for all home page related operations.
2726
*
2827
* @author Dominik Schadow
2928
*/
30-
@ManagedBean(name = "userBean")
31-
@RequestScoped
32-
public class UserBean {
33-
public String getSessionId() {
34-
return RequestContextHolder.currentRequestAttributes().getSessionId();
29+
@Controller
30+
@RequestMapping("/")
31+
public class IndexController {
32+
@GetMapping
33+
public String index() {
34+
return "index";
3535
}
3636
}

session-handling-spring-security/src/main/resources/log4j.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
5+
<base href="/"/>
6+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
7+
<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css"/>
8+
<title>Session Handling - Spring Security</title>
9+
</head>
10+
<body>
11+
<div class="container" id="main">
12+
<div class="row" id="welcome">
13+
<div class="col-12">
14+
<h1>User Profile
15+
<small th:text="${greeting}"></small>
16+
</h1>
17+
18+
<p>Your current session is <strong th:text="${#httpServletRequest.session.id}"></strong>.</p>
19+
</div>
20+
</div>
21+
22+
<div class="row" id="links">
23+
<div class="col-12">
24+
<a th:href="@{user/user}">User</a> | Admin
25+
</div>
26+
</div>
27+
28+
<div class="row" id="logout" th:if="${#httpServletRequest.remoteUser}">
29+
<div class="col-12">
30+
<form th:action="@{/logout}" method="post">
31+
<button type="submit" class="btn btn-default btn-sm">
32+
<span class="glyphicon glyphicon-off"></span> Log out
33+
</button>
34+
</form>
35+
</div>
36+
</div>
37+
</div>
38+
</body>
39+
</html>

0 commit comments

Comments
 (0)