Class JdbcUserDetailsManager

  • All Implemented Interfaces:
    org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.MessageSourceAware, UserDetailsService, GroupManager, UserDetailsManager

    public class JdbcUserDetailsManager
    extends JdbcDaoImpl
    implements UserDetailsManager, GroupManager
    Jdbc user management service, based on the same table structure as its parent class, JdbcDaoImpl.

    Provides CRUD operations for both users and groups. Note that if the enableAuthorities property is set to false, calls to createUser, updateUser and deleteUser will not store the authorities from the UserDetails or delete authorities for the user. Since this class cannot differentiate between authorities which were loaded for an individual or for a group of which the individual is a member, it's important that you take this into account when using this implementation for managing your users.

    Since:
    2.0
    • Field Detail

      • DEF_CREATE_USER_SQL

        public static final java.lang.String DEF_CREATE_USER_SQL
        See Also:
        Constant Field Values
      • DEF_DELETE_USER_SQL

        public static final java.lang.String DEF_DELETE_USER_SQL
        See Also:
        Constant Field Values
      • DEF_UPDATE_USER_SQL

        public static final java.lang.String DEF_UPDATE_USER_SQL
        See Also:
        Constant Field Values
      • DEF_INSERT_AUTHORITY_SQL

        public static final java.lang.String DEF_INSERT_AUTHORITY_SQL
        See Also:
        Constant Field Values
      • DEF_DELETE_USER_AUTHORITIES_SQL

        public static final java.lang.String DEF_DELETE_USER_AUTHORITIES_SQL
        See Also:
        Constant Field Values
      • DEF_USER_EXISTS_SQL

        public static final java.lang.String DEF_USER_EXISTS_SQL
        See Also:
        Constant Field Values
      • DEF_CHANGE_PASSWORD_SQL

        public static final java.lang.String DEF_CHANGE_PASSWORD_SQL
        See Also:
        Constant Field Values
      • DEF_FIND_GROUPS_SQL

        public static final java.lang.String DEF_FIND_GROUPS_SQL
        See Also:
        Constant Field Values
      • DEF_FIND_USERS_IN_GROUP_SQL

        public static final java.lang.String DEF_FIND_USERS_IN_GROUP_SQL
        See Also:
        Constant Field Values
      • DEF_INSERT_GROUP_SQL

        public static final java.lang.String DEF_INSERT_GROUP_SQL
        See Also:
        Constant Field Values
      • DEF_FIND_GROUP_ID_SQL

        public static final java.lang.String DEF_FIND_GROUP_ID_SQL
        See Also:
        Constant Field Values
      • DEF_INSERT_GROUP_AUTHORITY_SQL

        public static final java.lang.String DEF_INSERT_GROUP_AUTHORITY_SQL
        See Also:
        Constant Field Values
      • DEF_DELETE_GROUP_SQL

        public static final java.lang.String DEF_DELETE_GROUP_SQL
        See Also:
        Constant Field Values
      • DEF_DELETE_GROUP_AUTHORITIES_SQL

        public static final java.lang.String DEF_DELETE_GROUP_AUTHORITIES_SQL
        See Also:
        Constant Field Values
      • DEF_DELETE_GROUP_MEMBERS_SQL

        public static final java.lang.String DEF_DELETE_GROUP_MEMBERS_SQL
        See Also:
        Constant Field Values
      • DEF_RENAME_GROUP_SQL

        public static final java.lang.String DEF_RENAME_GROUP_SQL
        See Also:
        Constant Field Values
      • DEF_INSERT_GROUP_MEMBER_SQL

        public static final java.lang.String DEF_INSERT_GROUP_MEMBER_SQL
        See Also:
        Constant Field Values
      • DEF_DELETE_GROUP_MEMBER_SQL

        public static final java.lang.String DEF_DELETE_GROUP_MEMBER_SQL
        See Also:
        Constant Field Values
      • DEF_GROUP_AUTHORITIES_QUERY_SQL

        public static final java.lang.String DEF_GROUP_AUTHORITIES_QUERY_SQL
        See Also:
        Constant Field Values
      • DEF_DELETE_GROUP_AUTHORITY_SQL

        public static final java.lang.String DEF_DELETE_GROUP_AUTHORITY_SQL
        See Also:
        Constant Field Values
      • logger

        protected final org.apache.commons.logging.Log logger
    • Constructor Detail

      • JdbcUserDetailsManager

        public JdbcUserDetailsManager()
      • JdbcUserDetailsManager

        public JdbcUserDetailsManager​(javax.sql.DataSource dataSource)
    • Method Detail

      • initDao

        protected void initDao()
                        throws org.springframework.context.ApplicationContextException
        Overrides:
        initDao in class JdbcDaoImpl
        Throws:
        org.springframework.context.ApplicationContextException
      • loadUsersByUsername

        protected java.util.List<UserDetails> loadUsersByUsername​(java.lang.String username)
        Executes the SQL usersByUsernameQuery and returns a list of UserDetails objects. There should normally only be one matching user.
        Overrides:
        loadUsersByUsername in class JdbcDaoImpl
      • deleteUser

        public void deleteUser​(java.lang.String username)
        Description copied from interface: UserDetailsManager
        Remove the user with the given login name from the system.
        Specified by:
        deleteUser in interface UserDetailsManager
      • changePassword

        public void changePassword​(java.lang.String oldPassword,
                                   java.lang.String newPassword)
                            throws AuthenticationException
        Description copied from interface: UserDetailsManager
        Modify the current user's password. This should change the user's password in the persistent user repository (datbase, LDAP etc).
        Specified by:
        changePassword in interface UserDetailsManager
        Parameters:
        oldPassword - current password (for re-authentication if required)
        newPassword - the password to change to
        Throws:
        AuthenticationException
      • userExists

        public boolean userExists​(java.lang.String username)
        Description copied from interface: UserDetailsManager
        Check if a user with the supplied login name exists in the system.
        Specified by:
        userExists in interface UserDetailsManager
      • findAllGroups

        public java.util.List<java.lang.String> findAllGroups()
        Description copied from interface: GroupManager
        Returns the names of all groups that this group manager controls.
        Specified by:
        findAllGroups in interface GroupManager
      • findUsersInGroup

        public java.util.List<java.lang.String> findUsersInGroup​(java.lang.String groupName)
        Description copied from interface: GroupManager
        Locates the users who are members of a group
        Specified by:
        findUsersInGroup in interface GroupManager
        Parameters:
        groupName - the group whose members are required
        Returns:
        the usernames of the group members
      • createGroup

        public void createGroup​(java.lang.String groupName,
                                java.util.List<GrantedAuthority> authorities)
        Description copied from interface: GroupManager
        Creates a new group with the specified list of authorities.
        Specified by:
        createGroup in interface GroupManager
        Parameters:
        groupName - the name for the new group
        authorities - the authorities which are to be allocated to this group.
      • deleteGroup

        public void deleteGroup​(java.lang.String groupName)
        Description copied from interface: GroupManager
        Removes a group, including all members and authorities.
        Specified by:
        deleteGroup in interface GroupManager
        Parameters:
        groupName - the group to remove.
      • renameGroup

        public void renameGroup​(java.lang.String oldName,
                                java.lang.String newName)
        Description copied from interface: GroupManager
        Changes the name of a group without altering the assigned authorities or members.
        Specified by:
        renameGroup in interface GroupManager
      • addUserToGroup

        public void addUserToGroup​(java.lang.String username,
                                   java.lang.String groupName)
        Description copied from interface: GroupManager
        Makes a user a member of a particular group.
        Specified by:
        addUserToGroup in interface GroupManager
        Parameters:
        username - the user to be given membership.
        groupName - the name of the group to which the user will be added.
      • removeUserFromGroup

        public void removeUserFromGroup​(java.lang.String username,
                                        java.lang.String groupName)
        Description copied from interface: GroupManager
        Deletes a user's membership of a group.
        Specified by:
        removeUserFromGroup in interface GroupManager
        Parameters:
        username - the user
        groupName - the group to remove them from
      • setAuthenticationManager

        public void setAuthenticationManager​(AuthenticationManager authenticationManager)
      • setCreateUserSql

        public void setCreateUserSql​(java.lang.String createUserSql)
      • setDeleteUserSql

        public void setDeleteUserSql​(java.lang.String deleteUserSql)
      • setUpdateUserSql

        public void setUpdateUserSql​(java.lang.String updateUserSql)
      • setCreateAuthoritySql

        public void setCreateAuthoritySql​(java.lang.String createAuthoritySql)
      • setDeleteUserAuthoritiesSql

        public void setDeleteUserAuthoritiesSql​(java.lang.String deleteUserAuthoritiesSql)
      • setUserExistsSql

        public void setUserExistsSql​(java.lang.String userExistsSql)
      • setChangePasswordSql

        public void setChangePasswordSql​(java.lang.String changePasswordSql)
      • setFindAllGroupsSql

        public void setFindAllGroupsSql​(java.lang.String findAllGroupsSql)
      • setFindUsersInGroupSql

        public void setFindUsersInGroupSql​(java.lang.String findUsersInGroupSql)
      • setInsertGroupSql

        public void setInsertGroupSql​(java.lang.String insertGroupSql)
      • setFindGroupIdSql

        public void setFindGroupIdSql​(java.lang.String findGroupIdSql)
      • setInsertGroupAuthoritySql

        public void setInsertGroupAuthoritySql​(java.lang.String insertGroupAuthoritySql)
      • setDeleteGroupSql

        public void setDeleteGroupSql​(java.lang.String deleteGroupSql)
      • setDeleteGroupAuthoritiesSql

        public void setDeleteGroupAuthoritiesSql​(java.lang.String deleteGroupAuthoritiesSql)
      • setDeleteGroupMembersSql

        public void setDeleteGroupMembersSql​(java.lang.String deleteGroupMembersSql)
      • setRenameGroupSql

        public void setRenameGroupSql​(java.lang.String renameGroupSql)
      • setInsertGroupMemberSql

        public void setInsertGroupMemberSql​(java.lang.String insertGroupMemberSql)
      • setDeleteGroupMemberSql

        public void setDeleteGroupMemberSql​(java.lang.String deleteGroupMemberSql)
      • setGroupAuthoritiesSql

        public void setGroupAuthoritiesSql​(java.lang.String groupAuthoritiesSql)
      • setDeleteGroupAuthoritySql

        public void setDeleteGroupAuthoritySql​(java.lang.String deleteGroupAuthoritySql)
      • setUserCache

        public void setUserCache​(UserCache userCache)
        Optionally sets the UserCache if one is in use in the application. This allows the user to be removed from the cache after updates have taken place to avoid stale data.
        Parameters:
        userCache - the cache used by the AuthenticationManager.