end*_*unc 6 authentication spring roles spring-security spring-webflow-2
我正在研究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中
<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
这是我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
我发现这个函数,即我的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;
}
Run Code Online (Sandbox Code Playgroud)
更新:
在我的项目中,我必须使用此语法来创建角色:ROLE_X其中x是任何字符串.例; 我们可以拥有ROLE_MYNAME,但我们不能将MYNAME或MYNAME_ROLE作为角色.它必须从ROLE_开始.我仍在努力找到这个问题.我会更新如果我找到了任何答案.
编辑:有一个详细的解释,为什么我们必须在这个页面中使用ROLE_; http://bluefoot.info/howtos/spring-security-adding-a-custom-role-prefix/
小智 0
现在 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12974 次 |
| 最近记录: |