仅限春季安全授权

时间:2013-07-17 17:56:26

标签: java spring spring-security waffle

我正在尝试使用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()没有返回我在登录表单上输入的正确用户名。 我工厂里有没有遗漏的东西?

1 个答案:

答案 0 :(得分:0)

您有两种选择:

    如果您使用Configure JdbcDaoImpl

    ,请将
  • provided DB schema作为UserDetailsService

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