【问题标题】:spring security authorization only仅弹簧安全授权
【发布时间】:2013-07-16 10:11:15
【问题描述】:

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

【问题讨论】:

    标签: java spring spring-security waffle


    【解决方案1】:

    你有两个选择:

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

    【讨论】:

      猜你喜欢
      • 2015-04-19
      • 2017-07-21
      • 2016-01-02
      • 2011-05-16
      • 2023-01-03
      • 2018-09-08
      • 1970-01-01
      • 2015-01-09
      • 2017-08-26
      相关资源
      最近更新 更多