remove security (#1765)

This commit is contained in:
lor6 2017-05-01 19:35:13 +03:00 committed by Grzegorz Piwowarek
parent 4aff9d33d9
commit 196b869fff
6 changed files with 24 additions and 85 deletions

View File

@ -41,11 +41,6 @@
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId> <artifactId>spring-boot-starter-tomcat</artifactId>
@ -143,6 +138,10 @@
<version>${togglz.version}</version> <version>${togglz.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-server</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,7 +1,7 @@
package com.baeldung.toggle; package com.baeldung.toggle;
import org.togglz.core.Feature; import org.togglz.core.Feature;
import org.togglz.core.activation.UserRoleActivationStrategy; import org.togglz.core.activation.SystemPropertyActivationStrategy;
import org.togglz.core.annotation.ActivationParameter; import org.togglz.core.annotation.ActivationParameter;
import org.togglz.core.annotation.DefaultActivationStrategy; import org.togglz.core.annotation.DefaultActivationStrategy;
import org.togglz.core.annotation.EnabledByDefault; import org.togglz.core.annotation.EnabledByDefault;
@ -10,14 +10,13 @@ import org.togglz.core.context.FeatureContext;
public enum MyFeatures implements Feature { public enum MyFeatures implements Feature {
@Label("Administrator Feature") @Label("Employee Management Feature") @EnabledByDefault @DefaultActivationStrategy(id = SystemPropertyActivationStrategy.ID,
@EnabledByDefault parameters = { @ActivationParameter(name = SystemPropertyActivationStrategy.PARAM_PROPERTY_NAME, value = "employee.feature"),
@DefaultActivationStrategy(id = UserRoleActivationStrategy.ID, parameters = { @ActivationParameter(name = UserRoleActivationStrategy.PARAM_ROLES_NAME, value = "ROLE_ADMIN") }) @ActivationParameter(name = SystemPropertyActivationStrategy.PARAM_PROPERTY_VALUE, value = "true") })
ADMIN_FEATURE; EMPLOYEE_MANAGEMENT_FEATURE;
public boolean isActive() { public boolean isActive() {
return FeatureContext.getFeatureManager() return FeatureContext.getFeatureManager().isActive(this);
.isActive(this);
} }
} }

View File

@ -9,7 +9,7 @@ public class SalaryService {
@Autowired @Autowired
EmployeeRepository employeeRepository; EmployeeRepository employeeRepository;
@FeatureAssociation(value = MyFeatures.ADMIN_FEATURE) @FeatureAssociation(value = MyFeatures.EMPLOYEE_MANAGEMENT_FEATURE)
public void increaseSalary(long id) { public void increaseSalary(long id) {
Employee employee = employeeRepository.findOne(id); Employee employee = employeeRepository.findOne(id);
employee.setSalary(employee.getSalary() + employee.getSalary() * 0.1); employee.setSalary(employee.getSalary() + employee.getSalary() * 0.1);

View File

@ -1,33 +0,0 @@
package com.baeldung.toggle;
import org.springframework.beans.factory.annotation.Autowired;
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;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//@formatter:off
auth.inMemoryAuthentication()
.withUser("user").password("pass").roles("USER")
.and()
.withUser("admin").password("pass").roles("ADMIN");
//@formatter:on
}
@Override
public void configure(HttpSecurity http) throws Exception {
//@formatter:off
http.authorizeRequests().antMatchers("/increaseSalary").permitAll()
.and()
.csrf().disable()
.httpBasic();
//@formatter:on
}
}

View File

@ -2,14 +2,10 @@ package com.baeldung.toggle;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.togglz.core.manager.EnumBasedFeatureProvider; import org.togglz.core.manager.EnumBasedFeatureProvider;
import org.togglz.core.spi.FeatureProvider; import org.togglz.core.spi.FeatureProvider;
import org.togglz.core.user.UserProvider;
import org.togglz.spring.security.SpringSecurityUserProvider;
@Configuration @Configuration
@EnableJpaRepositories("com.baeldung.toggle") @EnableJpaRepositories("com.baeldung.toggle")
@ -21,8 +17,4 @@ public class ToggleConfiguration {
return new EnumBasedFeatureProvider(MyFeatures.class); return new EnumBasedFeatureProvider(MyFeatures.class);
} }
@Bean
public UserProvider userProvider() {
return new SpringSecurityUserProvider("admin");
}
} }

View File

@ -11,14 +11,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ToggleApplication.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ToggleApplication.class)
@AutoConfigureMockMvc @AutoConfigureMockMvc
@ -31,54 +28,39 @@ public class ToggleIntegrationTest {
EmployeeRepository employeeRepository; EmployeeRepository employeeRepository;
@Autowired @Autowired
private MockMvc mvc; private MockMvc mockMvc;
@Autowired @Autowired
private WebApplicationContext wac; private WebApplicationContext wac;
@Autowired
private FilterChainProxy springSecurityFilterChain;
@Before @Before
public void setup() { public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.wac) this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
.addFilter(springSecurityFilterChain)
.build();
} }
@Test @Test
public void givenNoAuthentication_whenIncreaseSalary_thenNoIncrease() throws Exception { public void givenFeaturePropertyFalse_whenIncreaseSalary_thenNoIncrease() throws Exception {
Employee emp = new Employee(1, 2000); Employee emp = new Employee(1, 2000);
employeeRepository.save(emp); employeeRepository.save(emp);
mvc.perform(post("/increaseSalary").param("id", emp.getId() + ""))
.andExpect(status().is(200)); System.setProperty("employee.feature", "false");
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200));
emp = employeeRepository.findOne(1L); emp = employeeRepository.findOne(1L);
assertEquals("salary incorrect", 2000, emp.getSalary(), 0.5); assertEquals("salary incorrect", 2000, emp.getSalary(), 0.5);
} }
@Test @Test
public void givenAdminAuthentication_whenIncreaseSalary_thenIncrease() throws Exception { public void givenFeaturePropertyTrue_whenIncreaseSalary_thenIncrease() throws Exception {
Employee emp = new Employee(1, 2000); Employee emp = new Employee(1, 2000);
employeeRepository.save(emp); employeeRepository.save(emp);
mvc.perform(post("/increaseSalary").param("id", emp.getId() + "")
.with(httpBasic("admin", "pass"))) System.setProperty("employee.feature", "true");
.andExpect(status().is(200));
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200));
emp = employeeRepository.findOne(1L); emp = employeeRepository.findOne(1L);
assertEquals("salary incorrect", 2200, emp.getSalary(), 0.5); assertEquals("salary incorrect", 2200, emp.getSalary(), 0.5);
} }
@Test
public void givenUserAuthentication_whenIncreaseSalary_thenNoIncrease() throws Exception {
Employee emp = new Employee(1, 2000);
employeeRepository.save(emp);
mvc.perform(post("/increaseSalary").param("id", emp.getId() + "")
.with(httpBasic("user", "pass")))
.andExpect(status().is(200));
emp = employeeRepository.findOne(1L);
assertEquals("salary incorrect", 2000, emp.getSalary(), 0.5);
}
} }