Skip to content

Commit 607f78a

Browse files
author
Dave Syer
committed
Add secure sample with JDBC and data.sql
We can't easily solve the problem by not allowing Spring Security to eagerly instantiate everything, but we can be defensive about data.sql and make sure it is executed even if the listener isn't yet registered. Fixes gh-1386
1 parent 00ef265 commit 607f78a

File tree

12 files changed

+392
-0
lines changed

12 files changed

+392
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ private void runSchemaScripts() {
7878
try {
7979
this.applicationContext.publishEvent(new DataSourceInitializedEvent(
8080
this.dataSource));
81+
// The listener might not be registered yet, so don't rely on it.
82+
if (!this.initialized) {
83+
runDataScripts();
84+
this.initialized = true;
85+
}
8186
}
8287
catch (IllegalStateException ex) {
8388
logger.warn("Could not send event to complete DataSource initialization ("

spring-boot-samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<module>spring-boot-sample-web-method-security</module>
5454
<module>spring-boot-sample-web-secure</module>
5555
<module>spring-boot-sample-web-secure-custom</module>
56+
<module>spring-boot-sample-web-secure-jdbc</module>
5657
<module>spring-boot-sample-web-static</module>
5758
<module>spring-boot-sample-web-jsp</module>
5859
<module>spring-boot-sample-web-ui</module>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<!-- Your own application should inherit from spring-boot-starter-parent -->
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-samples</artifactId>
8+
<version>1.1.6.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>spring-boot-sample-web-secure-jdbc</artifactId>
11+
<name>spring-boot-sample-web-secure-jdbc</name>
12+
<description>Spring Boot Web Secure Sample</description>
13+
<url>http://projects.spring.io/spring-boot/</url>
14+
<organization>
15+
<name>Pivotal Software, Inc.</name>
16+
<url>http://www.spring.io</url>
17+
</organization>
18+
<properties>
19+
<main.basedir>${basedir}/../..</main.basedir>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-security</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-jdbc</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.h2database</groupId>
36+
<artifactId>h2</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.httpcomponents</groupId>
40+
<artifactId>httpclient</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.ui.secure;
18+
19+
import java.util.Date;
20+
import java.util.Map;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.security.SecurityProperties;
27+
import org.springframework.boot.builder.SpringApplicationBuilder;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.ComponentScan;
30+
import org.springframework.core.annotation.Order;
31+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
32+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
33+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
34+
import org.springframework.stereotype.Controller;
35+
import org.springframework.web.bind.annotation.RequestMapping;
36+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
37+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
38+
39+
@EnableAutoConfiguration
40+
@ComponentScan
41+
@Controller
42+
public class SampleWebSecureCustomApplication extends WebMvcConfigurerAdapter {
43+
44+
@RequestMapping("/")
45+
public String home(Map<String, Object> model) {
46+
model.put("message", "Hello World");
47+
model.put("title", "Hello Home");
48+
model.put("date", new Date());
49+
return "home";
50+
}
51+
52+
@RequestMapping("/foo")
53+
public String foo() {
54+
throw new RuntimeException("Expected exception in controller");
55+
}
56+
57+
public static void main(String[] args) throws Exception {
58+
new SpringApplicationBuilder(SampleWebSecureCustomApplication.class).run(args);
59+
}
60+
61+
@Override
62+
public void addViewControllers(ViewControllerRegistry registry) {
63+
registry.addViewController("/login").setViewName("login");
64+
}
65+
66+
@Bean
67+
public ApplicationSecurity applicationSecurity() {
68+
return new ApplicationSecurity();
69+
}
70+
71+
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
72+
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
73+
74+
@Autowired
75+
private SecurityProperties security;
76+
77+
@Autowired
78+
private DataSource dataSource;
79+
80+
@Override
81+
protected void configure(HttpSecurity http) throws Exception {
82+
http.authorizeRequests().antMatchers("/css/**").permitAll().anyRequest()
83+
.fullyAuthenticated().and().formLogin().loginPage("/login")
84+
.failureUrl("/login?error").permitAll();
85+
}
86+
87+
@Override
88+
public void configure(AuthenticationManagerBuilder auth) throws Exception {
89+
auth.jdbcAuthentication().dataSource(this.dataSource);
90+
}
91+
92+
}
93+
94+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
debug: true
2+
spring.thymeleaf.cache: false
3+
security.basic.enabled: false
4+
logging.level.org.springframework.security: INFO
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
insert into users (username, password, enabled) values ('user', 'user', true);
2+
3+
insert into authorities (username, authority) values ('user', 'ROLE_ADMIN');

spring-boot-samples/spring-boot-sample-web-secure-jdbc/src/main/resources/static/css/bootstrap.min.css

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Error</title>
5+
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"
6+
href="../../css/bootstrap.min.css" />
7+
</head>
8+
<body>
9+
<div class="container">
10+
<div class="navbar">
11+
<div class="navbar-inner">
12+
<a class="brand" href="http://www.thymeleaf.org"> Thymeleaf -
13+
Plain </a>
14+
<ul class="nav">
15+
<li><a th:href="@{/}" href="home.html"> Home </a></li>
16+
<li><a th:href="@{/logout}" href="logout"> Logout </a></li>
17+
</ul>
18+
</div>
19+
</div>
20+
<h1 th:text="${title}">Title</h1>
21+
<div id="created" th:text="${#dates.format(timestamp)}">July 11,
22+
2012 2:17:16 PM CDT</div>
23+
<div>
24+
There was an unexpected error (type=<span th:text="${error}">Bad</span>, status=<span th:text="${status}">500</span>).
25+
</div>
26+
<div th:text="${message}">Fake content</div>
27+
<div>
28+
Please contact the operator with the above information.
29+
</div>
30+
</div>
31+
</body>
32+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title th:text="${title}">Title</title>
5+
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"
6+
href="../../css/bootstrap.min.css" />
7+
</head>
8+
<body>
9+
<div class="container">
10+
<div class="navbar">
11+
<div class="navbar-inner">
12+
<a class="brand" href="http://www.thymeleaf.org"> Thymeleaf -
13+
Plain </a>
14+
<ul class="nav">
15+
<li><a th:href="@{/}" href="home.html"> Home </a></li>
16+
<li><a th:href="@{/logout}" href="logout"> Logout </a></li>
17+
</ul>
18+
</div>
19+
</div>
20+
<h1 th:text="${title}">Title</h1>
21+
<div th:text="${message}">Fake content</div>
22+
<div id="created" th:text="${#dates.format(date)}">July 11,
23+
2012 2:17:16 PM CDT</div>
24+
</div>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)