Skip to content

Commit 3b20cf2

Browse files
author
Ambrus Adrián-Zoltán
committed
Created an example about custom AccessDecisionVoters in Spring Security.
1 parent 09b7c6e commit 3b20cf2

9 files changed

Lines changed: 305 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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>org.baeldung</groupId>
7+
<artifactId>spring-security-custom-voter</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
11+
<name>spring-security-custom-voter</name>
12+
<description>Custom AccessDecisionVoter with Spring Security</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.4.1.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-security</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
</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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.baeldung.security;
2+
3+
import org.springframework.security.access.AccessDecisionVoter;
4+
import org.springframework.security.access.ConfigAttribute;
5+
import org.springframework.security.core.Authentication;
6+
import org.springframework.security.core.GrantedAuthority;
7+
8+
import java.time.LocalDateTime;
9+
import java.util.Collection;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
13+
public class MinuteBasedVoter implements AccessDecisionVoter {
14+
@Override
15+
public boolean supports(ConfigAttribute attribute) {
16+
return true;
17+
}
18+
19+
@Override
20+
public boolean supports(Class clazz) {
21+
return true;
22+
}
23+
24+
@Override
25+
public int vote(Authentication authentication, Object object, Collection collection) {
26+
List<String> roles = authentication
27+
.getAuthorities()
28+
.stream().map(GrantedAuthority::getAuthority)
29+
.collect(Collectors.toList());
30+
31+
for (String role: roles) {
32+
if ("ROLE_USER".equals(role) && LocalDateTime.now().getMinute() % 2 != 0) {
33+
return ACCESS_DENIED;
34+
}
35+
}
36+
return ACCESS_ABSTAIN;
37+
}
38+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.baeldung.security;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.access.AccessDecisionManager;
7+
import org.springframework.security.access.AccessDecisionVoter;
8+
import org.springframework.security.access.vote.AuthenticatedVoter;
9+
import org.springframework.security.access.vote.RoleVoter;
10+
import org.springframework.security.access.vote.UnanimousBased;
11+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
13+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
14+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
15+
import org.springframework.security.web.access.expression.WebExpressionVoter;
16+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
@Configuration
22+
@EnableWebSecurity
23+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
24+
@Autowired
25+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
26+
// @formatter: off
27+
auth.inMemoryAuthentication()
28+
.withUser("user").password("pass").roles("USER")
29+
.and()
30+
.withUser("admin").password("pass").roles("ADMIN");
31+
// @formatter: on
32+
}
33+
34+
@Override
35+
protected void configure(HttpSecurity http) throws Exception {
36+
// @formatter: off
37+
http
38+
// needed so our login could work
39+
.csrf()
40+
.disable()
41+
.authorizeRequests()
42+
.anyRequest()
43+
.authenticated()
44+
.accessDecisionManager(accessDecisionManager())
45+
.antMatchers("/").hasAnyRole("ROLE_ADMIN", "ROLE_USER")
46+
.and()
47+
.formLogin()
48+
.permitAll()
49+
.and()
50+
.logout()
51+
.permitAll()
52+
.deleteCookies("JSESSIONID")
53+
.logoutSuccessUrl("/login");
54+
// @formatter: on
55+
}
56+
57+
@Bean
58+
public AccessDecisionManager accessDecisionManager() {
59+
// @formatter: off
60+
List<AccessDecisionVoter<? extends Object>> decisionVoters =
61+
Arrays.asList(
62+
new WebExpressionVoter(),
63+
new RoleVoter(),
64+
new AuthenticatedVoter(),
65+
new MinuteBasedVoter());
66+
// @formatter: on
67+
return new UnanimousBased(decisionVoters);
68+
}
69+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.security;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.ImportResource;
5+
6+
/**
7+
* Created by ambrusadrianz on 09/10/2016.
8+
*/
9+
@Configuration
10+
//@ImportResource({"classpath:spring-security.xml"})
11+
public class XmlSecurityConfig {
12+
public XmlSecurityConfig() {
13+
super();
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.web;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
5+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
/**
9+
* Created by ambrusadrianz on 30/09/2016.
10+
*/
11+
12+
@Configuration
13+
public class MvcConfig extends WebMvcConfigurerAdapter {
14+
@Override
15+
public void addViewControllers(ViewControllerRegistry registry) {
16+
registry.addViewController("/").setViewName("private");
17+
}
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans:beans xmlns="http://www.springframework.org/schema/security"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:beans="http://www.springframework.org/schema/beans"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/security
7+
http://www.springframework.org/schema/security/spring-security-4.1.xsd
8+
http://www.springframework.org/schema/beans
9+
http://www.springframework.org/schema/beans/spring-beans.xsd">
10+
11+
<http use-expressions="true" auto-config="true" access-decision-manager-ref="accessDecisionManager">
12+
<intercept-url pattern="/**" access="hasAnyRole('ROLE_ADMIN', 'ROLE_USER')"/>
13+
14+
<form-login default-target-url="/"/>
15+
<csrf disabled="true"/>
16+
<logout logout-url="/logout" delete-cookies="JSESSIONID" logout-success-url="/login"/>
17+
</http>
18+
19+
<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased">
20+
<beans:constructor-arg>
21+
<beans:list>
22+
<beans:bean class="org.springframework.security.web.access.expression.WebExpressionVoter"/>
23+
<beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
24+
<beans:bean class="org.springframework.security.access.vote.RoleVoter"/>
25+
<beans:bean class="com.example.security.MinuteBasedVoter"/>
26+
</beans:list>
27+
</beans:constructor-arg>
28+
</beans:bean>
29+
30+
<beans:bean id="minuteBasedVoter" class="com.example.security.MinuteBasedVoter"/>
31+
32+
<authentication-manager>
33+
<authentication-provider>
34+
<user-service>
35+
<user name="user" password="pass" authorities="ROLE_USER"/>
36+
<user name="admin" password="pass" authorities="ROLE_ADMIN"/>
37+
</user-service>
38+
</authentication-provider>
39+
</authentication-manager>
40+
</beans:beans>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
3+
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
4+
<head>
5+
<title>Private</title>
6+
</head>
7+
<body>
8+
<h1>Congrats!</h1>
9+
</body>
10+
</html>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
5+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
6+
version="3.1">
7+
8+
<display-name>Spring Secured Application</display-name>
9+
10+
<!-- Spring MVC -->
11+
<servlet>
12+
<servlet-name>mvc</servlet-name>
13+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
14+
<load-on-startup>1</load-on-startup>
15+
</servlet>
16+
<servlet-mapping>
17+
<servlet-name>mvc</servlet-name>
18+
<url-pattern>/</url-pattern>
19+
</servlet-mapping>
20+
21+
<context-param>
22+
<param-name>contextClass</param-name>
23+
<param-value>
24+
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
25+
</param-value>
26+
</context-param>
27+
<context-param>
28+
<param-name>contextConfigLocation</param-name>
29+
<param-value>org.baeldung.spring.web.config</param-value>
30+
</context-param>
31+
<listener>
32+
<listener-class>
33+
org.springframework.web.context.ContextLoaderListener
34+
</listener-class>
35+
</listener>
36+
37+
<!-- Spring Security -->
38+
<filter>
39+
<filter-name>springSecurityFilterChain</filter-name>
40+
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
41+
</filter>
42+
<filter-mapping>
43+
<filter-name>springSecurityFilterChain</filter-name>
44+
<url-pattern>/*</url-pattern>
45+
</filter-mapping>
46+
47+
</web-app>

0 commit comments

Comments
 (0)