停用spring formlogin和基本身份验证

时间:2019-04-08 22:37:09

标签: spring-boot spring-security

我具有以下spring boot 2.0配置,但仍获得基本的auth登录屏幕。我不想像互联网上几乎所有帖子所建议的那样禁用所有春季安全性。我只想停止表单登录页面和基本身份验证,因此可以使用自己的表单。
我已经看到了allowAll和exclude = {SecurityAutoConfiguration.class}的所有建议,还有一些我不记得了的其他建议。这些不是我想要的。我想使用Spring Security,但是我的配置不是Spring Boots。是的,我知道很多人会说这是重复的,但是我不同意,因为所有其他答案都是完全禁用spring安全性,而不仅仅是停止愚蠢的登录页面。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class CustomSecurity extends WebSecurityConfigurerAdapter {
private final RememberMeServices rememberMeService;
private final AuthenticationProvider customAuthProvider;
@Value("${server.session.cookie.secure:true}")
private boolean useSecureCookie;

@Inject
public CustomSecurity(RememberMeServices rememberMeService, AuthenticationProvider customAuthProvider) {
    super(true);
    this.rememberMeService = rememberMeService;
    this.bouncerAuthProvider = bouncerAuthProvider;
}

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/v2/**").antMatchers("/webjars/**").antMatchers("/swagger-resources/**")
       .antMatchers("/swagger-ui.html");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().disable().formLogin().disable();
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).headers().frameOptions().disable();
    http.authenticationProvider(customAuthProvider).authorizeRequests().antMatchers("/health").permitAll()
        .anyRequest().authenticated();
    http.rememberMe().rememberMeServices(rememberMeService).useSecureCookie(useSecureCookie);
    http.exceptionHandling().authenticationEntryPoint(new ForbiddenEntryPoint());
}
}

1 个答案:

答案 0 :(得分:1)

如果您想重定向到自己的登录页面,我可以显示您的示例代码和配置

删除http.httpBasic().disable().formLogin().disable();,您应该将自己的登录页面设置为重定向,而不是禁用表单登录

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/my_login").permitAll().and().authorizeRequests().anyRequest().authenticated();
        http.formLogin().loginPage("/my_login");
    }

然后创建自己的LoginController

@Controller
public class LoginController {

    @RequestMapping("/my_login")
    public ModelAndView myLogin() {
        return new ModelAndView("login");
    }
}

您可以使用login视图解析器指定thymeleaf

相关问题