[英]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.