Spring Security Allows Unauthorized User Access to Restricted URL from a Forward

后端 未结 2 594
醉话见心
醉话见心 2021-01-17 06:45

Spring Security 3.2.0.RC2

Given:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
        .authorize         


        
相关标签:
2条回答
  • 2021-01-17 07:33

    @kungfuters is correct that the first step is ensuring the Filter is intercepting that request in the first place. To do so with a web.xml you would use the following:

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher> <!-- Include FORWARD here -->
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    

    To do so with Java Configuration you would use the following:

    public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
    
        protected  EnumSet<DispatcherType> getSecurityDispatcherTypes() {
            return return EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.ASYNC, DispatcherType.FORWARD);
        }
    
    }
    

    The last piece is that the FilterSecurityInterceptor (the piece that ensures URLs are protected) by default will only intercept the REQUEST and not additional dispatches (i.e. forwards). This is done because it is quite rare to protect the URLs that are forwarded to (typically you would protect the URL that does the forwarding). To enable that you need to use the following with xml configuration you need to use http@once-per-request=true:

    <http once-per-request="true">
       <!-- ... -->
    </http>
    

    Similarly, there is a oncePerRequest property within Java Configuration that can be used. For example:

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .authorizeRequests()
                .filterSecurityInterceptorOncePerRequest(false)
                // make sure to grant access to any login page you are forwarding to
                .antMatchers("/restricted/login").permitAll()
                .antMatchers("/restricted/**").hasRole("admin")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .permitAll()
            // etc
            ;
    }
    
    0 讨论(0)
  • 2021-01-17 07:40

    If you are using web.xml to configure your filter, try this:

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher> <!-- Include FORWARD here -->
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    

    ...or use the Servlet3 Java-based Config equivalent, which is to extend AbstractSecurityWebApplicationInitializer and override the getSecurityDispatcherTypes() method:

    public class YourSecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
    
        protected  EnumSet<DispatcherType> getSecurityDispatcherTypes() {
            // Return dispatcher types here, in your case you'll want the defaults, 
            // which are DispatcherType.REQUEST and DispatcherType.ERROR
            // ...as well as the one you need for your use case: DispatcherType.FORWARD
        }
    
    }
    

    I typed that here, so hopefully there are no errors. Should get you going, though.

    0 讨论(0)
提交回复
热议问题