mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-26 22:02:41 +00:00
Add hasAnyAuthority method in AuthorizePayloadsSpec.Access
See Fixes gh-7437 Co-authored-by: Eddú Meléndez <eddu.melendez@gmail.com>
This commit is contained in:
parent
3425db6d16
commit
3a66191756
@ -104,6 +104,7 @@ import java.util.List;
|
|||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
|
* @author Ebert Toribio
|
||||||
* @since 5.2
|
* @since 5.2
|
||||||
*/
|
*/
|
||||||
public class RSocketSecurity {
|
public class RSocketSecurity {
|
||||||
@ -320,6 +321,10 @@ public class RSocketSecurity {
|
|||||||
.just(new AuthorizationDecision(true)));
|
.just(new AuthorizationDecision(true)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AuthorizePayloadsSpec hasAnyAuthority(String... authorities) {
|
||||||
|
return access(AuthorityReactiveAuthorizationManager.hasAnyAuthority(authorities));
|
||||||
|
}
|
||||||
|
|
||||||
public AuthorizePayloadsSpec access(
|
public AuthorizePayloadsSpec access(
|
||||||
ReactiveAuthorizationManager<PayloadExchangeAuthorizationContext> authorization) {
|
ReactiveAuthorizationManager<PayloadExchangeAuthorizationContext> authorization) {
|
||||||
AuthorizePayloadsSpec.this.authzBuilder.add(new PayloadExchangeMatcherEntry<>(this.matcher, authorization));
|
AuthorizePayloadsSpec.this.authzBuilder.add(new PayloadExchangeMatcherEntry<>(this.matcher, authorization));
|
||||||
|
@ -51,6 +51,7 @@ import static org.assertj.core.api.Assertions.assertThatCode;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
|
* @author Ebert Toribio
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration
|
@ContextConfiguration
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ -167,6 +168,23 @@ public class RSocketMessageHandlerConnectionITests {
|
|||||||
// .isInstanceOf(RejectedSetupException.class);
|
// .isInstanceOf(RejectedSetupException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectWithAnyAuthority() {
|
||||||
|
UsernamePasswordMetadata credentials =
|
||||||
|
new UsernamePasswordMetadata("ebert", "ebert");
|
||||||
|
this.requester = requester()
|
||||||
|
.setupMetadata(credentials, UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE)
|
||||||
|
.connectTcp(this.server.address().getHostName(), this.server.address().getPort())
|
||||||
|
.block();
|
||||||
|
|
||||||
|
String hiEbert = this.requester.route("management.users")
|
||||||
|
.data("ebert")
|
||||||
|
.retrieveMono(String.class)
|
||||||
|
.block();
|
||||||
|
|
||||||
|
assertThat(hiEbert).isEqualTo("Hi ebert");
|
||||||
|
}
|
||||||
|
|
||||||
private RSocketRequester.Builder requester() {
|
private RSocketRequester.Builder requester() {
|
||||||
return RSocketRequester.builder()
|
return RSocketRequester.builder()
|
||||||
.rsocketStrategies(this.handler.getRSocketStrategies());
|
.rsocketStrategies(this.handler.getRSocketStrategies());
|
||||||
@ -208,13 +226,18 @@ public class RSocketMessageHandlerConnectionITests {
|
|||||||
.password("password")
|
.password("password")
|
||||||
.roles("USER", "SETUP")
|
.roles("USER", "SETUP")
|
||||||
.build();
|
.build();
|
||||||
|
UserDetails manager = User.withDefaultPasswordEncoder()
|
||||||
|
.username("ebert")
|
||||||
|
.password("ebert")
|
||||||
|
.roles("SETUP", "MANAGER")
|
||||||
|
.build();
|
||||||
|
|
||||||
UserDetails evil = User.withDefaultPasswordEncoder()
|
UserDetails evil = User.withDefaultPasswordEncoder()
|
||||||
.username("evil")
|
.username("evil")
|
||||||
.password("password")
|
.password("password")
|
||||||
.roles("EVIL")
|
.roles("EVIL")
|
||||||
.build();
|
.build();
|
||||||
return new MapReactiveUserDetailsService(admin, user, evil);
|
return new MapReactiveUserDetailsService(admin, user, manager, evil);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -225,6 +248,7 @@ public class RSocketMessageHandlerConnectionITests {
|
|||||||
.setup().hasRole("SETUP")
|
.setup().hasRole("SETUP")
|
||||||
.route("secure.admin.*").hasRole("ADMIN")
|
.route("secure.admin.*").hasRole("ADMIN")
|
||||||
.route("secure.**").hasRole("USER")
|
.route("secure.**").hasRole("USER")
|
||||||
|
.route("management.*").hasAnyAuthority("ROLE_MANAGER")
|
||||||
.anyRequest().permitAll()
|
.anyRequest().permitAll()
|
||||||
)
|
)
|
||||||
.basicAuthentication(Customizer.withDefaults());
|
.basicAuthentication(Customizer.withDefaults());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user