2015-04-23 54 views
2

我创建了一个CustomWebSecurityExpressionHandler,通过搜索函数ID来检查db表上的用户。我想在每个函数上只改变一些数据库更新并重新启动上下文,而不用重新编译和编辑一堆XML。Spring WebFlow + Spring Security:使用表达式而不是角色

我想在webflow中使用SpringSecurityExpression!就像我在春季其他任何地方都可以做的那样...

<?xml version="1.0" encoding="UTF-8"?> 
<flow xmlns="http://www.springframework.org/schema/webflow" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/webflow 
          http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"> 

    <secured attributes="isFUUU('key')" /> 

    <view-state id="main" view="dashboard/main.html" > 
    </view-state> 

</flow> 

我该如何让isFUU(“key”)工作?这是一个自定义的CustomAccessDecisionManager需要?

回答

1

我找到了一个解决办法

我不得不调试20个班春季安全的Webflow的发现,在SecurityFlowExecutionListener你即使设置弹簧安全性表达工作,听者将只有基于角色的。 我发现解析表达式需要一个特定的类型配置属性,WebExpressionConfigAttribute是精确的。 但它不是公开课! https://jira.spring.io/browse/SEC-1727。 所以在这个老吉拉建议,我需要创造我CustomSecurityFlowExecutionListener在同一封装(org.springframework.security.web.access.expression)

下面的例子

CustomSecurityFlowExecutionListener:

package org.springframework.security.web.access.expression; //First part of the trick! 

import foo.bar.example.services.security.CustomAccessDecisionManager; 

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.List; 

import org.springframework.expression.ExpressionParser; 
import org.springframework.security.access.AccessDecisionManager; 
import org.springframework.security.access.ConfigAttribute; 
import org.springframework.security.access.SecurityConfig; 
import org.springframework.webflow.security.SecurityFlowExecutionListener; 
import org.springframework.webflow.security.SecurityRule; 

/** 
* Force Spring WebFlow Security listener to use expression! 
* 
* @author roberto.gabrieli 
*/ 
public class CustomSecurityFlowExecutionListener<T > extends SecurityFlowExecutionListener 
{ 

    /** 
    * Convert SecurityRule into a form understood by Spring Security Force the usage of WebExpressionConfigAttribute! 
    * 
    * @param rule 
    *   the rule to convert 
    * @return list of ConfigAttributes for Spring Security 
    */ 
    @Override 
    @SuppressWarnings("deprecation") 
    protected Collection<ConfigAttribute> getConfigAttributes(SecurityRule rule) 
    { 
     // Get Access Decision Manager to find if has my expression handler 
     AccessDecisionManager adm = getAccessDecisionManager(); 

     ExpressionParser ep = null; 
     // Check if is my CustomAccessDecisionManager so I can use my expressions 
     if (adm instanceof CustomAccessDecisionManager) 
     { 
      ep = ((CustomAccessDecisionManager) adm).getWebSecurityExpressionHandler().getExpressionParser(); 
     } 

     List<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>(); 
     for (String attribute : rule.getAttributes()) 
     { 
      if (ep != null) 
       // this will end the trick with fireworks! 
       configAttributes.add(new WebExpressionConfigAttribute(ep.parseExpression(attribute))); 
      else 
       configAttributes.add(new SecurityConfig(attribute)); 
     } 
     return configAttributes; 
    } 
} 

Webflow的-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:webflow="http://www.springframework.org/schema/webflow-config" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
     http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.4.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 
... 

    <bean id="securityFlowExecutionListener" 
     class="org.springframework.security.web.access.expression.MamSecurityFlowExecutionListener"> 
     <property name="accessDecisionManager" ref="customAccessDecisionManager"/> 
    </bean> 

... 
</beans> 
0

我找到了另一种解决方案如何使用Spring表达郎在WebFlows中使用。它来自“Pro Spring Security”一书。简而言之,他们定义了定制AccessDecisionManger定制AccessDecisionVoterimplements AccessDesisionVoter<org.springframework.webflow.engine.State)和定制SecurityExpressionRoot。所以不需要像你的解决方案那样需要一个自己的监听器。这些自定义类支持流状态级别的表达式。你可以在github上找到完整的例子link