denyAll()重定向到登录网址,但我需要另一个网址

时间:2014-05-25 01:35:18

标签: spring spring-mvc spring-security

此代码正确阻止了在XHTML上完成的每个请求,但我想将请求重定向到url,如" / spring / denied"不是" / spring / login"这是在方法formLogic()

上设置的
        http
        .formLogin()
            .loginPage("/spring/login")
            .loginProcessingUrl("/spring/loginProcess")
            .defaultSuccessUrl("/spring/main")
            .failureUrl("/spring/login?login_error=1")
            .and()
        .logout()
            .logoutUrl("/spring/logout")
            .logoutSuccessUrl("/spring/logoutSuccess")
            .and()
        .authorizeRequests().antMatchers("/spring/**/*.xhtml").denyAll()
        .and()


        // Disable CSRF (won't work with JSF) but ensure last HTTP POST request is saved
        // See https://jira.springsource.org/browse/SEC-2498

        .csrf().disable()
        .requestCache()
            .requestCache(new HttpSessionRequestCache());

所以,我认为会有这些可能的场景:

  1. 有人想直接访问任何真正的XHTML文件(/main/index.xhtml):行为:请求被阻止并重定向到被拒绝的URL ,如果有人希望它,他必须使用正确的流程进行交互定义(pe / main,/ groups ....)
  2. 有人想要访问没有正确权限或匿名的安全网址(p.e./ admin,/ authenticate ...):行为:Spring安全拦截请求并重定向到登录网址
  3. 有些意图访问具有正确权限的安全URL(p.e / admin,/ authenticate ....):行为:Spring安全授予访问权限,弹簧Web流程使其任务正确重定向
  4. 有人打算访问未知网址(p.e./ImAUnluckyGuyAndThisUrlIsUnreal):行为:Spring webflow拦截请求并重定向到最后一个已知流程。
  5. 使用上面的XML配置案例是正确的。此外,我使用spring webflow 2.3而不是2.4.0RC1和Annotations配置

    案例1 :在web.xml上添加此代码,我不知道如何替换fot注释配置

        <security-constraint>
              <display-name>Restrict direct access to XHTML access</display-name>
              <web-resource-collection>
                <web-resource-name>XHTML</web-resource-name>
                <url-pattern>*.xhtml</url-pattern>
              </web-resource-collection>
            <auth-constraint />
      </security-constraint>
    

    案例4 :在抽象流程定义中添加此代码,我不知道是否无法在Spring webflow 2.4.0RC1上工作或者它是注释配置问题

        <global-transitions>
           <transition on-exception="org.springframework.webflow.engine.NoMatchingTransitionException" to="handlingViewState">
            <evaluate expression="handlingBean.handle(flowExecutionException)">    </evaluate>
        </transition>
    </global-transitions>
    

    案例2和3 :这些都没有问题。如果用户已通过身份验证但未获得权限,则使用 .exceptionHandling()重定向.addadDeniedPage(&#34; / spring / denied&#34;)或匿名用户被重定向到 loginPage()

    Webflow配置

    @Bean
    public FlowExecutor flowExecutor() {
        return getFlowExecutorBuilder(flowRegistry())
                .addFlowExecutionListener(new FlowFacesContextLifecycleListener())
                .addFlowExecutionListener(new SecurityFlowExecutionListener())
                .build();
    }
    
    @Bean
    public FlowDefinitionRegistry flowRegistry() {
        return getFlowDefinitionRegistryBuilder(flowBuilderServices())
                .setBasePath("/WEB-INF/flows")
                .addFlowLocationPattern("/**/*-flow.xml")
                .build();
    }
    
    @Bean
    public FlowBuilderServices flowBuilderServices() {
        return getFlowBuilderServicesBuilder().setDevelopmentMode(true).build();
    }
    

    }

    MVC配置

    @Autowired
    private WebFlowConfig webFlowConfig;
    
    @Bean
    public FlowHandlerMapping flowHandlerMapping() {
        FlowHandlerMapping mapping = new FlowHandlerMapping();
        mapping.setOrder(1);
        mapping.setFlowRegistry(this.webFlowConfig.flowRegistry());
        /* If no flow matches, map the path to a view, e.g. "/intro" maps to a view named "intro" */
        mapping.setDefaultHandler(new UrlFilenameViewController());
        return mapping;
    }
    
    @Bean
    public FlowHandlerAdapter flowHandlerAdapter() {
        JsfFlowHandlerAdapter adapter = new JsfFlowHandlerAdapter();
        adapter.setFlowExecutor(this.webFlowConfig.flowExecutor());
        return adapter;
    }
    
    @Bean
    public UrlBasedViewResolver faceletsViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setViewClass(JsfView.class);
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".xhtml");
        return resolver;
    }
    
    @Bean
    public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
        return new SimpleControllerHandlerAdapter();
    }
    

1 个答案:

答案 0 :(得分:2)

您需要的是多个<http> [HttpSecurity]配置,您需要提供自定义AuthenticationEntryPoint实现。

以下是案例1的HttpSecurity配置。(我希望您能为案例2和3提供配置,并且您可以使用已有的配置。)

@Configuration
@Order(1)                                                        
public static class XHTMLAccessDenyWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/spring/**/*.xhtml")
            .exceptionHandling().authenticationEntryPoint(new AccessDenyEntryPoint()).and()                
            .authorizeRequests().antMatchers("/spring/**/*.xhtml").denyAll();
    }
}

注意:上述安全配置的顺序应高于案例2和安全配置的安全配置。 3;因此,@Order被使用。

自定义AuthenticationEntryPoint实施只会将请求重定向到/ spring / deny页面,如下所示

public class AccessDenyEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        request.getRequestDispatcher("/spring/denied").forward(request, response);
    }
}