Ext id_token user profile
parent
d36447bb54
commit
101b97ff11
|
@ -1,6 +1,7 @@
|
||||||
package com.monkeyk.sos.domain.oauth;
|
package com.monkeyk.sos.domain.oauth;
|
||||||
|
|
||||||
import com.monkeyk.sos.domain.shared.GuidGenerator;
|
import com.monkeyk.sos.domain.shared.GuidGenerator;
|
||||||
|
import com.monkeyk.sos.domain.user.User;
|
||||||
import com.monkeyk.sos.domain.user.UserRepository;
|
import com.monkeyk.sos.domain.user.UserRepository;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -53,22 +54,25 @@ public class ClaimsOAuth2TokenCustomizer implements OAuth2TokenCustomizer<JwtEnc
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// String username = authorization.getPrincipalName();
|
String username = authorization.getPrincipalName();
|
||||||
|
User user = userRepository.findProfileByUsername(username);
|
||||||
|
boolean nullUser = (user == null);
|
||||||
|
|
||||||
Set<String> scopes = context.getAuthorizedScopes();
|
Set<String> scopes = context.getAuthorizedScopes();
|
||||||
if (scopes.contains(OidcScopes.ADDRESS)) {
|
if (scopes.contains(OidcScopes.ADDRESS)) {
|
||||||
Object attrVal = authorization.getAttribute(OidcScopes.ADDRESS);
|
String attrVal = nullUser ? null : user.address();
|
||||||
claims.claim(OidcScopes.ADDRESS, attrVal == null ? "" : attrVal);
|
claims.claim(OidcScopes.ADDRESS, attrVal == null ? "" : attrVal);
|
||||||
}
|
}
|
||||||
if (scopes.contains(OidcScopes.EMAIL)) {
|
if (scopes.contains(OidcScopes.EMAIL)) {
|
||||||
Object attrVal = authorization.getAttribute(OidcScopes.EMAIL);
|
String attrVal = nullUser ? null : user.email();
|
||||||
claims.claim(OidcScopes.EMAIL, attrVal == null ? "" : attrVal);
|
claims.claim(OidcScopes.EMAIL, attrVal == null ? "" : attrVal);
|
||||||
}
|
}
|
||||||
if (scopes.contains(OidcScopes.PHONE)) {
|
if (scopes.contains(OidcScopes.PHONE)) {
|
||||||
Object attrVal = authorization.getAttribute(OidcScopes.PHONE);
|
String attrVal = nullUser ? null : user.phone();
|
||||||
claims.claim(OidcScopes.PHONE, attrVal == null ? "" : attrVal);
|
claims.claim(OidcScopes.PHONE, attrVal == null ? "" : attrVal);
|
||||||
}
|
}
|
||||||
if (scopes.contains(OidcScopes.PROFILE)) {
|
if (scopes.contains(OidcScopes.PROFILE)) {
|
||||||
Object attrVal = authorization.getAttribute("nickname");
|
String attrVal = nullUser ? null : user.nickname();
|
||||||
claims.claim("nickname", attrVal == null ? "" : attrVal);
|
claims.claim("nickname", attrVal == null ? "" : attrVal);
|
||||||
claims.claim("updated_at", "");
|
claims.claim("updated_at", "");
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,16 @@ public interface UserRepository extends Repository {
|
||||||
|
|
||||||
User findByUsername(String username);
|
User findByUsername(String username);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询 User 的 各类 profile 基础数据
|
||||||
|
* 包括 phone, email, address, nickname, updated_at
|
||||||
|
*
|
||||||
|
* @param username username
|
||||||
|
* @return User only have profile fields
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
User findProfileByUsername(String username);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注意:产品化的设计此处应该有分页会更好
|
* 注意:产品化的设计此处应该有分页会更好
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.monkeyk.sos.infrastructure.jdbc;
|
||||||
|
|
||||||
|
import com.monkeyk.sos.domain.user.User;
|
||||||
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* table: user_
|
||||||
|
* 2023/10/17
|
||||||
|
*
|
||||||
|
* @author Shengzhao Li
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
public class UserProfileRowMapper implements RowMapper<User> {
|
||||||
|
|
||||||
|
|
||||||
|
public UserProfileRowMapper() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User mapRow(ResultSet rs, int i) throws SQLException {
|
||||||
|
User user = new User();
|
||||||
|
|
||||||
|
user.id(rs.getInt("id"));
|
||||||
|
user.guid(rs.getString("guid"));
|
||||||
|
|
||||||
|
user.archived(rs.getBoolean("archived"));
|
||||||
|
user.createTime(rs.getTimestamp("create_time").toLocalDateTime());
|
||||||
|
|
||||||
|
user.email(rs.getString("email"));
|
||||||
|
user.phone(rs.getString("phone"));
|
||||||
|
user.username(rs.getString("username"));
|
||||||
|
|
||||||
|
user.address(rs.getString("address"));
|
||||||
|
user.nickname(rs.getString("nickname"));
|
||||||
|
user.enabled(rs.getBoolean("enabled"));
|
||||||
|
user.updatedAt(rs.getLong("updated_at"));
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,6 +15,8 @@ import com.monkeyk.sos.domain.user.Privilege;
|
||||||
import com.monkeyk.sos.domain.user.User;
|
import com.monkeyk.sos.domain.user.User;
|
||||||
import com.monkeyk.sos.domain.user.UserRepository;
|
import com.monkeyk.sos.domain.user.UserRepository;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
@ -33,9 +35,15 @@ import java.util.stream.Collectors;
|
||||||
@Repository("userRepositoryJdbc")
|
@Repository("userRepositoryJdbc")
|
||||||
public class UserRepositoryJdbc implements UserRepository {
|
public class UserRepositoryJdbc implements UserRepository {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(UserRepositoryJdbc.class);
|
||||||
|
|
||||||
private final UserRowMapper userRowMapper = new UserRowMapper();
|
private final UserRowMapper userRowMapper = new UserRowMapper();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
private final UserProfileRowMapper userProfileRowMapper = new UserProfileRowMapper();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private JdbcTemplate jdbcTemplate;
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
@ -127,6 +135,28 @@ public class UserRepositoryJdbc implements UserRepository {
|
||||||
user = list.get(0);
|
user = list.get(0);
|
||||||
user.privileges().addAll(findPrivileges(user.id()));
|
user.privileges().addAll(findPrivileges(user.id()));
|
||||||
}
|
}
|
||||||
|
if (list.size() > 1) {
|
||||||
|
LOG.warn("Found {} user(s) by username: {}, checking duplicate data??", list.size(), username);
|
||||||
|
}
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public User findProfileByUsername(String username) {
|
||||||
|
final String sql = " select id, guid,create_time,archived, username,enabled,phone,email,address,nickname,updated_at from user_ where username = ? and archived = 0 ";
|
||||||
|
final List<User> list = this.jdbcTemplate.query(sql, userProfileRowMapper, username);
|
||||||
|
|
||||||
|
User user = null;
|
||||||
|
if (!list.isEmpty()) {
|
||||||
|
user = list.get(0);
|
||||||
|
}
|
||||||
|
if (list.size() > 1) {
|
||||||
|
LOG.warn("Found {} user profiles by username: {}, checking duplicate data??", list.size(), username);
|
||||||
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,10 @@ public class UserServiceImpl implements UserService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提示:产品化的设计此处应加上缓存提高性能
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
// @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
|
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
User user = userRepository.findByUsername(username);
|
User user = userRepository.findByUsername(username);
|
||||||
if (user == null || user.archived()) {
|
if (user == null || user.archived()) {
|
||||||
|
@ -45,7 +47,6 @@ public class UserServiceImpl implements UserService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
// @Transactional(readOnly = true)
|
|
||||||
public UserJsonDto loadCurrentUserJsonDto() {
|
public UserJsonDto loadCurrentUserJsonDto() {
|
||||||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
final Object principal = authentication.getPrincipal();
|
final Object principal = authentication.getPrincipal();
|
||||||
|
@ -60,10 +61,9 @@ public class UserServiceImpl implements UserService {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注意:产品化的设计此处应该有分页会更好
|
* 提示:产品化的设计此处应该有分页会更好
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
// @Transactional(readOnly = true)
|
|
||||||
public UserOverviewDto loadUserOverviewDto(UserOverviewDto overviewDto) {
|
public UserOverviewDto loadUserOverviewDto(UserOverviewDto overviewDto) {
|
||||||
List<User> users = userRepository.findUsersByUsername(overviewDto.getUsername());
|
List<User> users = userRepository.findUsersByUsername(overviewDto.getUsername());
|
||||||
overviewDto.setUserDtos(UserDto.toDtos(users));
|
overviewDto.setUserDtos(UserDto.toDtos(users));
|
||||||
|
@ -71,14 +71,12 @@ public class UserServiceImpl implements UserService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
// @Transactional(readOnly = true)
|
|
||||||
public boolean isExistedUsername(String username) {
|
public boolean isExistedUsername(String username) {
|
||||||
final User user = userRepository.findByUsername(username);
|
final User user = userRepository.findByUsername(username);
|
||||||
return user != null;
|
return user != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
// @Transactional(propagation = Propagation.REQUIRED)
|
|
||||||
public String saveUser(UserFormDto formDto) {
|
public String saveUser(UserFormDto formDto) {
|
||||||
User user = formDto.newUser();
|
User user = formDto.newUser();
|
||||||
userRepository.saveUser(user);
|
userRepository.saveUser(user);
|
||||||
|
|
|
@ -34,6 +34,28 @@ public class UserRepositoryJdbcTest extends AbstractRepositoryTest {
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void findProfileByUsername() {
|
||||||
|
|
||||||
|
String username = "userxxxx";
|
||||||
|
User user = userRepository.findProfileByUsername(username);
|
||||||
|
assertNull(user);
|
||||||
|
|
||||||
|
User user2 = new User(username, "{123}", "123", "ewo@honyee.cc");
|
||||||
|
user2.address("address").nickname("nick-name");
|
||||||
|
userRepository.saveUser(user2);
|
||||||
|
|
||||||
|
User user3 = userRepository.findProfileByUsername(username);
|
||||||
|
assertNotNull(user3);
|
||||||
|
assertNotNull(user3.phone());
|
||||||
|
assertNotNull(user3.email());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findByGuid() {
|
public void findByGuid() {
|
||||||
User user = userRepository.findByGuid("oood");
|
User user = userRepository.findByGuid("oood");
|
||||||
|
|
Loading…
Reference in New Issue