Skip to content

Commit 0735c03

Browse files
lor6pivovarit
authored andcommitted
togglz aspect (eugenp#1743)
* togglz aspect * formatting * trigger travis build
1 parent b9484b2 commit 0735c03

12 files changed

Lines changed: 325 additions & 0 deletions

File tree

‎spring-boot/pom.xml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<groupId>org.springframework.boot</groupId>
4141
<artifactId>spring-boot-starter-security</artifactId>
4242
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.security</groupId>
46+
<artifactId>spring-security-test</artifactId>
47+
</dependency>
4348

4449
<dependency>
4550
<groupId>org.springframework.boot</groupId>
@@ -125,6 +130,18 @@
125130
<artifactId>mysql-connector-java</artifactId>
126131
<version>6.0.6</version>
127132
</dependency>
133+
134+
<dependency>
135+
<groupId>org.togglz</groupId>
136+
<artifactId>togglz-spring-boot-starter</artifactId>
137+
<version>${togglz.version}</version>
138+
</dependency>
139+
140+
<dependency>
141+
<groupId>org.togglz</groupId>
142+
<artifactId>togglz-spring-security</artifactId>
143+
<version>${togglz.version}</version>
144+
</dependency>
128145

129146
</dependencies>
130147

@@ -262,6 +279,7 @@
262279
<subethasmtp.version>3.1.7</subethasmtp.version>
263280
<tomee-servlet-api.version>8.5.11</tomee-servlet-api.version>
264281
<h2.version>1.4.194</h2.version>
282+
<togglz.version>2.4.1.Final</togglz.version>
265283
</properties>
266284

267285
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.toggle;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class Employee {
8+
9+
@Id
10+
private long id;
11+
private double salary;
12+
13+
public Employee() {
14+
}
15+
16+
public Employee(long id, double salary) {
17+
this.id = id;
18+
this.salary = salary;
19+
}
20+
21+
public long getId() {
22+
return id;
23+
}
24+
25+
public void setId(long id) {
26+
this.id = id;
27+
}
28+
29+
public double getSalary() {
30+
return salary;
31+
}
32+
33+
public void setSalary(double salary) {
34+
this.salary = salary;
35+
}
36+
37+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.toggle;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface EmployeeRepository extends CrudRepository<Employee, Long>{
6+
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.toggle;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target({ ElementType.METHOD, ElementType.TYPE })
10+
public @interface FeatureAssociation {
11+
MyFeatures value();
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.toggle;
2+
3+
import org.apache.log4j.Logger;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.Around;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.springframework.stereotype.Component;
8+
9+
@Aspect
10+
@Component
11+
public class FeaturesAspect {
12+
13+
private static final Logger LOG = Logger.getLogger(FeaturesAspect.class);
14+
15+
@Around(value = "@within(featureAssociation) || @annotation(featureAssociation)")
16+
public Object checkAspect(ProceedingJoinPoint joinPoint, FeatureAssociation featureAssociation) throws Throwable {
17+
if (featureAssociation.value()
18+
.isActive()) {
19+
return joinPoint.proceed();
20+
} else {
21+
LOG.info("Feature " + featureAssociation.value()
22+
.name() + " is not enabled!");
23+
return null;
24+
}
25+
}
26+
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.toggle;
2+
3+
import org.togglz.core.Feature;
4+
import org.togglz.core.activation.UserRoleActivationStrategy;
5+
import org.togglz.core.annotation.ActivationParameter;
6+
import org.togglz.core.annotation.DefaultActivationStrategy;
7+
import org.togglz.core.annotation.EnabledByDefault;
8+
import org.togglz.core.annotation.Label;
9+
import org.togglz.core.context.FeatureContext;
10+
11+
public enum MyFeatures implements Feature {
12+
13+
@Label("Administrator Feature")
14+
@EnabledByDefault
15+
@DefaultActivationStrategy(id = UserRoleActivationStrategy.ID, parameters = { @ActivationParameter(name = UserRoleActivationStrategy.PARAM_ROLES_NAME, value = "ROLE_ADMIN") })
16+
ADMIN_FEATURE;
17+
18+
public boolean isActive() {
19+
return FeatureContext.getFeatureManager()
20+
.isActive(this);
21+
}
22+
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.toggle;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
9+
@Controller
10+
public class SalaryController {
11+
12+
@Autowired
13+
SalaryService salaryService;
14+
15+
@PostMapping(value = "/increaseSalary")
16+
@ResponseBody
17+
public void increaseSalary(@RequestParam long id) {
18+
salaryService.increaseSalary(id);
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.toggle;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class SalaryService {
8+
9+
@Autowired
10+
EmployeeRepository employeeRepository;
11+
12+
@FeatureAssociation(value = MyFeatures.ADMIN_FEATURE)
13+
public void increaseSalary(long id) {
14+
Employee employee = employeeRepository.findOne(id);
15+
employee.setSalary(employee.getSalary() + employee.getSalary() * 0.1);
16+
employeeRepository.save(employee);
17+
}
18+
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.toggle;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
10+
@Configuration
11+
@EnableWebSecurity
12+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
13+
14+
@Autowired
15+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
16+
//@formatter:off
17+
auth.inMemoryAuthentication()
18+
.withUser("user").password("pass").roles("USER")
19+
.and()
20+
.withUser("admin").password("pass").roles("ADMIN");
21+
//@formatter:on
22+
}
23+
24+
@Override
25+
public void configure(HttpSecurity http) throws Exception {
26+
//@formatter:off
27+
http.authorizeRequests().antMatchers("/increaseSalary").permitAll()
28+
.and()
29+
.csrf().disable()
30+
.httpBasic();
31+
//@formatter:on
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.toggle;
2+
3+
import javax.annotation.security.RolesAllowed;
4+
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
import com.baeldung.autoconfiguration.MySQLAutoconfiguration;
9+
10+
@SpringBootApplication(exclude = MySQLAutoconfiguration.class)
11+
public class ToggleApplication {
12+
@RolesAllowed("*")
13+
public static void main(String[] args) {
14+
System.setProperty("security.basic.enabled", "false");
15+
SpringApplication.run(ToggleApplication.class, args);
16+
}
17+
}

0 commit comments

Comments
 (0)