gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:56:33 25 4
gpt4 key购买 nike

我正在研究 JAVA EE 技术,以学习 JSF-Spring-Hibernate 与...我正在尝试使用不同的用户角色进行网络应用程序。现在,我只有一个用户角色 ROLE_USER。我无法改变这个角色。我尝试了 Debug模式来了解我们如何设置这个角色,但我无法理解它发生在哪里。

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

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

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

安全配置.xml

<?xml version="1.0" encoding="UTF-8"?>

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

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

<bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userServiceImp" />
<property name="hideUserNotFoundExceptions" value="false" />
</bean>

<bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<constructor-arg>
<ref bean="daoAuthenticationProvider" />
</constructor-arg>
</bean>

这是我的 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));
}

Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("USER_ROLE"));
User userDetails = new User(user.getUserName(), user.getPassword(),
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 已硬编码在您的应用程序中。理想情况下,用户到角色的映射将来自数据库中的权限表。然后可以使用数据库查询检索此映射信息,然后将其添加到权限集合中。

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

protected List<GrantedAuthority> loadUserAuthorities(String username) {
List<GrantedAuthority> authorities = null;
Object[] params = { username };
authorities = authoritiesByUsernameMapping.execute(params); // Query to get Authorities
return authorities;
}

Spring Security DB schema

关于spring - 如何在Spring Security 3中设置用户角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24912882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com