没有 Thymeleaf 的 Spring Boot 安全性

Dan*_*ase 6 spring-mvc spring-security spring-boot

我想使用 Spring Security 保护 Spring Boot 中“/static/secure”中的所有页面,但我不想使用视图。

我创建了一个登录表单,方法=“POST”,并设置了基于 Java 的配置类以成功转到 /static/secure/main.html。

我希望我可以从特定问题的角度进行处理,不幸的是,我可以找到有关如何进行身份验证的指导的所有内容都使用 Thymeleaf 或视图。我真的只是希望它在成功登录后导航到 /static/secure/main.html,而不是调用视图。有谁知道如何配置 Spring Boot 以在身份验证时转到常规 HTML 页面?

我试图只使用 Spring Security 来处理登录,然后一旦我们进入,应用程序的其余部分将是 Angular,根据需要点击 REST API 端点 - 所以我真的不想要“视图”概念,也不想要 Thymeleaf混合。

任何帮助将非常感激!

谢谢你,丹

Ale*_*hko 2

成功的身份验证将重定向到用户最初尝试打开的页面。因此,只要您保护/static/secure文件夹的安全,访问/static/secure/main.html就会触发身份验证,然后在成功时自动重定向到该页面。

您的整体配置将如下所示:

@Configuration
@EnableWebMvc
public class SpringMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("/static/");
    }
}
Run Code Online (Sandbox Code Playgroud)

(假定您通过 URL 提供 Angular 页面,/static/...并且页面本身来自/src/webapp/staticMaven 项目中的文件夹。)

在您的 Spring Security 配置中:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/static/secure/**").authenticated()
        .and()
        // ...
}
Run Code Online (Sandbox Code Playgroud)