Merge pull request #10393 from sampada07/JAVA-4011

JAVA-4011: Split or move spring-5-security module
This commit is contained in:
Loredana Crusoveanu
2021-01-06 11:04:02 +02:00
committed by GitHub
8 changed files with 2 additions and 1 deletions
@@ -8,6 +8,8 @@ This module contains articles about Spring Boot Security
- [Spring Security for Spring Boot Integration Tests](https://www.baeldung.com/spring-security-integration-tests)
- [Introduction to Spring Security Taglibs](https://www.baeldung.com/spring-security-taglibs)
- [Guide to @CurrentSecurityContext in Spring Security](https://www.baeldung.com/spring-currentsecuritycontext)
- [Disable Security for a Profile in Spring Boot](https://www.baeldung.com/spring-security-disable-profile)
### Spring Boot Security Auto-Configuration
@@ -0,0 +1,14 @@
package com.baeldung.securityprofile;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@SpringBootApplication
@EnableWebSecurity
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.securityprofile;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@Profile("test")
public class ApplicationNoSecurity extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/**");
}
}
@@ -0,0 +1,16 @@
package com.baeldung.securityprofile;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@Profile("prod")
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated();
}
}
@@ -0,0 +1,16 @@
package com.baeldung.securityprofile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.List;
@RestController
public class EmployeeController {
@GetMapping("/employees")
public List<String> getEmployees() {
return Collections.singletonList("Adam Johnson");
}
}
@@ -0,0 +1,28 @@
package com.baeldung.securityprofile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(value = EmployeeController.class)
@ActiveProfiles("test")
public class EmployeeControllerNoSecurityUnitTest {
@Autowired
private MockMvc mockMvc;
@Test
public void whenSecurityDisabled_shouldBeOk() throws Exception {
this.mockMvc.perform(get("/employees"))
.andExpect(status().isOk());
}
}
@@ -0,0 +1,28 @@
package com.baeldung.securityprofile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(value = EmployeeController.class)
@ActiveProfiles("prod")
public class EmployeeControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@Test
public void whenSecurityEnabled_shouldBeForbidden() throws Exception {
this.mockMvc.perform(get("/employees"))
.andExpect(status().isForbidden());
}
}