Skip to content

Commit 182a752

Browse files
forketyforkEugen
authored andcommitted
BAEL-1222: Hit the Ground Running with the Spring Security Java Configuration (eugenp#2988)
1 parent f965483 commit 182a752

10 files changed

Lines changed: 212 additions & 0 deletions

File tree

‎guest/spring-security/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Building
2+
3+
To build the module, use Maven's `package` goal:
4+
5+
```
6+
mvn clean package
7+
```
8+
9+
## Running
10+
11+
To run the application, use Spring Boot's `run` goal:
12+
13+
```
14+
mvn spring-boot:run
15+
```
16+
17+
The application will be accessible at [http://localhost:8080/](http://localhost:8080/)

‎guest/spring-security/pom.xml‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.stackify.guest</groupId>
8+
<artifactId>spring-security</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.0.0.M6</version>
15+
<relativePath/>
16+
</parent>
17+
18+
<name>spring-security</name>
19+
<description>Spring Security Sample Project</description>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</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.thymeleaf</groupId>
32+
<artifactId>thymeleaf-spring5</artifactId>
33+
<version>3.0.8.RELEASE</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-security</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-jdbc</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.h2database</groupId>
45+
<artifactId>h2</artifactId>
46+
<scope>runtime</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<repositories>
51+
<repository>
52+
<id>spring-milestones</id>
53+
<name>Spring Milestones</name>
54+
<url>https://repo.spring.io/milestone</url>
55+
<snapshots>
56+
<enabled>false</enabled>
57+
</snapshots>
58+
</repository>
59+
</repositories>
60+
<pluginRepositories>
61+
<pluginRepository>
62+
<id>spring-milestones</id>
63+
<name>Spring Milestones</name>
64+
<url>https://repo.spring.io/milestone</url>
65+
<snapshots>
66+
<enabled>false</enabled>
67+
</snapshots>
68+
</pluginRepository>
69+
</pluginRepositories>
70+
71+
<properties>
72+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
73+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
74+
<java.version>1.8</java.version>
75+
</properties>
76+
77+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.stackify.guest.springsecurity;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ComponentScan;
6+
7+
@SpringBootApplication
8+
@ComponentScan(basePackages = {"com.stackify.guest.springsecurity"})
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.stackify.guest.springsecurity.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
5+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6+
7+
@Configuration
8+
public class WebMvcConfiguration implements WebMvcConfigurer {
9+
10+
@Override
11+
public void addViewControllers(ViewControllerRegistry registry) {
12+
registry.addViewController("/customLogin").setViewName("customLogin");
13+
registry.addViewController("/loginSuccess").setViewName("index");
14+
}
15+
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.stackify.guest.springsecurity.config;
2+
3+
import org.springframework.context.annotation.Bean;
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.core.userdetails.UserDetailsService;
8+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
9+
import org.springframework.security.crypto.password.PasswordEncoder;
10+
import org.springframework.security.provisioning.JdbcUserDetailsManager;
11+
12+
import javax.sql.DataSource;
13+
14+
@EnableWebSecurity
15+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
16+
17+
@Bean
18+
public UserDetailsService jdbcUserDetailsService(DataSource dataSource) {
19+
JdbcUserDetailsManager manager = new JdbcUserDetailsManager();
20+
manager.setDataSource(dataSource);
21+
return manager;
22+
}
23+
24+
@Bean
25+
public PasswordEncoder passwordEncoder() {
26+
return new BCryptPasswordEncoder();
27+
}
28+
29+
@Override
30+
protected void configure(HttpSecurity http) throws Exception {
31+
http.authorizeRequests()
32+
.antMatchers("/css/**").permitAll()
33+
.anyRequest().authenticated()
34+
.and().formLogin()
35+
.loginPage("/customLogin")
36+
.defaultSuccessUrl("/loginSuccess", true)
37+
.permitAll();
38+
}
39+
40+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT INTO users VALUES ('jill', '$2a$04$qUlqAEEYF1YvrpJMosodoewgL6aO.qgHytl2k5L7kdXEWnJsFdxvq', TRUE);
2+
INSERT INTO authorities VALUES ('jill', 'USERS');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE users (
2+
username VARCHAR(256) PRIMARY KEY,
3+
password VARCHAR(256),
4+
enabled BOOLEAN
5+
);
6+
7+
CREATE TABLE authorities (
8+
username VARCHAR(256) REFERENCES users (username),
9+
authority VARCHAR(256)
10+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bad-login {
2+
color: red;
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
<link rel="stylesheet" href="/css/styles.css">
6+
</head>
7+
<body>
8+
<form th:action="@{/customLogin}" method="post">
9+
<fieldset>
10+
<label for="username">Login:</label>
11+
<input id="username" name="username">
12+
<label for="password">Password:</label>
13+
<input id="password" name="password" type="password">
14+
</fieldset>
15+
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
16+
<input type="submit" value="Login">
17+
<div th:if="${param.error}" class="bad-login">Bad login or password.</div>
18+
<div th:if="${param.logout}">Log out successful.</div>
19+
</form>
20+
</body>
21+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<p>Hello, <span th:text="${
5+
T(org.springframework.security.core.context.SecurityContextHolder)
6+
.context.authentication.principal.username}"></span>!</p>
7+
<form action="/logout" method="post">
8+
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
9+
<input type="submit" value="Log Out">
10+
</form>
11+
</html>

0 commit comments

Comments
 (0)