我正在使用Spring Boot和Angular前端。有时候我想直接从一个服务中重定向。我们只使用.html,.css和脚本文件(没有.jsp)。
这是我尝试重定向到savepassword的代码:
return "static/pages/savepassword.html";
所以第一个问题是如何在这种情况下正确重定向?
接下来,Spring找不到我的html文件。我有这个结构:
我读到Spring应该自动在“static”中找到所有静态文件,但它没有。所以我尝试配置我的ViewControllerRegistry和RessourceControllerRegistry。
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
// registry.addResourceHandler("/stylesheets/**").addResourceLocations("/stylesheets/");
// registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
// registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/savePassword").setViewName("static/pages/savePassword.html");
registry.addViewController("/login").setViewName("static/pages/login.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
当我直接调用“static/pages/login.html”时,它可以正常工作。但是当我想要调用“/login”时,系统显示404错误。 第二个问题:我需要配置什么,不需要配置什么?
更新1:Spring Security
http
.authorizeRequests().antMatchers("/login", "/logout", "/user/**").permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling()
.accessDeniedHandler(new CustomAccessDeniedHandler())
.authenticationEntryPoint(new CustomAuthenticationEntryPoint())
.and()
.requiresChannel()
.anyRequest().requiresSecure()
.and()
.csrf()
.disable()
//.csrfTokenRepository(csrfTokenRepository())
.addFilterBefore(new MDCFilter(), ExceptionTranslationFilter.class)
// .addFilterBefore(new CorsFilter(),// ChannelProcessingFilter.class)
//.addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
.formLogin()
.successHandler(new CustomSuccessAuthenticationHandler())
.failureHandler(new CustomFailureHandler())
.loginPage("/login").permitAll();
更新-2:
我更新了我的结构,但是仍然无法将/login重定向到自定义登录页面。
更新-3:
我查看了Bob Shields的第二条评论:https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
我按照以下步骤进行操作:@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/savepassword").setViewName("savepassword.html");
registry.addViewController("/login").setViewName("login.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
我将 .html 设置为后缀名,因为否则 /login 将会导致递归问题。有了这个配置,我可以输入 /login,然后调用 /templates/login.html(保存密码同理)。
现在,我想知道如何在 html 中包含我的 css/js:
<link rel="stylesheet" type="text/css" href="..resources/stylesheets/bootstrap.min.css" />
<script src="..resources/scripts/angular.min.js"></script>
<script src="..resources/scripts/login.js"></script>
这个不起作用......但我可以直接通过/scripts/login.js调用我的脚本。
ViewControllerRegistry
会将login解析为static/pages/login.html,并采用这个登录页面。 - AndreasGloeckner