0

我对 Struts2(2.3.15.1) Spring 插件有疑问。我使用 Shiro 来保证安全性,并使用 Annotations 对其进行配置。代码如下所示:

public class Order2Action extends BaseAction {

 @Autowired  private UserDAO userDAO;

 private static final Logger log = LoggerFactory.getLogger(Order2Action.class);


 @Action(value = "list2",
    results = {@Result(name = SUCCESS, location = "list.jsp")}
 )
 @RequiresRoles(ROLE_DATA_OPERATOR)
 public String showOrderList() throws InterruptedException {
   log.info("UserDAOImpl {}", userDAO);
   return SUCCESS;
 }
}

在这种情况下,UserDAO 不会注入到 Action。但是如果我注释掉字符串“@RequiresRoles(ROLE_DATA_OPERATOR)” - UserDAO 被注入到 Action。

这里可能是什么问题?

我的 ApplicationContext conf 如下所示:

<?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:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context-3.0.xsd
                       http://www.springframework.org/schema/tx
                       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- enable processing of annotations such as @Autowired and @Configuration -->
    <context:annotation-config/>
    <context:component-scan base-package="ee"/>


<bean id="makuDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    ...
</bean>

<!--Shiro security configuration -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="/maku/auth/login"/>
    <property name="unauthorizedUrl" value="/maku/auth/unauthenticated"/>
</bean>

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="cacheManager" ref="shiroCacheMan"/>
    <property name="realm" ref="jdbcRealm"/>
</bean>

<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
    <property name="dataSource" ref="makuDataSource"/>
    <property name="credentialsMatcher">
        <bean class="org.apache.shiro.authc.credential.PasswordMatcher"/>
    </property>
</bean>

<bean name="shiroCacheMan" class="org.apache.shiro.cache.ehcache.EhCacheManager"/>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- Enable Shiro Annotations for Spring-configured beans.  Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
    <property name="proxyTargetClass" value="true" />
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean>

</beans>

用户道:

public interface UserDAO {
  User getByUsername(String username);
}

@Repository
public class UserDAOImpl extends AbstractJDBCDAO implements UserDAO {

  @Override
  public User getByUsername(String username){
    return makuTmpl.queryForObject("select id, username, fullname from users where     username=?",
    new RowMapper<User>() {
      @Override
      public User mapRow(ResultSet rs, int i) throws SQLException {
        User u = new User();
        u.setId(rs.getLong( 1 ));
        u.setUsername(rs.getString(2));
        u.setFullname(rs.getString(3));
        return u;
      }
    }, username);
  }
}
4

0 回答 0