Skip to content

Commit 2ad2d4d

Browse files
linhvovnpivovarit
authored andcommitted
[tlinh2110@gmail.com]Bael1313 Create spring-acl module (eugenp#3131)
* [tlinh2110@gmail.com] [BAEL1313] Introduction Spring ACL * [tlinh2110@gmail.com] BAEL 1313 - Update unit test * [tlinh2110@gmail.com]BAEL1313- remove unused files from spring-core * [BAEL1313] Create spring-acl module * [BAEL1313] Add resources for Spring ACL * [BAEL1313] Remove acl from spring-security-mvc-boot * [BAEL1313] Add Spring ACL module in parent pom.xml
1 parent f84dbbb commit 2ad2d4d

11 files changed

Lines changed: 454 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<module>spark-java</module>
142142
<!-- <module>spring-5</module>-->
143143
<module>spring-5-mvc</module>
144+
<module>spring-acl</module>
144145
<module>spring-activiti</module>
145146
<module>spring-akka</module>
146147
<module>spring-amqp</module>

‎spring-acl/pom.xml‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-acl</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
11+
<name>spring-acl</name>
12+
<description>Spring ACL</description>
13+
14+
<parent>
15+
<artifactId>parent-boot-5</artifactId>
16+
<groupId>com.baeldung</groupId>
17+
<version>0.0.1-SNAPSHOT</version>
18+
<relativePath>../parent-boot-5</relativePath>
19+
</parent>
20+
21+
<dependencies>
22+
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-jpa</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>com.h2database</groupId>
30+
<artifactId>h2</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework</groupId>
35+
<artifactId>spring-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.security</groupId>
41+
<artifactId>spring-security-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.security</groupId>
47+
<artifactId>spring-security-acl</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.security</groupId>
51+
<artifactId>spring-security-config</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework</groupId>
55+
<artifactId>spring-context-support</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>net.sf.ehcache</groupId>
59+
<artifactId>ehcache-core</artifactId>
60+
<version>2.6.11</version>
61+
<type>jar</type>
62+
</dependency>
63+
64+
</dependencies>
65+
66+
</project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.baeldung.acl.config;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7+
import org.springframework.cache.ehcache.EhCacheFactoryBean;
8+
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
12+
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
13+
import org.springframework.security.acls.AclPermissionCacheOptimizer;
14+
import org.springframework.security.acls.AclPermissionEvaluator;
15+
import org.springframework.security.acls.domain.AclAuthorizationStrategy;
16+
import org.springframework.security.acls.domain.AclAuthorizationStrategyImpl;
17+
import org.springframework.security.acls.domain.ConsoleAuditLogger;
18+
import org.springframework.security.acls.domain.DefaultPermissionGrantingStrategy;
19+
import org.springframework.security.acls.domain.EhCacheBasedAclCache;
20+
import org.springframework.security.acls.jdbc.BasicLookupStrategy;
21+
import org.springframework.security.acls.jdbc.JdbcMutableAclService;
22+
import org.springframework.security.acls.jdbc.LookupStrategy;
23+
import org.springframework.security.acls.model.PermissionGrantingStrategy;
24+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
25+
26+
@Configuration
27+
@EnableAutoConfiguration
28+
public class ACLContext {
29+
30+
@Autowired
31+
DataSource dataSource;
32+
33+
@Bean
34+
public EhCacheBasedAclCache aclCache() {
35+
return new EhCacheBasedAclCache(aclEhCacheFactoryBean().getObject(), permissionGrantingStrategy(), aclAuthorizationStrategy());
36+
}
37+
38+
@Bean
39+
public EhCacheFactoryBean aclEhCacheFactoryBean() {
40+
EhCacheFactoryBean ehCacheFactoryBean = new EhCacheFactoryBean();
41+
ehCacheFactoryBean.setCacheManager(aclCacheManager().getObject());
42+
ehCacheFactoryBean.setCacheName("aclCache");
43+
return ehCacheFactoryBean;
44+
}
45+
46+
@Bean
47+
public EhCacheManagerFactoryBean aclCacheManager() {
48+
return new EhCacheManagerFactoryBean();
49+
}
50+
51+
@Bean
52+
public PermissionGrantingStrategy permissionGrantingStrategy() {
53+
return new DefaultPermissionGrantingStrategy(new ConsoleAuditLogger());
54+
}
55+
56+
@Bean
57+
public AclAuthorizationStrategy aclAuthorizationStrategy() {
58+
return new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_ADMIN"));
59+
}
60+
61+
@Bean
62+
public MethodSecurityExpressionHandler defaultMethodSecurityExpressionHandler() {
63+
DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
64+
AclPermissionEvaluator permissionEvaluator = new AclPermissionEvaluator(aclService());
65+
expressionHandler.setPermissionEvaluator(permissionEvaluator);
66+
expressionHandler.setPermissionCacheOptimizer(new AclPermissionCacheOptimizer(aclService()));
67+
return expressionHandler;
68+
}
69+
70+
@Bean
71+
public LookupStrategy lookupStrategy() {
72+
return new BasicLookupStrategy(dataSource, aclCache(), aclAuthorizationStrategy(), new ConsoleAuditLogger());
73+
}
74+
75+
@Bean
76+
public JdbcMutableAclService aclService() {
77+
return new JdbcMutableAclService(dataSource, lookupStrategy(), aclCache());
78+
}
79+
80+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.acl.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
6+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
7+
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
8+
9+
@Configuration
10+
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
11+
public class AclMethodSecurityConfiguration extends GlobalMethodSecurityConfiguration {
12+
13+
@Autowired
14+
MethodSecurityExpressionHandler defaultMethodSecurityExpressionHandler;
15+
16+
@Override
17+
protected MethodSecurityExpressionHandler createExpressionHandler() {
18+
return defaultMethodSecurityExpressionHandler;
19+
}
20+
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.acl.config;
2+
3+
import org.springframework.boot.autoconfigure.domain.EntityScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.PropertySource;
6+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
7+
import org.springframework.transaction.annotation.EnableTransactionManagement;
8+
9+
@Configuration
10+
@EnableTransactionManagement
11+
@EnableJpaRepositories(basePackages = "org.baeldung.acl.persistence.dao")
12+
@PropertySource("classpath:org.baeldung.acl.datasource.properties")
13+
@EntityScan(basePackages={ "org.baeldung.acl.persistence.entity" })
14+
public class JPAPersistenceConfig {
15+
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.acl.persistence.dao;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.acl.persistence.entity.NoticeMessage;
6+
import org.springframework.data.jpa.repository.JpaRepository;
7+
import org.springframework.data.repository.query.Param;
8+
import org.springframework.security.access.prepost.PostAuthorize;
9+
import org.springframework.security.access.prepost.PostFilter;
10+
import org.springframework.security.access.prepost.PreAuthorize;
11+
12+
public interface NoticeMessageRepository extends JpaRepository<NoticeMessage, Long>{
13+
14+
@PostFilter("hasPermission(filterObject, 'READ')")
15+
List<NoticeMessage> findAll();
16+
17+
@PostAuthorize("hasPermission(returnObject, 'READ')")
18+
NoticeMessage findById(Integer id);
19+
20+
@SuppressWarnings("unchecked")
21+
@PreAuthorize("hasPermission(#noticeMessage, 'WRITE')")
22+
NoticeMessage save(@Param("noticeMessage")NoticeMessage noticeMessage);
23+
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.acl.persistence.entity;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
import javax.persistence.Table;
7+
8+
@Entity
9+
@Table(name="system_message")
10+
public class NoticeMessage {
11+
12+
@Id
13+
@Column
14+
private Integer id;
15+
@Column
16+
private String content;
17+
public Integer getId() {
18+
return id;
19+
}
20+
public void setId(Integer id) {
21+
this.id = id;
22+
}
23+
public String getContent() {
24+
return content;
25+
}
26+
public void setContent(String content) {
27+
this.content = content;
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
INSERT INTO system_message(id,content) VALUES (1,'First Level Message');
2+
INSERT INTO system_message(id,content) VALUES (2,'Second Level Message');
3+
INSERT INTO system_message(id,content) VALUES (3,'Third Level Message');
4+
5+
INSERT INTO acl_class (id, class) VALUES
6+
(1, 'org.baeldung.acl.persistence.entity.NoticeMessage');
7+
8+
INSERT INTO acl_sid (id, principal, sid) VALUES
9+
(1, 1, 'manager'),
10+
(2, 1, 'hr'),
11+
(3, 1, 'admin'),
12+
(4, 0, 'ROLE_EDITOR');
13+
14+
INSERT INTO acl_object_identity (id, object_id_class, object_id_identity, parent_object, owner_sid, entries_inheriting) VALUES
15+
(1, 1, 1, NULL, 3, 0),
16+
(2, 1, 2, NULL, 3, 0),
17+
(3, 1, 3, NULL, 3, 0)
18+
;
19+
20+
INSERT INTO acl_entry (id, acl_object_identity, ace_order, sid, mask, granting, audit_success, audit_failure) VALUES
21+
(1, 1, 1, 1, 1, 1, 1, 1),
22+
(2, 1, 2, 1, 2, 1, 1, 1),
23+
(3, 1, 3, 4, 1, 1, 1, 1),
24+
(4, 2, 1, 2, 1, 1, 1, 1),
25+
(5, 2, 2, 4, 1, 1, 1, 1),
26+
(6, 3, 1, 4, 1, 1, 1, 1),
27+
(7, 3, 2, 4, 2, 1, 1, 1)
28+
;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
create table system_message (id integer not null, content varchar(255), primary key (id));
2+
3+
CREATE TABLE IF NOT EXISTS acl_sid (
4+
id bigint(20) NOT NULL AUTO_INCREMENT,
5+
principal tinyint(1) NOT NULL,
6+
sid varchar(100) NOT NULL,
7+
PRIMARY KEY (id),
8+
UNIQUE KEY unique_uk_1 (sid,principal)
9+
);
10+
11+
CREATE TABLE IF NOT EXISTS acl_class (
12+
id bigint(20) NOT NULL AUTO_INCREMENT,
13+
class varchar(255) NOT NULL,
14+
PRIMARY KEY (id),
15+
UNIQUE KEY unique_uk_2 (class)
16+
);
17+
18+
CREATE TABLE IF NOT EXISTS acl_entry (
19+
id bigint(20) NOT NULL AUTO_INCREMENT,
20+
acl_object_identity bigint(20) NOT NULL,
21+
ace_order int(11) NOT NULL,
22+
sid bigint(20) NOT NULL,
23+
mask int(11) NOT NULL,
24+
granting tinyint(1) NOT NULL,
25+
audit_success tinyint(1) NOT NULL,
26+
audit_failure tinyint(1) NOT NULL,
27+
PRIMARY KEY (id),
28+
UNIQUE KEY unique_uk_4 (acl_object_identity,ace_order)
29+
);
30+
31+
CREATE TABLE IF NOT EXISTS acl_object_identity (
32+
id bigint(20) NOT NULL AUTO_INCREMENT,
33+
object_id_class bigint(20) NOT NULL,
34+
object_id_identity bigint(20) NOT NULL,
35+
parent_object bigint(20) DEFAULT NULL,
36+
owner_sid bigint(20) DEFAULT NULL,
37+
entries_inheriting tinyint(1) NOT NULL,
38+
PRIMARY KEY (id),
39+
UNIQUE KEY unique_uk_3 (object_id_class,object_id_identity)
40+
);
41+
42+
ALTER TABLE acl_entry
43+
ADD FOREIGN KEY (acl_object_identity) REFERENCES acl_object_identity(id);
44+
45+
ALTER TABLE acl_entry
46+
ADD FOREIGN KEY (sid) REFERENCES acl_sid(id);
47+
48+
--
49+
-- Constraints for table acl_object_identity
50+
--
51+
ALTER TABLE acl_object_identity
52+
ADD FOREIGN KEY (parent_object) REFERENCES acl_object_identity (id);
53+
54+
ALTER TABLE acl_object_identity
55+
ADD FOREIGN KEY (object_id_class) REFERENCES acl_class (id);
56+
57+
ALTER TABLE acl_object_identity
58+
ADD FOREIGN KEY (owner_sid) REFERENCES acl_sid (id);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
2+
spring.datasource.username=sa
3+
spring.datasource.password=
4+
spring.datasource.driverClassName=org.h2.Driver
5+
spring.jpa.hibernate.ddl-auto=update
6+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
7+
8+
spring.h2.console.path=/myconsole
9+
spring.h2.console.enabled=true
10+
spring.datasource.initialize=true
11+
spring.datasource.schema=classpath:acl-schema.sql
12+
spring.datasource.data=classpath:acl-data.sql

0 commit comments

Comments
 (0)