-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
avihavai
committed
Dec 19, 2016
0 parents
commit 7ef4a47
Showing
11 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>sec</groupId> | ||
<artifactId>cybersecuritybase-project</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.4.2.RELEASE</version> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-thymeleaf</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-security</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-test</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-devtools</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<fork>true</fork> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>cobertura-maven-plugin</artifactId> | ||
<version>2.7</version> | ||
<configuration> | ||
<instrumentation> | ||
<ignoreTrivial>true</ignoreTrivial> | ||
</instrumentation> | ||
<formats> | ||
<format>html</format> | ||
<format>xml</format> | ||
</formats> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
12 changes: 12 additions & 0 deletions
12
src/main/java/sec/project/CyberSecurityBaseProjectApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package sec.project; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class CyberSecurityBaseProjectApplication { | ||
|
||
public static void main(String[] args) throws Throwable { | ||
SpringApplication.run(CyberSecurityBaseProjectApplication.class); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/sec/project/config/CustomUserDetailsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package sec.project.config; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import javax.annotation.PostConstruct; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CustomUserDetailsService implements UserDetailsService { | ||
|
||
private Map<String, String> accountDetails; | ||
|
||
@PostConstruct | ||
public void init() { | ||
// this data would typically be retrieved from a database | ||
this.accountDetails = new TreeMap<>(); | ||
this.accountDetails.put("ted", "$2a$06$rtacOjuBuSlhnqMO2GKxW.Bs8J6KI0kYjw/gtF0bfErYgFyNTZRDm"); | ||
} | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
if (!this.accountDetails.containsKey(username)) { | ||
throw new UsernameNotFoundException("No such user: " + username); | ||
} | ||
|
||
return new org.springframework.security.core.userdetails.User( | ||
username, | ||
this.accountDetails.get(username), | ||
true, | ||
true, | ||
true, | ||
true, | ||
Arrays.asList(new SimpleGrantedAuthority("USER"))); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/sec/project/config/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package sec.project.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
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.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private UserDetailsService userDetailsService; | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
// no real security at the moment | ||
http.authorizeRequests() | ||
.anyRequest().permitAll(); | ||
} | ||
|
||
@Autowired | ||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { | ||
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/sec/project/controller/SignupController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package sec.project.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import sec.project.domain.Signup; | ||
import sec.project.repository.SignupRepository; | ||
|
||
@Controller | ||
public class SignupController { | ||
|
||
@Autowired | ||
private SignupRepository signupRepository; | ||
|
||
@RequestMapping("*") | ||
public String defaultMapping() { | ||
return "redirect:/form"; | ||
} | ||
|
||
@RequestMapping(value = "/form", method = RequestMethod.GET) | ||
public String loadForm() { | ||
return "form"; | ||
} | ||
|
||
@RequestMapping(value = "/form", method = RequestMethod.POST) | ||
public String submitForm(@RequestParam String name, @RequestParam String address) { | ||
signupRepository.save(new Signup(name, address)); | ||
return "done"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package sec.project.domain; | ||
|
||
import javax.persistence.Entity; | ||
import org.springframework.data.jpa.domain.AbstractPersistable; | ||
|
||
@Entity | ||
public class Signup extends AbstractPersistable<Long> { | ||
|
||
private String name; | ||
private String address; | ||
|
||
public Signup() { | ||
super(); | ||
} | ||
|
||
public Signup(String name, String address) { | ||
this(); | ||
this.name = name; | ||
this.address = address; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sec.project.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import sec.project.domain.Signup; | ||
|
||
public interface SignupRepository extends JpaRepository<Signup, Long> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<title>Thank you!</title> | ||
</head> | ||
<body> | ||
|
||
<h1>Thank you! You have been signed up to the event!</h1> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<title>Sign up form</title> | ||
</head> | ||
<body> | ||
|
||
<h1>Sign up to the event using this form</h1> | ||
|
||
<form action="#" th:action="@{/form}" method="POST"> | ||
<p><label for="name">Name</label>: <input type="text" name="name" id="name"/></p> | ||
<p><label for="address">Address</label>: <input type="text" name="address" id="address"/></p> | ||
<p><input type="submit" value="Submit" /></p> | ||
</form> | ||
|
||
<p></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package sec.project; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import sec.project.repository.SignupRepository; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class SampleTest { | ||
|
||
@Autowired | ||
private WebApplicationContext webAppContext; | ||
|
||
@Autowired | ||
private SignupRepository signupRepository; | ||
|
||
private MockMvc mockMvc; | ||
|
||
@Before | ||
public void setUp() { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); | ||
} | ||
|
||
@Test | ||
public void signupAddsDataToDatabase() throws Throwable { | ||
mockMvc.perform(post("/form").param("name", "Testname").param("address", "Testaddress")).andReturn(); | ||
assertEquals(1L, signupRepository.findAll().stream().filter(s -> s.getName().equals("Testname") && s.getAddress().equals("Testaddress")).count()); | ||
} | ||
} |