add example for security with profiles (#9185)

Co-authored-by: Mihai Lepadat <mihai.lepadat@irian.at>
This commit is contained in:
Mihai238
2020-06-09 21:49:03 +02:00
committed by GitHub
parent c387fe5dd6
commit 26c11b3e85
6 changed files with 119 additions and 0 deletions
@@ -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");
}
}