【发布时间】:2021-03-01 04:39:29
【问题描述】:
当我在浏览器中输入我的 React 应用程序的任何路由时,例如:http://localhost/login,请求到达我的服务器,我的服务器以 401 Unauthorized 响应。
当请求不是授权的后端 api 时,我想在我的反应路由中处理请求。
WebSecurityConfig.java:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
...
.formLogin()
.disable()
.authorizeRequests()
.antMatchers(
"/error",
"/",
"/favicon.ico",
"/static/**",
"/api/auth/**",
"/api/oauth2/**",
"/api/courses/**",
"/api/stripe/**",
"/api/lesson/content")
.permitAll()
.anyRequest()
.authenticated()
.and()
...
.exceptionHandling()
.authenticationEntryPoint(new RestAuthenticationEntryPoint())
.and();
http.addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
RestAuthenticationEntryPoint.java:
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
AuthenticationException e) throws IOException, ServletException {
httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED,
e.getLocalizedMessage());
}
}
有没有办法将请求转发到 RestAuthenticationEntryPoint 中的 index.html?
【问题讨论】:
-
When request is not an authorized backend api什么意思? -
当它不匹配任何 antMatchers 并转到 RestAuthenticationEntryPoint.java。
-
为什么你不只是在
/**上提供你的 react 应用程序,然后在路由器中通过向 /user 发出请求或其他什么来检查他们是否已登录,如果他们没有登录,你会重定向他们登录。到达/api的每个请求都会返回 401
标签: java spring spring-boot spring-security single-page-application