Skip to content

Commit

Permalink
Cleanup guide
Browse files Browse the repository at this point in the history
  • Loading branch information
gregturn committed Mar 11, 2016
1 parent 83222ee commit e2bee44
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
23 changes: 2 additions & 21 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The method is tagged with `@RequestMapping` to flag the path and the REST action
`@RestController` also tells Spring MVC to write the text directly into the HTTP response body, because there aren't any views. Instead, when you visit the page, you'll get a simple message in the browser as the focus of this guide is securing the page with LDAP.

== Build the unsecured web application

Before you secure the web application, verify that it works. To do that, you need to define some key beans. To do that, create an `Application` class.

`src/main/java/hello/Application.java`
Expand Down Expand Up @@ -116,27 +117,6 @@ include::complete/src/main/resources/test-server.ldif[]

NOTE: Using an LDIF file isn't standard configuration for a production system. However, it's very useful for testing purposes or guides.


== Create an Application class

`src/main/java/hello/Application.java`
[source,java]
----
include::complete/src/main/java/hello/Application.java[]
----

`@SpringBootApplication` is a convenience annotation that adds all of the following:

- `@Configuration` tags the class as a source of bean definitions for the application context.
- `@EnableAutoConfiguration` tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
- Normally you would add `@EnableWebMvc` for a Spring MVC app, but Spring Boot adds it automatically when it sees **spring-webmvc** on the classpath. This flags the application as a web application and activates key behaviors such as setting up a `DispatcherServlet`.
- `@ComponentScan` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.

The `main()` method uses Spring Boot's `SpringApplication.run()` method to launch an application. Did you notice that there wasn't a single line of XML? No **web.xml** file either. This web application is 100% pure Java and you didn't have to deal with configuring any plumbing or infrastructure.

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[]
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[]

:module: secured web application

If you visit the site at http://localhost:8080, you should be redirected to a login page provided by Spring Security.
Expand All @@ -148,6 +128,7 @@ Welcome to the home page!
....

== Summary

Congratulations! You have just written a web application and secured it with http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/[Spring Security]. In this case, you used an http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#ldap[LDAP-based user store].


Expand Down
2 changes: 1 addition & 1 deletion complete/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ repositories {
mavenCentral()
}

// tag::security[]
sourceCompatibility = 1.8
targetCompatibility = 1.8

// tag::security[]
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-security")
Expand Down
28 changes: 14 additions & 14 deletions complete/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@
<version>1.3.3.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<!-- tag::security[] -->
<dependencies>
<dependency>
Expand All @@ -43,10 +37,16 @@
<version>1.5.5</version>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>
<!-- end::security[] -->


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
5 changes: 2 additions & 3 deletions complete/src/main/java/hello/WebSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
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.servlet.configuration.EnableWebMvcSecurity;

@Configuration
@EnableWebMvcSecurity
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**").permitAll()
.anyRequest().fullyAuthenticated()
.and()
.formLogin();
Expand Down
23 changes: 11 additions & 12 deletions initial/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
<version>1.3.3.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
Expand All @@ -22,16 +32,5 @@
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


</project>

0 comments on commit e2bee44

Please sign in to comment.