mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 01:02:14 +00:00
NamespaceGlobalMethodSecurityTests groovy->java
Issue: gh-4939
This commit is contained in:
parent
9587f3280e
commit
2c519b7e74
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -13,24 +13,27 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.security.config.annotation.method.configuration
|
package org.springframework.security.config.annotation.method.configuration;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
import org.springframework.security.access.AccessDeniedException
|
|
||||||
import org.springframework.security.access.PermissionEvaluator;
|
import org.springframework.security.access.PermissionEvaluator;
|
||||||
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
|
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
|
||||||
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler
|
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
|
||||||
import org.springframework.security.authentication.AuthenticationManager
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||||
import org.springframework.security.authentication.TestingAuthenticationToken
|
|
||||||
import org.springframework.security.config.annotation.BaseSpringSpec
|
|
||||||
import org.springframework.security.config.annotation.authentication.AuthenticationManagerBuilder
|
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
import org.springframework.security.config.test.SpringTestRule;
|
||||||
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
|
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Demonstrate the samples
|
* Demonstrate the samples
|
||||||
@ -38,31 +41,35 @@ import org.springframework.security.core.context.SecurityContextHolder
|
|||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SampleEnableGlobalMethodSecurityTests extends BaseSpringSpec {
|
public class SampleEnableGlobalMethodSecurityTests {
|
||||||
def setup() {
|
@Rule
|
||||||
|
public final SpringTestRule spring = new SpringTestRule();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MethodSecurityService methodSecurityService;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
SecurityContextHolder.getContext().setAuthentication(
|
SecurityContextHolder.getContext().setAuthentication(
|
||||||
new TestingAuthenticationToken("user", "password","ROLE_USER"))
|
new TestingAuthenticationToken("user", "password", "ROLE_USER"));
|
||||||
}
|
}
|
||||||
|
|
||||||
def preAuthorize() {
|
@Test
|
||||||
when:
|
public void preAuthorize() {
|
||||||
loadConfig(SampleWebSecurityConfig)
|
this.spring.register(SampleWebSecurityConfig.class).autowire();
|
||||||
MethodSecurityService service = context.getBean(MethodSecurityService)
|
|
||||||
then:
|
|
||||||
service.secured() == null
|
|
||||||
service.jsr250() == null
|
|
||||||
|
|
||||||
when:
|
assertThat(this.methodSecurityService.secured()).isNull();
|
||||||
service.preAuthorize()
|
assertThat(this.methodSecurityService.jsr250()).isNull();
|
||||||
then:
|
|
||||||
thrown(AccessDeniedException)
|
assertThatThrownBy(() -> this.methodSecurityService.preAuthorize())
|
||||||
|
.isInstanceOf(AccessDeniedException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled=true)
|
@EnableGlobalMethodSecurity(prePostEnabled=true)
|
||||||
public static class SampleWebSecurityConfig {
|
static class SampleWebSecurityConfig {
|
||||||
@Bean
|
@Bean
|
||||||
public MethodSecurityService methodSecurityService() {
|
public MethodSecurityService methodSecurityService() {
|
||||||
return new MethodSecurityServiceImpl()
|
return new MethodSecurityServiceImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -74,24 +81,23 @@ public class SampleEnableGlobalMethodSecurityTests extends BaseSpringSpec {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'custom permission handler'() {
|
|
||||||
when:
|
|
||||||
loadConfig(CustomPermissionEvaluatorWebSecurityConfig)
|
|
||||||
MethodSecurityService service = context.getBean(MethodSecurityService)
|
|
||||||
then:
|
|
||||||
service.hasPermission("allowed") == null
|
|
||||||
|
|
||||||
when:
|
@Test
|
||||||
service.hasPermission("denied") == null
|
public void customPermissionHandler() {
|
||||||
then:
|
this.spring.register(CustomPermissionEvaluatorWebSecurityConfig.class).autowire();
|
||||||
thrown(AccessDeniedException)
|
|
||||||
|
assertThat(this.methodSecurityService.hasPermission("allowed")).isNull();
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> this.methodSecurityService.hasPermission("denied"))
|
||||||
|
.isInstanceOf(AccessDeniedException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled=true)
|
@EnableGlobalMethodSecurity(prePostEnabled=true)
|
||||||
public static class CustomPermissionEvaluatorWebSecurityConfig extends GlobalMethodSecurityConfiguration {
|
public static class CustomPermissionEvaluatorWebSecurityConfig extends GlobalMethodSecurityConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
public MethodSecurityService methodSecurityService() {
|
public MethodSecurityService methodSecurityService() {
|
||||||
return new MethodSecurityServiceImpl()
|
return new MethodSecurityServiceImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
Loading…
x
Reference in New Issue
Block a user