【问题标题】:Cntrolling Access using Spring Security3.1使用 Spring Security 3.1 控制访问
【发布时间】:2013-11-25 14:24:27
【问题描述】:

在我的项目中,我使用的是 spring security3.1,我附上我的 spring security 文件:

 <beans:beans xmlns="http://www.springframework.org/schema/security"
 xmlns:beans="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http auto-config="true">
    <intercept-url pattern="/login" access="ROLE_ADMIN" />
    <form-login login-page="/login" default-target-url="/welcome"
        authentication-failure-url="/loginfailed" />
    <logout logout-success-url="/logout" />
</http>

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query="SELECT user_name,user_password,account_status FROM systemuser WHERE user_name=?"
            authorities-by-username-query="SELECT user_name,authority FROM systemuser WHERE user_name=?"/>
    </authentication-provider>
</authentication-manager>
   </beans:beans>

它工作正常。现在我的要求是我想自定义用户的访问权限。例如我有 3 个锚标记如下:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 <table>
 <tr>
 <td><a href="${pageContext.request.contextPath}/manageUsers" id="user_link">Manage  Users</a></td>
<td><a href="${pageContext.request.contextPath}/allcontact">Manage Contact</a></td>
<td><a href="<c:url value="/j_spring_security_logout" />" > Logout</a></td>
</tr>

现在,我希望管理员可以访问所有这 3 个选项卡(或链接),将他们带到相应的页面,但 ROLE_USER(普通用户)将无法访问'管理用户" 选项卡。所以我的意思是当具有权限 ROLE_ADMIN 的用户登录时,所有 3 个链接都将可见,但是当具有权限的用户 "ROLE_USER" 登录时只有 2 个链接,即 “管理联系人”和“注销” 将可见。

我该如何实现这个任何人都可以建议我????????

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    我遇到了这个问题,我通过实现自己的 AuthentificationProvider 解决了它。

    为此,您需要完成几个步骤:

    1 实现 GrantedAuthority 接口

    public class UserAccessRole implements GrantedAuthority {
    
        private String accessRole;
    
        public UserAccessRole(String ar) {
            accessRole = ar;
        }
    
        public UserAccessRole(AdminUserRoleEnum ar) {
            accessRole = ar.toString();
        }
    
        public String getAuthority() {
            return accessRole;
        }
    
    }
    

    AccessRole 是您系统中用户角色的名称。

    2 实现您的 UserDetailService。这是您的身份验证提供程序。在这里您应该从数据库中检索用户(!重要提示:您应该在数据库中添加用户角色并使用用户检索它)。 AdminUser 这是我的 DAO 类(你可以随意命名)。请注意,用户类是 spring-security 框架的一部分。您在此处创建的用户返回到 spring-security 并完成身份验证过程。

    import org.springframework.security.core.userdetails.User;
    
    public class UserAuthenticationProvider implements UserDetailsService {
    
        @Resource(name = "adminUserDAO")
        AdminUserDAO adminDAO;
    
        public UserDetails loadUserByUsername(String name)
                throws UsernameNotFoundException, DataAccessException {
            AdminUser admin = adminDAO.getByName(name);
            User user = null;
    
            if (admin != null) {
                Set<UserAccessRole> roles = new HashSet<UserAccessRole>();
                roles.add(new UserAccessRole(admin.getRole()));
    
                user = new User(
                        admin.getName(),
                        admin.getPassword(),
                        true,
                        true,
                        true,
                        true,
                        roles
                );
            }
    
            return user;
        }
    
    }
    

    3 在您的控制器中的某处使用此代码进行用户重定向。用户将收到页面取决于它的角色:

    Collection<? extends GrantedAuthority> name =   SecurityContextHolder.getContext().getAuthentication().getAuthorities();
    UserAccessRole role = (UserAccessRole) name.iterator().next();
    if (role.getAuthority().equals(AdminUserRoleEnum.ADMIN_USER.toString())) {
       return "redirect:/admin_user_page/";
    }
    if (role.getAuthority().equals(AdminUserRoleEnum.USER.toString())) {
       return "redirect:/user_page";
    }
    

    4 最后配置spring-security:

       <beans:bean id="userAuthentificationProvider"
                    class="path_to.UserAuthentificationProvider">
        </beans:bean> 
    
       <authentication-manager>
            <authentication-provider user-service-ref="userAuthentificationProvider">
            </authentication-provider>
        </authentication-manager>
    

    【讨论】:

    • 我正在使用数据库从您的示例中检索用户名、权限和密码,您似乎没有将凭据存储在使用基于简单表单的身份验证的数据库中。请给出您的“userAuthentificationProvider”bean 的代码
    • 是的,我在第 2 步中写了这个。
    猜你喜欢
    • 2014-10-15
    • 2015-04-05
    • 2012-04-19
    • 1970-01-01
    • 2011-07-15
    • 2016-09-30
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多