首页 > 解决方案 > 在安全请求上触发 Spring Security 登录?

问题描述

Spring Security“将为您提供登录表单”... https://docs.spring.io/spring-security/site/docs/current/guides/html5/hellomvc-javaconfig.html

那么如何触发安全请求的内置表单呢?

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("admin").password("admin").roles("ADMIN", "USER").and()
            .withUser("guest").password("guest").roles("USER");
}

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .httpBasic().and()
            .authorizeRequests()
            .antMatchers(HttpMethod.POST,"/test").hasRole("ADMIN")
            .antMatchers(HttpMethod.PUT,"/test/**").hasRole("ADMIN")
            .antMatchers(HttpMethod.DELETE,"/test/**").hasRole("ADMIN")
            .antMatchers(HttpMethod.PATCH,"/test/**").hasRole("ADMIN").and()
            .formLogin()
            .and()
            .csrf().disable();
}

当我在没有登录的情况下对“/test”进行 POST 时,它会被正确地阻止和保护,但我希望 Spring 在从我的前端发出请求时触发其登录表单。尤其是在没有创建 html 文件或 /login 控制器的情况下,这似乎使此功能成为可能。

标签: javaspringspring-securityspring-web

解决方案


推荐阅读