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

View File

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

View File

@ -9,7 +9,7 @@ public class SalaryService {
@Autowired
EmployeeRepository employeeRepository;
@FeatureAssociation(value = MyFeatures.ADMIN_FEATURE)
@FeatureAssociation(value = MyFeatures.EMPLOYEE_MANAGEMENT_FEATURE)
public void increaseSalary(long id) {
Employee employee = employeeRepository.findOne(id);
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.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.togglz.core.manager.EnumBasedFeatureProvider;
import org.togglz.core.spi.FeatureProvider;
import org.togglz.core.user.UserProvider;
import org.togglz.spring.security.SpringSecurityUserProvider;
@Configuration
@EnableJpaRepositories("com.baeldung.toggle")
@ -21,8 +17,4 @@ public class ToggleConfiguration {
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.context.SpringBootTest;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ToggleApplication.class)
@AutoConfigureMockMvc
@ -31,54 +28,39 @@ public class ToggleIntegrationTest {
EmployeeRepository employeeRepository;
@Autowired
private MockMvc mvc;
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Autowired
private FilterChainProxy springSecurityFilterChain;
@Before
public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.wac)
.addFilter(springSecurityFilterChain)
.build();
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void givenNoAuthentication_whenIncreaseSalary_thenNoIncrease() throws Exception {
public void givenFeaturePropertyFalse_whenIncreaseSalary_thenNoIncrease() throws Exception {
Employee emp = new Employee(1, 2000);
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);
assertEquals("salary incorrect", 2000, emp.getSalary(), 0.5);
}
@Test
public void givenAdminAuthentication_whenIncreaseSalary_thenIncrease() throws Exception {
public void givenFeaturePropertyTrue_whenIncreaseSalary_thenIncrease() throws Exception {
Employee emp = new Employee(1, 2000);
employeeRepository.save(emp);
mvc.perform(post("/increaseSalary").param("id", emp.getId() + "")
.with(httpBasic("admin", "pass")))
.andExpect(status().is(200));
System.setProperty("employee.feature", "true");
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200));
emp = employeeRepository.findOne(1L);
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);
}
}