Spring boot SpringSecurity(使用SpringBoot)返回登录表单作为POST、EDIT、DELETE操作的响应
我正在制作一个meme共享应用程序,我在其中保护了一些REST端点,如POST、DELETE和EDIT。并删除了GET操作的身份验证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"
@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 USERconfigure(WebSecurity-web)
方法应放在configure(HttpSecurity-http)
您已在
configure(HttpSecurity http)
如果要启用HTTP基本身份验证,需要在configure(HttpSecurity-HTTP)
如果您同时配置了
formLogin
和httpBasic
,Spring Security将根据请求的内容类型使用内容协商来确定它应该如何对待未经身份验证的用户。非常感谢Romil和Eleftheria Stein Kousathana,您的建议奏效了!
http.formLogin()
http.httpBasic()