2012-01-05 91 views
0

我想寫一個自定義方法安全攔截器。但是,它並未使用在我的安全上下文中添加到bean屬性中的身份驗證管理器,並且在檢查身份驗證管理器是否存在時返回null。誰能闡明爲什麼身份驗證管理器bean屬性沒有被使用?我使用的是彈簧安全3.0.5在WebSphere 7.0春季安全方法安全攔截器不拿起認證管理器

下面是一個包含bean的方法攔截

<beans:bean id="methodInterceptor" 
    class="bigbank.security.CustomMethodSecInterceptor"> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
    <beans:property name="accessDecisionManager" ref="universalAccessDecisionManager" /> 
    <beans:property name="securityMetadataSource" ref="tspmMethodSecurityMetaData" /> 

這裏是我的方法安全性攔截

public class CustomMethodSecInterceptor extends MethodSecurityInterceptor { 

private static final Log logger = LogFactory 
     .getLog(WebSphere2SpringSecurityPropagationInterceptor.class); 
private AuthenticationManager authenticationManager = null; 
private AuthenticationDetailsSource authenticationDetailsSource = new WebSpherePreAuthenticatedAuthenticationDetailsSource(); 
private final WASUsernameAndGroupsExtractor wasHelper; 

public CustomMethodSecInterceptor() { 
    wasHelper = new DefaultWASUsernameAndGroupsExtractor(); 
} 

@Override 
public Object invoke(MethodInvocation mi) throws Throwable { 
    try { 
     logger.debug("Performing Spring Security authentication with WebSphere credentials"); 
     System.out.println("@@going through ss authentication"); 
     authenticateSpringSecurityWithWASCredentials(); 
     InterceptorStatusToken token = super.beforeInvocation(mi); 

     logger.debug("Proceeding with method invocation"); 
     Object result = mi.proceed(); 
     return super.afterInvocation(token, result); 

    } finally { 
     logger.debug("Clearing Spring Security security context"); 
     SecurityContextHolder.clearContext(); 
    } 
} 

private void authenticateSpringSecurityWithWASCredentials() { 
    Assert.notNull(authenticationManager); // This is where the error is coming up 
    Assert.notNull(authenticationDetailsSource); 

    String userName = wasHelper.getCurrentUserName(); 
    if (logger.isDebugEnabled()) { 
     logger.debug("Creating authentication request for user " + userName); 
    } 
    PreAuthenticatedAuthenticationToken authRequest = new PreAuthenticatedAuthenticationToken(
      userName, "N/A"); 
    authRequest.setDetails(authenticationDetailsSource.buildDetails(null)); 
    if (logger.isDebugEnabled()) { 
     logger.debug("Authentication request for user " + userName + ": " 
       + authRequest); 
    } 
    Authentication authResponse = authenticationManager 
      .authenticate(authRequest); 
    if (logger.isDebugEnabled()) { 
     logger.debug("Authentication response for user " + userName + ": " 
       + authResponse); 
    } 
    SecurityContextHolder.getContext().setAuthentication(authResponse); 
} 

public void setAuthenticationManager(
     AuthenticationManager authenticationManager) { 
    this.authenticationManager = authenticationManager; 
} 

}

以下是錯誤:

Caused by: java.lang.IllegalArgumentException: An AuthenticationManager is required 
at org.springframework.util.Assert.notNull(Assert.java:112) 
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.afterPropertiesSet(AbstractSecurityInterceptor.java:118) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409) 
... 119 more 

回答

0

您已經覆蓋了setAuthenticationManager方法,所以當它被調用春天注入AuthenticationManager,它不設置在AbstractSecurityInterceptor相應字段。

由於基類包含此屬性的getter,因此最好刪除字段和setter方法,並只使用getter訪問代碼中的驗證管理器。