【发布时间】:2022-01-25 07:20:16
【问题描述】:
我遇到了一个问题,我在 React 上写了一个页面来重置密码,当我只运行 React 应用程序并单击链接 127.0.0.1:8080/user/resetPassword/53442 时,一切都显示良好。一旦我与 Spring Boot 捆绑运行,就会出现 whitelabel error page type = 404。在 Spring 日志中我得到了这个:
GET "/ user / resetPassword / afsa11", parameters = {}
Mapped to `ResourceHttpRequestHandler [URL [file: C: / Users / Jake Morgan / IdeaProjects / react-invasion / build / index.html]]
Resource not found
这个补丁我没有写任何控制器,也没有使用模型视图,因为那里不需要,我通过axios输入表单后请求另一个点。
很可能我错过了一些东西并忘记了一些设置。
也许doFilter?
我的App.js - 反应:
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css'
import Header from './Components/Header';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Home from './Pages/Home';
import PasswordResetPage from './Pages/PasswordResetPage';
import Test from './Components/Test';
function App() {
return (
<>
<div className="App">
<Router>
<Switch>
<Route path="/vasua/" component={Test}/>
<Route path="/user/resetPassword/:id" component={PasswordResetPage} />
<Route exact path="/" component={Home} />
</Switch>
</Router>
</div>
<Header />
</>
);
}
export default App;
我的MvcSecurityConfig:
@Value("${path.frontend}")
private String frontendPath;
@Value("${frontendStaticResourcesPathPatterns}")
private String[] frontendStaticResourcesPathPatterns;
private static final String BASE_API_PATH = "/";
public void addResourceHandlers(ResourceHandlerRegistry registry){
String pathToFrontend = "file:" + this.frontendPath;
String pathToIndexHTML = pathToFrontend + "index.html";
registry
.addResourceHandler(frontendStaticResourcesPathPatterns)
.setCachePeriod(0)
.addResourceLocations(pathToFrontend);
registry.addResourceHandler("/", "/**")
.setCachePeriod(0)
.addResourceLocations(pathToIndexHTML)
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
if (resourcePath.startsWith(BASE_API_PATH) || resourcePath.startsWith(BASE_API_PATH.substring(1))) {
return null;
}
return location.exists() && location.isReadable() ? location : null;
}
});
}
我的网络安全配置:
@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsServiceImpl userDetailsService;
@Autowired
private AuthEntryPointJwt unauthorizedHandler;
@Bean
public AuthTokenFilter authenticationJwtTokenFilter() {
return new AuthTokenFilter();
}
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(
"/user/forgotPassword",
"/user/resetPassword",
"/user/resetPassword/*",
"/user/resetPassword/**/",
"/api/auth/*",
"/api/auth/**").permitAll()
.antMatchers(
"/",
"/favicon.ico",
"/static/**",
"/manifest.json",
"/logo192.png",
"/index.html").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().disable();
http
.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
}
文件夹:
C:/Users/Jake Morgan/IdeaProjects/react-invasion/build/
| asset-manifest.json
| favicon.ico
| index.html
| logo192.png
| logo512.png
| manifest.json
| robots.txt
| tree.txt
|
\---static
+---css
| 2.818011ed.chunk.css
| 2.818011ed.chunk.css.map
| main.6745c9b9.chunk.css
| main.6745c9b9.chunk.css.map
|
+---js
| 2.d6a0ce6c.chunk.js
| 2.d6a0ce6c.chunk.js.LICENSE.txt
| 2.d6a0ce6c.chunk.js.map
| 3.c6fb18dc.chunk.js
| 3.c6fb18dc.chunk.js.map
| main.a7a27014.chunk.js
| main.a7a27014.chunk.js.map
| runtime-main.197b0595.js
| runtime-main.197b0595.js.map
|
\---media
Invasion-Project.0dba6a23.exe
mov.41f2b828.mp4
start.e3a0d02b.png
x.85f9f2a1.svg
Application.properties
#Frontend
path.frontend = C:/Users/Jake Morgan/IdeaProjects/react-invasion/build/
frontendStaticResourcesPathPatterns=/**/*.css,/**/*.html,/**/*.js,/**/*.jsx,/**/*.png,/**/*.ttf,/**/*.woff,/**/*.woff2,/**/*.ico,/**/*.jpg,/**/*.mp4,/**/*.svg,manifest.json
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${path.frontend}
更新
我在我的 React.js 中添加了 HashRouter 而不是 BrowserRouter,现在页面打开了,但是在这个地址:
import { HashRouter as Router, Switch, Route } from "react-router-dom";
127.0.0.1:8080/#/user/resetPassword/412 这个#号是什么,可以去掉吗?
【问题讨论】:
-
为什么要自定义 jwtfilter?请阅读spring security文档中jwt章节如何正确实现对jwts的处理,spring security从2018年开始就有jwtsupport
-
这个功能没用,问题不一样
-
@ДжейкМорган 这个文件
C: / Users / Jake Morgan / IdeaProjects / react-invasion / build / index.html存在吗?您已将 URL 映射到此文件。 -
"127.0.0.1:8080/" 可以正常打开,问题出在页面上
-
日志显示文件不在构建文件夹中,index.html 在哪里。请更新您的问题,以显示所有项目和资源的放置位置的完整文件夹结构。显然,您混淆了事物的加载方式。
标签: reactjs spring-boot spring-mvc spring-security react-router