简体   繁体   English

spring-webflux url重定向和静态html和rest api路由

[英]spring-webflux url redirect and static html and rest api routing

I need the restful api under " /api/* " using authorization, all other routing like " / " and " /** " url goes to " /resources/static/index.html " without authorization. 我需要使用授权在“ /api/* ”下的静态api,所有其他路由(如“ / ”和“ /** ” url)都将未经授权转到“ /resources/static/index.html ”。 (the index.html used polymer to handle routing). (index.html使用聚合物来处理路由)。

Is it possible to achieve this? 有可能实现这一目标吗?

last edit: added ".httpBasic().and().formLogin().and()", it worked for "/api/**" authorization. 最后编辑:添加了“ .httpBasic()。and()。formLogin()。and()”,它适用于“ / api / **”授权。 but there's still problem while visiting " http://127.0.0.1:8080/template-one/gzgg " which I intent it would redirect to " http://127.0.0.1:8080 ". 但是访问“ http://127.0.0.1:8080/template-one/gzgg ”时仍然存在问题,我打算将其重定向到“ http://127.0.0.1:8080 ”。

I tried using 我尝试使用

@Bean
public MapReactiveUserDetailsService userDetailsRepository() {
    UserDetails admin = User.withDefaultPasswordEncoder().username("admin").password("123").roles("ADMIN").build();
    UserDetails santiago = User.withDefaultPasswordEncoder().username("santiago").password("456").roles("USER").build();
    return new MapReactiveUserDetailsService(admin,santiago);
}

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    return http
            .authorizeExchange()
            .pathMatchers(HttpMethod.GET, "/api/**").hasRole("ADMIN")
            .pathMatchers(HttpMethod.POST, "/api/**").hasRole("ADMIN")

            .pathMatchers(HttpMethod.GET, "/**").permitAll()
            .pathMatchers(HttpMethod.POST, "/**").permitAll()
            //.pathMatchers("/users/{user}/**").access(this::currentUserMatchesPath)
            .anyExchange().authenticated()
            .and()
            .httpBasic().and()
            .formLogin().and()
            .build();
}

and

@Configuration
public class GlobalRouter {

@Bean
RouterFunction<?> routerFunction() {
    RouterFunction router = resources("/**", new ClassPathResource("static/"));
    return router;

  }
}

and

@Component
public class CustomWebFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    if (exchange.getRequest().getURI().getPath().equals("/")) {
        return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().path("/index.html").build()).build());
    }

    return chain.filter(exchange);
}
}

It's OK to visit "resources/static/index.html", but while visiting " http://localhost:8080/api/person/all ", I enter "admin"/"123" for authrization, but authrization failed. 可以访问“ resources / static / index.html”,但是在访问“ http:// localhost:8080 / api / person / all ”时,我输入“ admin” /“ 123”进行身份验证,但是身份验证失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @EnableResourceServer不适用于spring-webflux - @EnableResourceServer not working with spring-webflux spring-webflux 应用启动失败 - Spring-webflux application startup failures 如何将@RestController 添加到 spring-webflux 应用程序? - How to add @RestController to spring-webflux apps? 使用参数重定向到 Spring MVC (REST API) 中的 URL - Redirect to URL in Spring MVC (REST API) with params 如何在没有spring-boot的情况下在spring-webflux中加载配置? - how to load config in spring-webflux without spring-boot? 使用打开的 api 代码生成器为 spring-webflux 生成 API 接口时,如何防止 Mono 请求正文生成? - How to prevent Mono Request body generation when generating the API interfaces for spring-webflux with open api code generator? 使用spring-webflux时WebTestClient不起作用 - WebTestClient does not work when using spring-webflux 如何在 spring-webflux WebTestClient 测试中验证异常? - How to validate Exception in spring-webflux WebTestClient tests? 没有上下文根路径的spring-webflux安全中的白名单端点 - Whitelist endpoint in spring-webflux security without context root path Lombok 的@RequiredArgsConstructor 能否初始化一个Spring-Webflux WebClient? - Can Lombok's @RequiredArgsConstructor initialize a Spring-Webflux WebClient?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM