2011-12-31 66 views
2

我写了春季安全applictionContext-security.xml文件下面的代码:如何检查春季安全多个表进行身份验证

<?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> 

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

回答

5

如果您在Spring Security schema definition中搜索身份验证提供程序,您将看到它具有无限最大值。因此,您可以为需要在</authentication-manager>内访问的每个表格添加一个<authentication-provider>,例如,

<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> 
相关问题