1
在我的web應用程序時,我有一種類型的用戶(typical_user)我做了以下內容:如何爲幾種類型的用戶實現UserDetailsService?
1)實施UserDetailsService
public class UserServiceImpl implements UserService, UserDetailsService {
private UserDao userDao;
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException
{
UserEntity user = userDao.loadUserByEmail(username);
if (user == null) {
throw new UsernameNotFoundException(String.format(
getMessageBundle().getString("badCredentials"), username));
}
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
User userDetails = new User(user.getEmail(), user.getPassword(),
authorities);
return userDetails;
}}
2)security-config.xml
這樣寫的配置對於用戶:
<security:authentication-manager>
<security:authentication-provider
user-service-ref="userService">
<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="userService" />
<property name="hideUserNotFoundExceptions" value="false" />
</bean>
但現在我想擁有另一種類型的用戶(管理員)。所以,我需要另一個執行loadUserByUsername
方法(其中用戶將得到ROLE_ADMIN
)。
我可以寫另一個班級(AdminServiceImpl
),但我的security-config.xml
將如何看起來像?
將角色存儲在數據庫中。 –