如何在Spring Security 3中设置用户角色?

我正在研究JAVA EE技术,以学习JSF-Spring-Hibernate vs …
我正在尝试使用不同的用户角色来执行Web应用程序.现在,我只有一个用户角色ROLE_USER.我无法改变这个角色.我尝试使用debug mod来理解我们如何设置这个角色,但我无法理解它在哪里发生.

所以,我的主要问题是我无法在我的应用程序中创建任何其他ROLE.如果我不使用< secured attributes =“ROLE_USER”/>在我的流程中(我正在使用spring web-flow)每个人都可以访问该页面.如果我只做ROLE_USER.但我想创建一个名为ROLE_ADMIN的新角色,只让ROLE_ADMIN到达该页面.

注意:我没有在这里添加所有文件因为我说只添加相关的类和文件.如果您需要更多信息,请告诉我.

这是我正在遵循的教程soruce代码.
https://code.google.com/p/jee-tutorial-youtube/source/browse/

安全-config.xml中

ecurity:http auto-config="true">
    ecurity:form-login login-page="/app/main"
        default-target-url="/app/account" />
    ecurity:logout logout-url="/app/logout"
        logout-success-url="/app/main" />
ecurity:http>

ecurity:authentication-manager>
    ecurity:authentication-provider
        user-service-ref="userServiceImp">
        ecurity:password-encoder hash="md5" />
    ecurity:authentication-provider>
ecurity:authentication-manager>

ecurity.authentication.dao.DaoAuthenticationProvider">
    ecurity.authentication.ProviderManager">
    

这是我的UserAuthenticationProviderServiceImp类,我在其中验证用户身份.

 public boolean processUserAuthentication(Users user) {

        try {
                Authentication request = new UsernamePasswordAuthenticationToken(user.getUserName(),user.getpassword());
                Authentication result = authenticationManager.authenticate(request);
                SecurityContextHolder.getContext().setAuthentication(result);

                return true;
        } catch(AuthenticationException e) {
                FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,e.getMessage(),"Sorry!"));

                return false;
        }
}

我发现这个函数,即我的Service类,在processUserAuthentication中调用.然后我认为这是设置角色的函数.

public UserDetails loadUserByUsername(String userName)
        throws UsernameNotFoundException {

    Users user = userDao.loadUserByUserName(userName);

    if (user == null) {
        throw new UsernameNotFoundException(String.format(
                getMessageBundle().getString("badCredentials"),userName));
    }

    Collectiongetpassword(),authorities);
    return userDetails;
}

更新:

在我的项目中,我必须使用此语法来创建角色:ROLE_X其中x是任何字符串.例;我们可以拥有ROLE_MYNAME,但我们不能将MYNAME或MYNAME_ROLE作为角色.它必须从ROLE_开始.我仍在努力找到这个问题.我会更新如果我找到了任何答案.

编辑:
一个详细的解释,为什么我们必须在这页面中使用ROLE_;
http://bluefoot.info/howtos/spring-security-adding-a-custom-role-prefix/

最佳答案
现在USER_ROLE在您的应用程序中是硬编码的.理想情况下,用户到角色映射将来自数据库中的“权限”表.然后,可以使用数据库查询检索此映射信息,然后将其添加到authority集合中.

authorities.add(loadUserAuthorities(user.getUsername()));

protected Listarams = { username };
    authorities = authoritiesByUsernameMapping.execute(params); // Query to get Authorities
    return authorities;      
}

Spring Security DB schema

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...