【问题标题】:ROLE_USER and ADMIN access restriction to URL'sROLE_USER 和 ADMIN 对 URL 的访问限制
【发布时间】:2016-10-19 22:43:24
【问题描述】:

我有

/welcome/employees - 员工名单

/welcome/employees/edit/ - 编辑员工

/welcome/employees/find/ - 查找员工

我想定义ADMIN 来访问所有内容,但USER 只是为了查看列表和查找。

我应该如何进行相应的配置。

   @Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/welcome", "/welcome/employees", "/welcome/employee/find").access("hasRole('ROLE_USER')")
        .antMatchers("/welcome/**").access("hasRole('ROLE_ADMIN')")
        .and()
            .formLogin().loginPage("/login")
            .defaultSuccessUrl("/welcome")
            .failureUrl("/login?error")
            .usernameParameter("username").passwordParameter("password")                
        .and()
            .logout().logoutSuccessUrl("/login?logout"); 
    
}

【问题讨论】:

    标签: spring spring-mvc spring-security spring-boot


    【解决方案1】:

    如果您不指定 ROLE 来访问每个经过身份验证的用户都可以访问该 url。你可以这样试试。

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/welcome/employees/edit/**").access("hasRole('ROLE_ADMIN')")
        .anyRequest().fullyAuthenticated()
        // part of your config
    }
    

    如果USER 尝试访问/welcome/employees/edit/ 将得到 403 错误。但是ADMIN 可以使用此配置访问每个 url。因为如果您不指定访问角色,每个人都可以访问该网址。但除了edit 页面,每个用户都可以在登录后访问every url。不要忘记这一点。

    【讨论】:

    • 我实际上在 localhost 页面不工作 localhost 重定向你太多次之间看到了这一点。尝试:重新加载页面 清除 cookie ERR_TOO_MANY_REDIRECTS 现在似乎没有开始,请帮助
    • 我添加了这个 .permitAll() 并且解决了太多重定向。现在似乎限制用户使用您的代码版本编辑页面,如果我也想将他限制为/添加怎么办?
    • 您可以将/add添加到antMatchers(),现在只有管理员可以访问/add
    猜你喜欢
    • 2013-03-28
    • 2018-09-02
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2014-02-05
    相关资源
    最近更新 更多