如何在spring security中检查多个表以进行身份​​验证

sud*_*udo 2 java database spring spring-security

我在applictionContext-Security.xml中为spring安全性编写了以下代码:

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

<global-method-security secured-annotations="enabled">
</global-method-security>

<!-- URL pattern based security -->


<http auto-config="true">
    <intercept-url pattern="/common/*" access="ROLE_ADMIN" />
     <form-login login-page="/login/login.jsp"/>
    <logout logout-success-url="/home/index.jsp"/>
</http>

<authentication-manager>
    <authentication-provider>

        <!-- <password-encoder hash="md5"/> <user-service> <user name="admin" 
            password="65dc70650690999922d7dcd99dbd4033" authorities="ROLE_ADMIN"/> </user-service> -->

        <jdbc-user-service data-source-ref="dataSource"
            authorities-by-username-query="
            select admin.userName as username, auth.authority as authority from Administrators admin, Authorities auth
            where admin.id=auth.AdministratorId and admin.userName= ?"
            users-by-username-query="select userName as username,hashedPassword as password, enabled as enabled
                                        from Administrators where userName= ?" />

    </authentication-provider>
</authentication-manager>

</beans:beans>
Run Code Online (Sandbox Code Playgroud)

在那个xml文件中,我只能从数据库中检查角色的管理员表.如何检查内部用户,管理员和外部用户表等其他表以检查角色?我是否需要在新类中编写查询而不是在xml中编写.请给我任何想法或建议,因为我刚开始这样做.给我任何链接或网站或代码来查看.谢谢

mat*_*sev 5

如果在Spring Security 架构定义中搜索authentication-provider,您将看到它具有无限制的最大出现次数.因此,您可以为每个需要在其中访问的表添加一个,例如<authentication-provider></authentication-manager>

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
            authorities-by-username-query="select [...] from Administrators" 
        .../>
    </authentication-provider>

    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
            authorities-by-username-query="select [...] from Users" 
        .../>
    </authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)