【发布时间】:2016-06-15 11:26:29
【问题描述】:
我在前端使用弹簧靴和角度。有时我想直接从服务重定向。我们只使用 .html、.css 和脚本文件(没有 .jsp)。
这是return "static/pages/savepassword.html"; 我尝试重定向到保存密码的方式
那么第一个问题在这种情况下如何正确重定向?
接下来,Spring 找不到我的 html 文件。我有这个结构
我读到 spring 应该自动在“静态”中找到所有静态文件,但它没有。所以我尝试配置我的 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。 第二个问题:我需要配置什么,哪些不需要?
UPDATE-1:Spring 安全性
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();
UPDATE-2 我更新了结构,但从 /login 重定向到自定义登录仍然存在问题。
UPDATE-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 将被调用(savepassword.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 调用我的脚本。
【问题讨论】:
-
你可以试试 /login.html 吗?
-
不,因为默认的 Spring Security 页面出现了。通常,我们的自定义 login.html (static/pages/login.html) 应该会显示出来。我更新了我的答案,所以你可以看到 spring 安全配置。我认为
ViewControllerRegistry将登录解析为 static/pages/login,html 并会使用此登录页面 -
我可能需要将静态文件夹放在 src/main/ressource 中而不是放在 webapp 中吗?
-
您可以尝试将一页直接放在 webapp 文件夹下并使用 /login.html 引用它吗?
-
我停用了spring security,但是直接将html文件放在“资源”中时无法访问。
标签: java html spring spring-mvc spring-boot