Skip to content

Commit

Permalink
fix update user password failure bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed Jan 20, 2022
1 parent f4ae484 commit 6bedeba
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Apollo 2.0.0
* [Change Copy Right year to 2022](https://github.com/apolloconfig/apollo/pull/4202)
* [Allow disable apollo client cache](https://github.com/apolloconfig/apollo/pull/4199)
* [Make password check not hardcoded](https://github.com/apolloconfig/apollo/pull/4207)
* [Fix update user's password failure](https://github.com/apolloconfig/apollo/pull/4212)

------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.ctrip.framework.apollo.portal.entity.po;

import com.google.common.base.MoreObjects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* @author lepdou 2022-01-20
*/
@Entity
@Table(name = "Authorities")
public class Authority {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id")
private long id;
@Column(name = "Username", nullable = false)
private String username;
@Column(name = "Authority", nullable = false)
private String authority;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getAuthority() {
return authority;
}

public void setAuthority(String authority) {
this.authority = authority;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).omitNullValues().add("id", id)
.add("username", username)
.add("authority", authority).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.ctrip.framework.apollo.portal.repository;

import com.ctrip.framework.apollo.portal.entity.po.Authority;

import org.springframework.data.repository.PagingAndSortingRepository;

/**
* @author lepdou 2022-01-20
*/
public interface AuthorityRepository extends PagingAndSortingRepository<Authority, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.ctrip.framework.apollo.common.condition.ConditionalOnMissingProfile;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.repository.AuthorityRepository;
import com.ctrip.framework.apollo.portal.repository.UserRepository;
import com.ctrip.framework.apollo.portal.spi.LogoutHandler;
import com.ctrip.framework.apollo.portal.spi.SsoHeartbeatHandler;
Expand Down Expand Up @@ -137,9 +138,8 @@ public static JdbcUserDetailsManager jdbcUserDetailsManager(PasswordEncoder pass
@Bean
@ConditionalOnMissingBean(UserService.class)
public UserService springSecurityUserService(PasswordEncoder passwordEncoder,
JdbcUserDetailsManager userDetailsManager,
UserRepository userRepository) {
return new SpringSecurityUserService(passwordEncoder, userDetailsManager, userRepository);
UserRepository userRepository, AuthorityRepository authorityRepository) {
return new SpringSecurityUserService(passwordEncoder, userRepository, authorityRepository);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@

import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
import com.ctrip.framework.apollo.portal.entity.po.Authority;
import com.ctrip.framework.apollo.portal.entity.po.UserPO;
import com.ctrip.framework.apollo.portal.repository.AuthorityRepository;
import com.ctrip.framework.apollo.portal.repository.UserRepository;
import com.ctrip.framework.apollo.portal.spi.UserService;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.JdbcUserDetailsManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -41,41 +38,43 @@
*/
public class SpringSecurityUserService implements UserService {

private final List<GrantedAuthority> authorities = Collections
.unmodifiableList(Arrays.asList(new SimpleGrantedAuthority("ROLE_user")));

private final PasswordEncoder passwordEncoder;

private final JdbcUserDetailsManager userDetailsManager;

private final UserRepository userRepository;

private final AuthorityRepository authorityRepository;

public SpringSecurityUserService(
PasswordEncoder passwordEncoder,
JdbcUserDetailsManager userDetailsManager,
UserRepository userRepository) {
UserRepository userRepository,
AuthorityRepository authorityRepository) {
this.passwordEncoder = passwordEncoder;
this.userDetailsManager = userDetailsManager;
this.userRepository = userRepository;
this.authorityRepository = authorityRepository;
}

@Transactional
public void createOrUpdate(UserPO user) {
String username = user.getUsername();
String newPassword = passwordEncoder.encode(user.getPassword());

User userDetails = new User(username, passwordEncoder.encode(user.getPassword()), authorities);

if (userDetailsManager.userExists(username)) {
userDetailsManager.updateUser(userDetails);
UserPO managedUser = userRepository.findByUsername(username);
if (managedUser == null) {
user.setPassword(newPassword);
user.setEnabled(1);
userRepository.save(user);

//save authorities
Authority authority = new Authority();
authority.setUsername(username);
authority.setAuthority("ROLE_user");
authorityRepository.save(authority);
} else {
userDetailsManager.createUser(userDetails);
managedUser.setPassword(newPassword);
managedUser.setEmail(user.getEmail());
managedUser.setUserDisplayName(user.getUserDisplayName());
userRepository.save(managedUser);
}

UserPO managedUser = userRepository.findByUsername(username);
managedUser.setEmail(user.getEmail());
managedUser.setUserDisplayName(user.getUserDisplayName());

userRepository.save(managedUser);
}

@Override
Expand Down

0 comments on commit 6bedeba

Please sign in to comment.