Skip to content

Commit 8892b7b

Browse files
committed
custom expression handler
1 parent c879a9d commit 8892b7b

26 files changed

+1543
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
29+
</attributes>
30+
</classpathentry>
31+
<classpathentry kind="output" path="target/classes"/>
32+
</classpath>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-security-custom-permission</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.m2e.core.maven2Builder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
<buildCommand>
34+
<name>org.eclipse.wst.validation.validationbuilder</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
38+
</buildSpec>
39+
<natures>
40+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
41+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
42+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
43+
<nature>org.eclipse.jdt.core.javanature</nature>
44+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
45+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
46+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
47+
</natures>
48+
</projectDescription>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>spring-security-custom-permission</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>war</packaging>
9+
10+
<name>spring-security-custom-permission</name>
11+
<description>Spring Security custom permission</description>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>1.3.3.RELEASE</version>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-security</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-tomcat</artifactId>
32+
<scope>provided</scope>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.thymeleaf.extras</groupId>
42+
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
43+
</dependency>
44+
45+
<!-- -->
46+
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-data-jpa</artifactId>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>com.h2database</groupId>
54+
<artifactId>h2</artifactId>
55+
</dependency>
56+
57+
<!-- -->
58+
59+
<dependency>
60+
<groupId>junit</groupId>
61+
<artifactId>junit</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>org.hamcrest</groupId>
67+
<artifactId>hamcrest-core</artifactId>
68+
<scope>test</scope>
69+
</dependency>
70+
71+
<dependency>
72+
<groupId>org.hamcrest</groupId>
73+
<artifactId>hamcrest-library</artifactId>
74+
<scope>test</scope>
75+
</dependency>
76+
77+
<dependency>
78+
<groupId>com.jayway.restassured</groupId>
79+
<artifactId>rest-assured</artifactId>
80+
<version>${rest-assured.version}</version>
81+
<scope>test</scope>
82+
<exclusions>
83+
<exclusion>
84+
<artifactId>commons-logging</artifactId>
85+
<groupId>commons-logging</groupId>
86+
</exclusion>
87+
</exclusions>
88+
</dependency>
89+
90+
</dependencies>
91+
92+
<build>
93+
<plugins>
94+
<plugin>
95+
<groupId>org.springframework.boot</groupId>
96+
<artifactId>spring-boot-maven-plugin</artifactId>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
101+
<properties>
102+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
103+
<java.version>1.8</java.version>
104+
<rest-assured.version>2.4.0</rest-assured.version>
105+
</properties>
106+
107+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.config;
2+
3+
import org.baeldung.security.CustomMethodSecurityExpressionHandler;
4+
import org.baeldung.security.CustomPermissionEvaluator;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
7+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
8+
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
9+
10+
@Configuration
11+
@EnableGlobalMethodSecurity(prePostEnabled = true)
12+
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
13+
14+
@Override
15+
protected MethodSecurityExpressionHandler createExpressionHandler() {
16+
// final DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
17+
final CustomMethodSecurityExpressionHandler expressionHandler = new CustomMethodSecurityExpressionHandler();
18+
expressionHandler.setPermissionEvaluator(new CustomPermissionEvaluator());
19+
return expressionHandler;
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
6+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
9+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
10+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
11+
12+
@Configuration
13+
@EnableWebMvc
14+
public class MvcConfig extends WebMvcConfigurerAdapter {
15+
16+
public MvcConfig() {
17+
super();
18+
}
19+
20+
//
21+
@Bean
22+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
23+
return new PropertySourcesPlaceholderConfigurer();
24+
}
25+
26+
@Override
27+
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
28+
configurer.enable();
29+
}
30+
31+
@Override
32+
public void addViewControllers(final ViewControllerRegistry registry) {
33+
super.addViewControllers(registry);
34+
registry.addViewController("/").setViewName("forward:/index");
35+
registry.addViewController("/index");
36+
}
37+
38+
@Override
39+
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
40+
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.baeldung.config;
2+
3+
import org.baeldung.security.MyUserDetailsService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
8+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
11+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12+
13+
@Configuration
14+
@EnableWebSecurity
15+
@ComponentScan("org.baeldung.security")
16+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
17+
18+
@Autowired
19+
private MyUserDetailsService userDetailsService;
20+
21+
@Override
22+
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
23+
auth.userDetailsService(userDetailsService);
24+
}
25+
26+
@Override
27+
public void configure(WebSecurity web) throws Exception {
28+
web.ignoring().antMatchers("/resources/**");
29+
}
30+
31+
@Override
32+
protected void configure(final HttpSecurity http) throws Exception {
33+
// @formatter:off
34+
http.authorizeRequests()
35+
.antMatchers("/login").permitAll()
36+
.antMatchers("/admin").hasRole("ADMIN")
37+
.anyRequest().authenticated()
38+
.and().formLogin().permitAll()
39+
.and().csrf().disable();
40+
;
41+
// @formatter:on
42+
}
43+
}

0 commit comments

Comments
 (0)