代码之家  ›  专栏  ›  技术社区  ›  Droo

从数据库或属性获取Spring安全截获URL

  •  3
  • Droo  · 技术社区  · 16 年前

    希望这是非常简单的,存在的,我忽略了我鼻子下面的一些东西。我知道我可以通过注释限制访问:

    @Secured({"ROLE_ADMIN"})
    

    或通过配置:

    <security:intercept-url pattern="/**" access="ROLE_USER, ROLE_ADMIN, ROLE_SUPER_USER" />
    

    我希望从数据库获取身份验证规则,例如:

    <security:intercept-url provider="authProvider"/>
    
    <bean id="authProvider" class="AuthProviderImpl">
        <property name="userDetailsService" ref="userDetailsService"/>
    </bean>
    

    最坏的情况是,必须有一种通过属性文件填充的方法,对吗?…

    /admin/**=ROLE_ADMIN
    /**=ROLE_USER

    <security:intercept-url props="classpath:urls.properties"/>
    

    等。

    请告诉我这个存在,否则我的大脑会爆炸!!!!GrailsSpring安全插件附带了这个现成的插件,所以我知道它必须存在。请不要让我的大脑爆炸!!!!

    编辑:

    明白了…

    你必须提供一个自定义 org.springframework.security.intercept.web.FilterSecurityInterceptor 并提供 objectDefinitionSource 以下内容:

    <bean id="filterSecurityInterceptor" class="org.springframework.security.intercept.web.FilterSecurityInterceptor">
        <security:custom-filter before="FILTER_SECURITY_INTERCEPTOR" />
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="accessDecisionManager" ref="accessDecisionManager" />
        <property name="objectDefinitionSource">
            <value>
                CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
                PATTERN_TYPE_APACHE_ANT
                /**login.html=IS_AUTHENTICATED_ANONYMOUSLY
                /user/**=ROLE_ADMIN
            </value>
        </property>
    </bean>
    

    我想我要用一个工厂豆:

    public class RequestMappingFactoryBean implements FactoryBean {
    
        private final static String EOL = System.getProperty("line.separator");
    
        public Object getObject() throws Exception {
            StringBuffer sb = new StringBuffer();
            sb.append("CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON");
            sb.append(EOL);
            sb.append("PATTERN_TYPE_APACHE_ANT");
            sb.append(EOL);
            sb.append("/**login.html=IS_AUTHENTICATED_ANONYMOUSLY");
            sb.append(EOL);
            sb.append("/user/**=ROLE_ADMIN");
            return sb.toString();
        }
    
        @SuppressWarnings("unchecked")
        public Class getObjectType() {
            return String.class;
        }
    
        public boolean isSingleton() {
            return true;
        }
    
    }
    

    给它一把刀等。

    <bean id="filterSecurityInterceptor" class="org.springframework.security.intercept.web.FilterSecurityInterceptor">
        <security:custom-filter before="FILTER_SECURITY_INTERCEPTOR" />
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="accessDecisionManager" ref="accessDecisionManager" />
        <property name="objectDefinitionSource" ref="requestMappings" />
    </bean>
    
    <bean id="requestMappings" class="RequestMappingFactoryBean" />
    
    2 回复  |  直到 16 年前
        1
  •  2
  •   Chris Freeman    16 年前

        2
  •  3
  •   Michel    16 年前

    <!-- Settings -->
    <b:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <b:property name="locations">
            <b:value>/WEB-INF/config.properties</b:value>
        </b:property>
    </b:bean>
    

    <http entry-point-ref="authenticationProcessingFilterEntryPoint">
            <intercept-url pattern='/custom/**' access="${roles.admin}"/>
    </http>
    
    推荐文章