Skip to content

Commit

Permalink
Introduce spring security
Browse files Browse the repository at this point in the history
  • Loading branch information
Pozo committed Jul 15, 2024
1 parent b3bb437 commit eedaf65
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
<artifactId>jedis</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.pozo.investmentfunds.api;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
.requestMatchers(HttpMethod.GET, "/").permitAll()
.requestMatchers(HttpMethod.GET, "/funds").permitAll()
.requestMatchers(HttpMethod.GET, "/funds/*").permitAll()
.requestMatchers(HttpMethod.GET, "/rates/*").permitAll()
.requestMatchers(HttpMethod.POST, "/rates/*").permitAll()
.requestMatchers(HttpMethod.POST, "/sheets/rates/*").permitAll()
.anyRequest().denyAll()
).csrf(AbstractHttpConfigurer::disable); // https://stackoverflow.com/a/51088555
return http.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.pozo.investmentfunds.api.funds

import com.github.pozo.investmentfunds.api.SecurityConfiguration
import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.context.annotation.Import
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
Expand All @@ -14,6 +16,7 @@ import java.util.*


@WebMvcTest(FundsController::class)
@Import(SecurityConfiguration::class) // https://stackoverflow.com/questions/45116833/springboot-webmvctest-security-issue
internal class FundsControllerTest(@Autowired val mockMvc: MockMvc) {

@MockkBean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.pozo.investmentfunds.api.sheets

import com.github.pozo.investmentfunds.api.SecurityConfiguration
import com.github.pozo.investmentfunds.api.rates.Rate
import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
Expand All @@ -8,12 +9,14 @@ import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.context.annotation.Import
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

@WebMvcTest(SheetsController::class)
@Import(SecurityConfiguration::class) // https://stackoverflow.com/questions/45116833/springboot-webmvctest-security-issue
class SheetsControllerTest(@Autowired val mockMvc: MockMvc) {

@MockkBean
Expand Down

0 comments on commit eedaf65

Please sign in to comment.