Java Code Examples for org.springframework.security.web.server.WebFilterExchange

The following examples show how to use org.springframework.security.web.server.WebFilterExchange. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: syncope   Source File: SessionRemovalServerLogoutHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> logout(final WebFilterExchange exchange, final Authentication authentication) {
    return exchange.getExchange().getSession().doOnNext(session -> {
        session.invalidate();
        EVENTS.debug("Invalidate session {}", (authentication == null) ? null : authentication.getPrincipal());
        cacheManager.getCache(SessionConfig.DEFAULT_CACHE).evictIfPresent(session.getId());
    }).flatMap(session -> Mono.empty());
}
 
Example 2
@Override
public Mono<Void> onLogoutSuccess(final WebFilterExchange exchange, final Authentication authentication) {
    return Mono.just(authentication).
            filter(OAuth2AuthenticationToken.class::isInstance).
            filter(token -> authentication.getPrincipal() instanceof OidcUser).
            map(OAuth2AuthenticationToken.class::cast).
            flatMap(this::endSessionEndpoint).
            map(endSessionEndpoint -> endpointUri(exchange, endSessionEndpoint, authentication)).
            switchIfEmpty(serverLogoutSuccessHandler.onLogoutSuccess(exchange, authentication).then(Mono.empty())).
            flatMap(endpointUri -> redirectStrategy.sendRedirect(exchange.getExchange(), endpointUri));
}
 
Example 3
private URI endpointUri(
        final WebFilterExchange exchange,
        final URI endSessionEndpoint,
        final Authentication authentication) {

    UriComponentsBuilder builder = UriComponentsBuilder.fromUri(endSessionEndpoint);
    builder.queryParam("id_token_hint", idToken(authentication));

    URI postLogout = globalPostLogout;
    String routeId = exchange.getExchange().getAttribute(ServerWebExchangeUtils.GATEWAY_PREDICATE_ROUTE_ATTR);
    if (StringUtils.isNotBlank(routeId)) {
        Optional<URI> routePostLogout = Optional.ofNullable(CACHE.get(routeId)).orElseGet(() -> {
            URI uri = null;
            Optional<SRARouteTO> route = routeProvider.getRouteTOs().stream().
                    filter(r -> routeId.equals(r.getKey())).findFirst();
            if (route.isPresent()) {
                uri = route.get().getPostLogout();
            }

            return CACHE.put(routeId, Optional.ofNullable(uri));
        });
        if (routePostLogout.isPresent()) {
            postLogout = routePostLogout.get();
        }
    }
    builder.queryParam("post_logout_redirect_uri", postLogout);

    return builder.encode(StandardCharsets.UTF_8).build().toUri();
}
 
Example 4
Source Project: open-cloud   Source File: ResourceServerConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
    // 自定义oauth2 认证, 使用redis读取token,而非jwt方式
    JsonAuthenticationEntryPoint entryPoint = new JsonAuthenticationEntryPoint(accessLogService);
    JsonAccessDeniedHandler accessDeniedHandler = new JsonAccessDeniedHandler(accessLogService);
    AccessManager accessManager = new AccessManager(apiresourceLocator, apiProperties);
    AuthenticationWebFilter oauth2 = new AuthenticationWebFilter(new RedisAuthenticationManager(new RedisTokenStore(redisConnectionFactory)));
    oauth2.setServerAuthenticationConverter(new ServerBearerTokenAuthenticationConverter());
    oauth2.setAuthenticationFailureHandler(new ServerAuthenticationEntryPointFailureHandler(entryPoint));
    oauth2.setAuthenticationSuccessHandler(new ServerAuthenticationSuccessHandler() {
        @Override
        public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
            ServerWebExchange exchange = webFilterExchange.getExchange();
            SecurityContextServerWebExchange securityContextServerWebExchange = new SecurityContextServerWebExchange(exchange, ReactiveSecurityContextHolder.getContext().subscriberContext(
                    ReactiveSecurityContextHolder.withAuthentication(authentication)
            ));
            return webFilterExchange.getChain().filter(securityContextServerWebExchange);
        }
    });
    http
            .httpBasic().disable()
            .csrf().disable()
            .authorizeExchange()
            .pathMatchers("/").permitAll()
            // 动态权限验证
            .anyExchange().access(accessManager)
            .and().exceptionHandling()
            .accessDeniedHandler(accessDeniedHandler)
            .authenticationEntryPoint(entryPoint).and()
            // 日志前置过滤器
            .addFilterAt(new PreRequestFilter(), SecurityWebFiltersOrder.FIRST)
            // 跨域过滤器
            .addFilterAt(corsFilter(), SecurityWebFiltersOrder.CORS)
            // 签名验证过滤器
            .addFilterAt(new PreSignatureFilter(baseAppServiceClient,apiProperties, new JsonSignatureDeniedHandler(accessLogService)), SecurityWebFiltersOrder.CSRF)
            // 访问验证前置过滤器
            .addFilterAt(new PreCheckFilter(accessManager, accessDeniedHandler), SecurityWebFiltersOrder.CSRF)
            // oauth2认证过滤器
            .addFilterAt(oauth2, SecurityWebFiltersOrder.AUTHENTICATION)
            // 日志过滤器
            .addFilterAt(new AccessLogFilter(accessLogService), SecurityWebFiltersOrder.SECURITY_CONTEXT_SERVER_WEB_EXCHANGE);
    return http.build();
}