Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据安全请求触发Spring安全登录?_Java_Spring_Spring Security_Spring Web - Fatal编程技术网

Java 根据安全请求触发Spring安全登录?

Java 根据安全请求触发Spring安全登录?,java,spring,spring-security,spring-web,Java,Spring,Spring Security,Spring Web,Spring Security“将为您提供登录表单” 那么,如何在安全请求上触发内置表单呢 @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin").password("admin").roles("ADMIN", "USER").and()

Spring Security“将为您提供登录表单”

那么,如何在安全请求上触发内置表单呢

@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”时,它被正确地阻止和保护,但我希望Spring在从我的前端发出请求时触发其登录表单。尤其是在不创建html文件或/login控制器的情况下,这似乎使此功能成为可能。

您是否也配置了安全筛选器?您应该删除对httpBasic()的调用,并使用formLogin().login页面(“/login”)。查看完整的example@AngeloImmediata这是Spring类还是Java类或xml配置?如果你能够链接一个文档或源…抱歉@StefanEmanuelsson,但除非我在那个例子中遗漏了什么,否则我似乎必须创建一个前端/login.html,这正是我想要摆脱的。我不想重定向,而是在视图中保留时触发Spring安全登录表单;如果配置正确,它应该由Spring Security生成。为了启用它,您不仅必须像以前那样创建一个配置类,而且还需要配置WebServlet过滤器。我在
web.xml
中这样配置它:`springSecurityFilterChainorg.springframework.web.filter.DelegatingFilterProxyspringSecurityFilterChainMySpringSvlt`