(老问题!对新用户的回应)当您在问题中标记 SHIRO 时,您可以通过扩展和 @Override 添加自己的逻辑
org.apache.shiro.realm.jdbc.JdbcRealm
:
getRoleNamesForUser(...) , getPermissions(..), doGetAuthenticationInfo(..)
这是一个示例:
@Override
protected Set<String> getRoleNamesForUser(Connection conn, String username) throws SQLException {
Set<String> roleNames = new LinkedHashSet<>();
Collection<UserRole> roles = /* Get roles from your DB, this example use JPA entity, **but you put here any logic you want**...*/
for(UserRole userRole:roles){
roleNames.add(userRole.getRole().getName());
}
return roleNames; // return roles so Shiro is 'aware' of roles to add them to current user
}
*请注意,相同的逻辑适用于您覆盖的其他方法。
** 您不需要 2 个 http 调用来记录用户,您只需使用 Shiro 编程身份验证即可。
这是启用了 Shiro 注释的完整示例..还有更多