【问题标题】:spring boot basic http authentication with multiple roles throws 403 forbidden error具有多个角色的spring boot基本http身份验证引发403禁止错误
【发布时间】:2015-09-05 21:20:54
【问题描述】:

我正在尝试使用多个角色配置 spring boot-Embedded Tomcat 基本 HTTP 身份验证,其中大多数 url 相似,但很少有特定于每个角色的。在这里,对于第一个角色,基本的 HTTP 身份验证会弹出并正常工作。使用下面的代码,

    @Configuration
    @EnableWebMvcSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)

public class TestSecurityAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests().antMatchers(null, getAppAdminRolePaths()).authenticated()
                .anyRequest().hasAnyRole("APPADMIN")
                .and()
                .httpBasic();

        http.csrf().disable()
                .authorizeRequests().antMatchers(null, getAppUserRolePaths()).authenticated()
                .anyRequest().hasAnyRole("APPUSER")
                .and()
                .httpBasic();

        http.authorizeRequests().antMatchers(null, new String[]{"/app/appOwnerView.html"}).authenticated()
                .anyRequest().hasAnyRole("APPOWNER")
                .and()
                .httpBasic();
    }

    @Override
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("appadminname").password("appadminpwd").roles("APPADMIN").and()
        .withUser("appusername").password("appuserpwd").roles("APPUSER").and()
        .withUser("appownername").password("appoownerpwd").roles("APPOWNER");
    }

    private static String[] getAppAdminRolePaths(){
        return new String[]{"/appweb/*",
                "/app/checkService.html",               
                "/app/index.html",                
                "/app/testData.html",    
                "/app/adminView.html", 
                "/app/demo.html"};
    }

    private static String[] getAppUserRolePaths(){
        return new String[]{"/appweb/*",
                "/app/checkService.html",               
                "/app/index.html",                
                "/app/testData.html",    
                "/app/userView.html", 
                "/app/demo.html"};
    }
}

对于带有 url http://localhost:8080/app/index.html 的浏览器中的 HTTP 用户名/密码弹出窗口,使用 appadminname/appadminpwd 可以正常工作。但是对于相同的 url,如果我输入 appusername/appuserpwd 它会抛出 HTTP 403 禁止访问错误。我不确定为什么配置的第二个角色 APPUSER 会抛出此错误。请告知是否有办法解决此问题。

谢谢

【问题讨论】:

    标签: spring-security spring-boot basic-authentication spring-4 embedded-tomcat-7


    【解决方案1】:

    我很欣赏这个问题现在有点老了,但这可能对某人仍然有用。

    首先,我不确定为什么您对 antMatchers() 的调用提供 null 作为第一个参数; antMatchers() 期望定义此规则所涵盖的 URL 的字符串列表,因此我不确定在这种情况下应该匹配什么 null。

    其次,anyRequest() 意味着该规则将应用于对应用程序发出的任何请求,而不管使用的 URL 是什么,Spring 将按照定义的顺序应用安全规则。您通常会首先定义 URL 及其关联角色,然后默认为任何其他必须使用类似 anyRequest().authenticated() 进行身份验证的请求(但不一定需要任何特定角色)的规则

    您的第一条规则说 任何 向应用程序发出的请求必须由具有角色 APPADMIN 的用户发出,当您尝试以 appusername 登录时,它会拒绝您访问,因此第二条规则允许APPUSERs 甚至没有被处理。

    第三,当您可能实际上应该将它们链接在一起时,您正在对 http.authorizeRequests() 进行多次调用,例如:

    http.csrf().disable().authorizeRequests()
        .antMatchers( getAppAdminRolePaths() ).hasRole("APPADMIN")
        .antMatchers( getAppUserRolePaths() ).hasRole("APPUSER")
        .anyRequest().authenticated();
    


    最后,当您只有一个角色要检查时,您可以使用 hasRole() 而不是 hasAnyRole()。

    您也不需要在同一规则中提供 authenticated() 和 hasRole(),因为 hasRole() 意味着用户已经通过身份验证。

    您可以在 Spring 文档中找到更多解释和示例:http://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#authorize-requests

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 2019-10-24
      • 2017-08-03
      • 2019-05-12
      • 2020-12-23
      • 2016-11-05
      • 2020-12-26
      • 1970-01-01
      相关资源
      最近更新 更多