Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11: Added initial configuration of Spring Security #14

Merged
merged 2 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,30 @@ public String home() {
return "index";
}

@GetMapping("/login")
public String login() { return "login"; }
@GetMapping("/about")
String about() {
return "index";
}

@GetMapping("/logout")
public String logout() { return "logout"; }
String logout() {
return "logout";
}

@GetMapping("/login")
String login() {
return "login";
}

@GetMapping("/secret")
String secret() {
return "index";
}

@GetMapping("/privacy-policy")
public String privacyPolicy() { return "privacyPolicy"; }

@GetMapping("/register")
public String register() { return "register"; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ppijerman.ppijidentitybackend.server.controller;

import org.springframework.context.annotation.Configuration;
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;

@Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.cors()
.and().authorizeRequests()
.antMatchers(
"/",
"/privacy-policy",
"/about"
).permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").permitAll()
.and().logout().logoutUrl("/logout").invalidateHttpSession(true).clearAuthentication(true).permitAll();
}
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
datasource:
url: jdbc:postgresql://$DATABASE_HOST:$DATABASE_PORT/$DATABASE_NAME
username: $DATABASE_USERNAME
password: $DATABASE_PASSWORD
driver-class-name: org.postgresql.Driver
session:
jdbc:
initialize-schema: always
10 changes: 10 additions & 0 deletions src/main/resources/templates/logout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

</body>
</html>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.ppijerman.ppijidentitybackend.server.controller;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest
@AutoConfigureMockMvc
public class WebControllerTest {
@Autowired
private MockMvc mvc;

@BeforeEach
public void SetUp() {

}

@TestFactory
public Collection<DynamicTest> testPublicUrlAccessible() {
return Stream.of(
"/",
"/privacy-policy",
"/about"
).map(url -> DynamicTest.dynamicTest("Test access " + url, () -> testUrlAccessible(url)))
.collect(Collectors.toUnmodifiableList());
}

private void testUrlAccessible(String url) throws Exception {
mvc.perform(get(url))
.andExpect(status().is2xxSuccessful());
}
}