首页 > 解决方案 > spring security 和 basic auth 只有一个 entryPoint:如何管理另一个端点上的错误

问题描述

我只需要在我的控制器方法之一中添加基本身份验证其他方法没有身份验证,但仍然有一个@PreAuthorize以检查参数是否有效。

我的问题是,httpbasic 转换 preAuthorize 在 401 中抛出的所有异常(有些应该是 403 等)。我觉得我的所有端点都在基本身份验证之下,而不仅仅是我使用的那些@Secured

我怎样才能避免这种情况?

我的代码:

@Controller
class MyController {

@Secured("BASIC_AUTHENTIFIED")
public void someMethodOnlyForAuthentified(){...}


@PreAuthorize("check(parameters)")
public void someMethodForEveryone(List<String> parameters){...}


}

在我的安全配置中:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser(login).password(password).roles("BASIC_AUTHENTIFIED");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .authorizeRequests().antMatchers("/**").permitAll()
            .anyRequest().authenticated()
            .and().httpBasic()
            .and().csrf().disable();
}

标签: javaspringspring-securitybasic-authentication

解决方案


推荐阅读