我正在嘗試開發一個用戶管理工具,使用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()沒有返回我在登錄表單中輸入的正確用戶名。 有沒有我從工廠班缺少的東西?