I have a custom Authenticat开发者_Python百科ionProvider
that simply returns the Authentication
object for the authenticate
method. What I want to do is add a role to the user when they log in. This is for demo purposes, so all I want is the user to enter a username and let them in. I need to assign them the admin role.
There are, of course, several ways to achieve that.
My preferred one is do this in a custom UserDetailsService
. The only method is loadUserByUsername
, that will return an instance of UserDetails
. When you are constructing your UserDetails
, you can add whatever GrantedAuthority
you want.
So first, you'll declare your custom UserDetailsService
in your application context configuration file:
<bean id="myCustomUDS" class="com.myapp.AppUDS" />
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider user-service-ref="myCustomUDS">
</sec:authentication-provider>
</sec:authentication-manager>
Then you write the class itself:
public class AppUDS implements UserDetailsService {
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
//create your concrete UserDetails
//add your custom role (i.e. GrantedAuthority) to that object (that will be added to all users)
//return it
}
}
精彩评论