首页 > 解决方案 > 如何在spring-security中为嵌套url设置不同的访问权限

问题描述

这是我的安全配置:

protected void configure(HttpSecurity http) throws Exception {


        http.authorizeRequests().antMatchers("**/feedback")
                .access("hasAnyAuthority('MANAGER','EMPLOYEE')")
                .and().authorizeRequests().antMatchers("/api/**","/ticket/**")
                .access("isAuthenticated()")
                .and().authorizeRequests().antMatchers("**/create/**")
                .access("hasAnyAuthority('MANAGER','EMPLOYEE')")
                .and().formLogin().loginPage("/login").failureUrl("/login?error")
                .and().formLogin().defaultSuccessUrl("/ticket/all", true)
                .usernameParameter("email")
                .passwordParameter("password")
                .and().csrf().disable();
    }

我希望每个人都有权查看票证“/ticket”,但我想禁止工程师访问 /ticket/create 和 /ticket/**/feedback。马上

antMatchers("/api/**","/ticket/**")
                    .access("isAuthenticated()")

允许访问所有人。我应该如何正确地做到这一点?多个 antMatchers 是否有某种覆盖规则?

标签: javaspringspring-mvcspring-security

解决方案


您需要添加以下代码:

.and().authorizeRequests().antMatchers("/ticket").permitAll()

代码将是:

http.authorizeRequests().antMatchers("**/feedback")
                .access("hasAnyAuthority('MANAGER','EMPLOYEE')")
                .and().authorizeRequests().antMatchers("/ticket")
                .permitAll()
                .and().authorizeRequests().antMatchers("/api/**","/ticket/**")
                .access("isAuthenticated()")
                .and().authorizeRequests().antMatchers("**/create/**")
                .access("hasAnyAuthority('MANAGER','EMPLOYEE')")
                .and().formLogin().loginPage("/login").failureUrl("/login?error")
                .and().formLogin().defaultSuccessUrl("/ticket/all", true)
                .usernameParameter("email")
                .passwordParameter("password")
                .and().csrf().disable();

我希望能帮助你。


推荐阅读