Spring Security(带有Spring Boot)返回登录表单作为POST、EDIT、DELETE操作的响应



我正在制作一个表情包共享应用程序,在该应用程序中我保护了一些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/**"); 
}

现在的问题是,当我试图发布一个模因并使用Authorization>类型>POSTMAN中的基本身份验证选项,我得到了登录表单作为响应。EDIT和DELETE操作也是如此。

我找不到解决办法。有人能为这个问题提出一些解决办法吗?

在此处输入图像描述

在此处输入图像描述

您已在configure(HttpSecurity http)中为您的应用程序启用表单登录

http.formLogin()

如果要启用HTTP基本身份验证,则需要在configure(HttpSecurity http)中进行配置

http.httpBasic()

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

  1. 您应该使用ROLE_前缀指定权限,例如configure(HttpSecurity http)方法中的ROLE_ADMINROLE_USER
  2. configure(WebSecurity web)方法应放在configure(HttpSecurity http)之前检查详细用法
  3. 使用http.csrf().disable()禁用CSRF令牌
  4. 验证用户om是否分配了正确的权限

@Override public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/xmeme"); 
web.ignoring().antMatchers("/xmeme/memes"); 
web.ignoring().antMatchers("/xmeme/meme/**"); 
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/xmeme/delete/**").hasAuthority("ROLE_ADMIN")
.antMatchers("/xmeme/edit/**").hasAnyAuthority("ROLE_ADMIN")
.antMatchers("/xmeme/post").hasAnyAuthority("ROLE_ADMIN", "ROLE_USER")
.antMatchers("/user/register").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll().defaultSuccessUrl("/xmeme/memes", true)
.and()
.logout().permitAll()
.and()
.exceptionHandling().accessDeniedPage("/403");
}

最新更新