具有弹簧安全性的多个数据库

时间:2015-05-18 16:43:10

标签: java spring-security

我正在尝试扩展 spring security 以使用多个数据库,对于身份验证提供程序我正在使用自定义类< em> MyUserDetailsS​​ervice ,它扩展了 JdbcDaoImpl 。 我创建了一个类 RoutingDataSource 来执行到相应数据库的路由并进行必要的XML配置,但我没有得到任何路由,即使通过调试,应用程序也从不执行RoutingDataSource的代码行类。
这段代码有什么问题?我该如何解决?

public class RoutingDataSource extends AbstractRoutingDataSource {


    @Override
    protected Object determineCurrentLookupKey() {
        String lookUpTenantDataSource = (String) ThreadLocalContextUtil.getTenantId();
        return lookUpTenantDataSource;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

XML配置:

<beans:bean id="dataSource" class="com.mycompany.bean.RoutingDataSource">
        <beans:property name="targetDataSources">
            <beans:map>
                <beans:entry key="tenant1" value-ref="tenant1DataSource"/>
                <beans:entry key="tenant2" value-ref="tenant2DataSource"/>
            </beans:map>
        </beans:property>
    </beans:bean>
     <beans:bean id="tenant1DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />        
        <beans:property name="url" value="jdbc:mysql://localhost:3306/tenant1_si" />
        <beans:property name="username" value="xxxx" />
        <beans:property name="password" value="xxxx" />
    </beans:bean>

    <beans:bean id="tenant2DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />        
        <beans:property name="url" value="jdbc:mysql://localhost:3306/tenant2" />
        <beans:property name="username" value="xxxx" />
        <beans:property name="password" value="xxxx" />
    </beans:bean>

<beans:bean id = "myUserDetailsService" class = "com.mycompany.bean.MyUserDetailsService">
        <beans:property name="dataSource" ref="dataSource"/>
    </beans:bean>

   <sec:authentication-manager alias="authenticationManager">
        <sec:authentication-provider user-service-ref="myUserDetailsService">
            <sec:password-encoder hash="md5"/>
        </sec:authentication-provider>
    </sec:authentication-manager>

MyUserDetailsS​​ervice类:

public class MyUserDetailsService extends JdbcDaoImpl {


    private static final Logger logger = Logger.getLogger(MyUserDetailsService.class);

    private static final String COMPTE_EJB_LOOKUP_PATH = "java:comp/env/compteFacadeLocal";

    @EJB(name = "compteFacadeLocal", beanInterface = CompteFacadeLocal.class)
    private CompteFacadeLocal compteFacade;

    private SystemFacadeLocal systemFacade;



    @Override
    public UserDetails loadUserByUsername(String userName) {

        User user;
        Compte compte = getUser(userName);
        if (compte != null) {
            user = new User(compte);
            if (user.isEnabled()) {
                if (!user.isAccountNonExpired()) {
                    throw new AccountExpiredException("Ce compte a expiré");
                }
            } else {
                throw new DisabledException("Le compte de '" + userName + "' est désactivé !");
            }
        } else {
            throw new UsernameNotFoundException(userName + " not found");
        }
        return user;
    }

    private Compte getUser(String userName) {
        try {
            InitialContext initialContext = new InitialContext();
            initialContext.lookup(COMPTE_EJB_LOOKUP_PATH);

            compteFacade = (CompteFacadeLocal) initialContext.lookup(COMPTE_EJB_LOOKUP_PATH);

            return compteFacade.findCompteByLogin(userName);


        } catch (Exception ex) {

            logger.error("Could not lookup for EJB UtilisateurFacadeLocal with lookup path " + COMPTE_EJB_LOOKUP_PATH);
        }
        return null;
    }

0 个答案:

没有答案