2013-07-17 87 views
0

我正在尝试开发一个用户管理工具,使用Waffle与Spring Security一起执行Windows身份验证。不幸的是,唯一提供给我的是验证部分。只有弹簧安全授权

我想为特定用户会话分配角色以限制用户权限。每个用户名都与其关联的角色一起存储在数据库中。我如何让Spring Security查询我的数据库并将相关角色加载到会话中,以便我可以在控制器中使用@PreAuthorize(hasRole(role))注释来限制对某些操作的访问?

编辑: 感谢您的回答,但我不认为这就是我所期待的。 所以我取得了一些进展(我认为)。对于我的身份验证提供我创建自己的自定义GrantedAuthorityFactory作为我waffleSpringAuthenticationProvider的属性如下:

<bean id="waffleSpringAuthenticationProvider" class="waffle.spring.WindowsAuthenticationProvider"> 
    <property name="AllowGuestLogin" value="false" /> 
    <property name="PrincipalFormat" value="fqn" /> 
    <property name="RoleFormat" value="both" /> 
    <property name="AuthProvider" ref="waffleWindowsAuthProvider" /> 
    <property name="grantedAuthorityFactory" ref ="simpleGrantedAuthorityFactory"/> 
    <!-- --> 

</bean> 

的grantedAuthorityFactory代码如下:

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 
import org.springframework.security.core.GrantedAuthority; 

import waffle.spring.GrantedAuthorityFactory; 
import waffle.windows.auth.WindowsAccount; 

public class SimpleGrantedAuthorityFactory implements GrantedAuthorityFactory{ 

    private final String PREFIX; 
    private final boolean CONVERT_TO_UPPER_CASE; 

    @Autowired 
    @Qualifier(value = "jdbcRoleDao") 
    private JdbcRoleDao jdbcRoleDao; 


    public SimpleGrantedAuthorityFactory(String prefix, boolean convertToUpperCase) 
    { 
     PREFIX = prefix; 
     CONVERT_TO_UPPER_CASE = convertToUpperCase; 
    } 

    @Override 
    public GrantedAuthority createGrantedAuthority(WindowsAccount windowsAccount) { 

     System.out.println("Username: "+windowsAccount.getFqn()); 
     String grantedAuthorityString = windowsAccount.getFqn(); 

     String grantedAuthority = jdbcRoleDao.getRole(grantedAuthorityString); 
     return new SimpleGrantedAuthority(PREFIX+grantedAuthority); 
    } 

} 

现在,当我运行该程序,并尝试登录后,登录失败。当我从配置文件中删除自定义工厂属性时,登录成功完成,没有分配角色。我不确定这是否重要,但windowsAccount.getFqn()没有返回我在登录表单中输入的正确用户名。 有没有我从工厂班缺少的东西?

回答

0

你有两个选择:

  • Configure JdbcDaoImpl为您UserDetailsService如果你使用provided DB schema

    <authentication-manager> 
        <authentication-provider> 
         <jdbc-user-service data-source-ref="yourDataSource"> 
        </authentication-provider> 
    </authentication-manager> 
    
  • 编写和配置自己的UserDetailsService如果使用自定义数据库架构。

    <authentication-manager> 
        <authentication-provider user-service-ref="idOfYourCustomUserDetailsService" /> 
    </authentication-manager>