Java 使用Spring引导控制器响应路由
我有一个后端Spring启动应用程序,它通过FormLogin身份验证启用了Spring安全性。下面是我的安全配置Java 使用Spring引导控制器响应路由,java,reactjs,spring-boot,spring-security,Java,Reactjs,Spring Boot,Spring Security,我有一个后端Spring启动应用程序,它通过FormLogin身份验证启用了Spring安全性。下面是我的安全配置 @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled=true) public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint());
http.authorizeRequests().antMatchers("/index.html").permitAll().antMatchers("/j_security_check").permitAll().antMatchers("/api/**").authenticated();
http.csrf().disable();
http.formLogin().usernameParameter("j_username")
.passwordParameter("j_password").loginPage("/index.html").loginProcessingUrl("/j_security_check")
.defaultSuccessUrl("/index.html", true).permitAll().failureUrl("/index.html?error=true");
http.logout().logoutUrl("/dashboard/logout").invalidateHttpSession(true).clearAuthentication(true)
.logoutSuccessUrl("/index.html").deleteCookies("JSESSIONID");
}
}
我的视图控件类如下所示:
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/")
.setViewName("forward:/index.html");
registry.addViewController("/home")
.setViewName("forward:/index.html");
}
@Override
public void addResourceHandlers(
ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("/WEB-INF/view/react/build/static/");
registry.addResourceHandler("/*.*")
.addResourceLocations("/WEB-INF/view/react/build/");
registry.addResourceHandler("/index.html")
.addResourceLocations("/WEB-INF/view/react/build/index.html");
}
}
index.html由react应用程序上的“npm运行构建”生成。生成的构建文件夹被移动到Spring boot项目的静态资源中。我在react应用程序中配置了/login、/home等路由。在/login组件中成功登录后,将使用以下命令将用户定向到/home页:
this.props.history.push("/home");
启动Spring boot后,我可以访问index.html中的登录页面,单击login后,我将被重定向到/home,并能够在一秒钟内看到home组件,然后再次被重定向到localhost:8080/?。我不确定它如何重定向localhost:8080/ 你成功了吗?还没成功。我最终在WLS.OK上的一个单独的节点服务器和Spring引导项目中部署了React。我是新来的。如何在“独立节点服务器”中运行?我在开发时在我的cmd行中运行它,但我不想在生产中这样做…你们是怎么做到的?你们应该使用webpack服务器在生产中进行部署。您可以参考以获取有关如何使用它的更多信息。