【问题标题】:Spring Boot - Specific URL AccessDecisionVoterSpring Boot - 特定 URL AccessDecisionVoter
【发布时间】:2020-04-30 10:47:57
【问题描述】:

我想对我的 Spring Boot 应用程序上的 2 个 URL 应用特定的安全规则。

我想使用 AccessDecisionVoter 功能来管理它。
一切都很好......事件太好了。

我的新规则适用于我的 2 个特定网址,但不幸的是适用于 我的所有网址

我的 AccessDecisionManager 声明:

    @Bean
    public AccessDecisionManager playerResourceDecisionManager() {
        List<AccessDecisionVoter<? extends Object>> decisionVoters = Arrays.asList(
            new AuthenticatedVoter(),
            hashSerialSecurityVoter
        );
        return new UnanimousBased(decisionVoters);
    }

这是我的 SecurityConfig 主要代码

        http.csrf()
        .disable().cors().and().exceptionHandling()
        // All Login Stuff
        .authenticationEntryPoint(new Http403ForbiddenEntryPoint() {})
            .and().authenticationProvider(getProvider())
                .formLogin().loginProcessingUrl("/login")
                    .successHandler(new AuthentificationLoginSuccessHandler())
                    .failureHandler(new SimpleUrlAuthenticationFailureHandler())
            .and().logout().logoutUrl("/logout")
                .logoutSuccessHandler(new AuthentificationLogoutSuccessHandler())
                .invalidateHttpSession(true).and().authorizeRequests()

         ...
        // Spring boot actuator - App Status
        .antMatchers("/actuator/*").permitAll()

        // Static Token end points
        .antMatchers("/my-filtered-url1", "/sub/my-filtered-url2/*")
            .permitAll()
            .accessDecisionManager(playerResourceDecisionManager())
            /* Here is the problem : 
             *   I want this accessDescisionManager apply only on my antMatchers
             *    (2 specific URLs), 
             * But it runs on every app calls.
             */

        .antMatchers(HttpMethod.POST, "/log/*").permitAll() 
        /* Login */
        .antMatchers("/login").permitAll()
        .antMatchers("/auth/**").permitAll()
        .antMatchers(HttpMethod.POST,"/user/lost-password").permitAll()

        .antMatchers("/user").hasAuthority("ADMIN")

        .anyRequest().authenticated();


我宁愿不在我的 hashSerialSecurityVoter 类中放入带有 URL 声明的特定代码。 我该怎么做?

问候。

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    安全配置设置如下:

    • http 是构建器(类型为HttpSecurity)。

    • 当您调用 authorizeRequests() 时,它会为您提供第二个构建器 (ExpressionInterceptUrlRegistry)。

    • 当您调用 antMatchers() 时,它会为您提供第三个构建器 (AuthorizedUrl)。

    • 当您调用 permitAll() 时,它会将您返回到第二个构建器。

    这意味着您在 *Registry 构建器上调用 accessDecisionManager(),而不是 AuthorizedUrl 构建器,即调用是全局的,与匹配器无关。

    您的缩进是错误的,这就是您感到困惑的原因:

        .antMatchers("/my-filtered-url1", "/sub/my-filtered-url2/*")
            .permitAll()
        .accessDecisionManager(playerResourceDecisionManager())
    

    访问管理器及其底层选民不负责指定应将哪些访问规则应用于特定 URL,这是 访问表达式 应用于AuthorizedUrl,例如

    • permitAll() - access("permitAll") 的缩写

    • authenticated() - access("authenticated") 的缩写

    • hasRole("ADMIN") - access("hasRole('ROLE_ADMIN')") 的缩写

    • hasAuthority("HASHSERIAL") - access("hasAuthority('HASHSERIAL')") 的缩写

    • 。 . .

    因此,如果您希望自定义 AccessDecisionVoter 对特定 URL 进行投票,那么您实现选民的 supports(ConfigAttribute attribute) 方法来识别特定属性,您全局注册选民,然后指定特定 URL 需要它:

        .antMatchers("/my-filtered-url1", "/sub/my-filtered-url2/*")
            .hasAuthority("HASHSERIAL")
    
    class HashSerialSecurityVoter implements AccessDecisionVoter<Object> {
    
        public boolean supports(ConfigAttribute attribute) {
            if ((attribute.getAttribute() != null)
                    && attribute.getAttribute().equals("HASHSERIAL")) {
                return true;
            }
            else {
                return false;
            }
        }
    
        public boolean supports(Class<?> clazz) {
            return true;
        }
    
        public int vote(Authentication authentication, Object object,
                        Collection<ConfigAttribute> attributes) {
            // Your logic here
        }
    }
    

    【讨论】:

    • 感谢@Andreas 的完整回答。这对我帮助很大。仍然锁定下一步。看我的回答。
    • 相关话题【下一步】:stackoverflow.com/questions/61616638/…
    • 在你的例子中,当它进入supports...attribute.getAttribute()是null
    猜你喜欢
    • 2017-01-04
    • 2014-08-25
    • 2018-11-22
    • 2023-04-04
    • 2015-12-18
    • 2017-11-27
    • 2021-04-21
    • 2019-05-31
    • 2016-12-13
    相关资源
    最近更新 更多