繁体   English   中英

仅春季安全授权

[英]spring security authorization only

我正在尝试使用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>

grantAuthorityFactory代码如下:

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()不会返回我在登录表单中输入的正确用户名。 我的工厂班上缺少什么吗?

您有两种选择:

  • 如果使用提供的数据库模式,请将JdbcDaoImpl配置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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM