Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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
Spring boot SpringSecurity(使用SpringBoot)返回登录表单作为POST、EDIT、DELETE操作的响应_Spring Boot_Spring Security_Authorization - Fatal编程技术网

Spring boot SpringSecurity(使用SpringBoot)返回登录表单作为POST、EDIT、DELETE操作的响应

Spring boot SpringSecurity(使用SpringBoot)返回登录表单作为POST、EDIT、DELETE操作的响应,spring-boot,spring-security,authorization,Spring Boot,Spring Security,Authorization,我正在制作一个meme共享应用程序,我在其中保护了一些REST端点,如POST、DELETE和EDIT。并删除了GET操作的身份验证 @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/xmeme/delete/**").hasAuthority("ADMIN"

我正在制作一个meme共享应用程序,我在其中保护了一些REST端点,如POST、DELETE和EDIT。并删除了GET操作的身份验证

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/xmeme/delete/**").hasAuthority("ADMIN")
        .antMatchers("/xmeme/edit/**").hasAnyAuthority("ADMIN")
        .antMatchers("/xmeme/post").hasAnyAuthority("ADMIN", "USER")
        .antMatchers("/user/register").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin().permitAll().defaultSuccessUrl("/xmeme/memes", true)
        .and()
        .logout().permitAll()
        .and()
        .exceptionHandling().accessDeniedPage("/403");
}


 @Override public void configure(WebSecurity web) throws Exception {
     web.ignoring().antMatchers("/xmeme"); 
     web.ignoring().antMatchers("/xmeme/memes"); 
     web.ignoring().antMatchers("/xmeme/meme/**"); 
 }
现在的问题是,当我试图发布一个meme并在POSTMAN中使用Authorization>Type>Basic Auth选项时,我得到的是登录表单作为响应。编辑和删除操作也会发生同样的情况

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/xmeme/delete/**").hasAuthority("ADMIN")
        .antMatchers("/xmeme/edit/**").hasAnyAuthority("ADMIN")
        .antMatchers("/xmeme/post").hasAnyAuthority("ADMIN", "USER")
        .antMatchers("/user/register").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin().permitAll().defaultSuccessUrl("/xmeme/memes", true)
        .and()
        .logout().permitAll()
        .and()
        .exceptionHandling().accessDeniedPage("/403");
}


 @Override public void configure(WebSecurity web) throws Exception {
     web.ignoring().antMatchers("/xmeme"); 
     web.ignoring().antMatchers("/xmeme/memes"); 
     web.ignoring().antMatchers("/xmeme/meme/**"); 
 }
我找不到解决办法。有人能为这个问题提出一些解决方法吗

  • 您应该在
    configure(HttpSecurity http)
    方法中使用ROLE\u前缀指定权限,例如ROLE\u ADMIN和ROLE\u USER
  • configure(WebSecurity-web)
    方法应放在
    configure(HttpSecurity-http)
  • 使用http.CSRF().Disable()禁用CSRF令牌
  • 验证用户om是否已分配正确的权限


  • 您已在
    configure(HttpSecurity http)

    如果要启用HTTP基本身份验证,需要在
    configure(HttpSecurity-HTTP)


    如果您同时配置了
    formLogin
    httpBasic
    ,Spring Security将根据请求的内容类型使用内容协商来确定它应该如何对待未经身份验证的用户。

    非常感谢Romil和Eleftheria Stein Kousathana,您的建议奏效了!
    http.formLogin()
    
    http.httpBasic()