我知道这已经回答了很多次,但我很困惑.我的应用程序中已经有一个身份验证机制,我只想使用Spring MVC的授权部分.我正在使用Spring MVC 3和Spring Security 3.
当我在互联网上搜索时,我找到了两个解决方案,第一个是实现AuthenticationProvider接口.例1.第二个是实现UserDetails和UserDetailsService,Example2所以我迷失在这里.
---- ----更新
问题的第二部分在这里.以及解决方法的解决方案.
Nil*_*ils 37
在大多数情况下,仅使用用户名和密码进行身份验证和角色授权时,实现自己的UserDetailsService就足够了.
用户名密码认证的流程一般如下:
因此,如果DaoAuthenticationProvider中的验证符合您的需求.然后,您只需实现自己的UserDetailsService并调整DaoAuthenticationProvider的验证.
使用spring 3.1的UserDetailsService的示例如下:
Spring XML:
<security:authentication-manager>
<security:authentication-provider user-service-ref="myUserDetailsService" />
</security:authentication-manager>
<bean name="myUserDetailsService" class="x.y.MyUserDetailsService" />
Run Code Online (Sandbox Code Playgroud)
UserDetailsService实现:
public MyUserDetailsService implements UserDetailsService {
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//Retrieve the user from wherever you store it, e.g. a database
MyUserClass user = ...;
if (user == null) {
throw new UsernameNotFoundException("Invalid username/password.");
}
Collection<? extends GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("Role1","role2","role3");
return new User(user.getUsername(), user.getPassword(), authorities);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30445 次 |
| 最近记录: |