BAEL-3338: A Guide to AuthenticationManagerResolver in Spring Security

Fix indentation problems in code, do some renaming to sync article with code
This commit is contained in:
maryarm
2019-12-02 16:33:37 +02:00
parent 07461f418a
commit 41a8ea19d3
5 changed files with 53 additions and 38 deletions
@@ -11,15 +11,15 @@ public class AuthResolverController {
@GetMapping("/customer/welcome")
public Mono<String> sayWelcomeToCustomer(Mono<Principal> principal) {
return principal
.map(Principal::getName)
.map(name -> String.format("Welcome to our site, %s!", name));
.map(Principal::getName)
.map(name -> String.format("Welcome to our site, %s!", name));
}
@GetMapping("/employee/welcome")
public Mono<String> sayWelcomeToEmployee(Mono<Principal> principal) {
return principal
.map(Principal::getName)
.map(name -> String.format("Welcome to our company, %s!", name));
.map(Principal::getName)
.map(name -> String.format("Welcome to our company, %s!", name));
}
}
@@ -19,7 +19,7 @@ import reactor.core.publisher.Mono;
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class AuthResolverSecurityConfig {
public class CustomWebSecurityConfig {
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
@@ -35,17 +35,17 @@ public class AuthResolverSecurityConfig {
}
public AuthenticationWebFilter authenticationWebFilter() {
AuthenticationWebFilter filter = new AuthenticationWebFilter(authenticationManagerResolver());
return filter;
return new AuthenticationWebFilter(resolver());
}
public ReactiveAuthenticationManagerResolver<ServerHttpRequest> authenticationManagerResolver() {
public ReactiveAuthenticationManagerResolver<ServerHttpRequest> resolver() {
return request -> {
if (request
.getPath()
.subPath(0)
.value()
.startsWith("/employee")) return Mono.just(employeesAuthenticationManager());
.startsWith("/employee"))
return Mono.just(employeesAuthenticationManager());
return Mono.just(customersAuthenticationManager());
};
}
@@ -55,7 +55,11 @@ public class AuthResolverSecurityConfig {
.switchIfEmpty(Mono.error(new UsernameNotFoundException(authentication
.getPrincipal()
.toString())))
.map(b -> new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))));
.map(b -> new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
authentication.getCredentials(),
Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))
)
);
}
public ReactiveAuthenticationManager employeesAuthenticationManager() {
@@ -63,7 +67,12 @@ public class AuthResolverSecurityConfig {
.switchIfEmpty(Mono.error(new UsernameNotFoundException(authentication
.getPrincipal()
.toString())))
.map(b -> new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))));
.map(
b -> new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
authentication.getCredentials(),
Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))
)
);
}
public Mono<String> customer(Authentication authentication) {
@@ -14,7 +14,8 @@ import org.springframework.util.Base64Utils;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AuthResolverApplication.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class AuthResolverIntegrationTest {
@Autowired private WebTestClient testClient;
@Autowired
private WebTestClient testClient;
@Test
public void givenCustomerCredential_whenWelcomeCustomer_thenExpectOk() {