首页 > 解决方案 > 使用 Spring Boot 安全性时未显示初始登录页面

问题描述

我尝试将 Spring Boot 安全依赖项添加到我的博客系统中。在添加 Spring Boot 安全性之前,会像我的代码一样显示初始视图。但是当我像下面添加spring boot security的配置文件时,

@Configuration
@EnableWebSecurity
public class BlogWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // TODO Auto-generated method stub
        super.configure(auth);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // TODO Auto-generated method stub
        super.configure(http);
    }

}

显示了一些有线初始登录视图。

在此处输入图像描述

如何删除这个初始登录视图,以便显示我的初始 view.html?是否有任何选项可以删除此登录视图?

标签: spring-bootspring-security

解决方案


我相信这是弹簧安全的默认设置。configure您应该根据需要修改覆盖的方法。

您可以阅读本教程,了解如何使用自定义配置实现 Spring Security。

例如:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
            .authorizeRequests()
                // add your resources here. By default, spring security blocks all resources that is not under /resources/**
                .antMatchers(HttpMethod.GET, "/", "/js/**", "/css/**", "/images/**").permitAll()
                // prevent spring security from blocking some pages that doesn't require authentication to be access here.
                .antMatchers("/forgot-password", "/change-password").permitAll()
                .anyRequest().authenticated()
            .and()
            // login configuration
            .formLogin()
                .loginPage("/login") // can either be mapping or file
                .permitAll()
            .and()
            // logout configuration
            .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/")
                .invalidateHttpSession(true)
                .deleteCookies("JSESSIONID")
                .clearAuthentication(true)
                .permitAll();
    }

推荐阅读