spring-boot - 如何在 Spring Security 中使用 Model Mapper?
问题描述
我尝试替换BeanUtils.copyProperties
为ModelMapper.map()
. 尝试使用 Spring Security 登录时出现错误:
1) Converter org.modelmapper.internal.converter.MergingCollectionConverter@2a5749f5 failed to convert org.hibernate.collection.internal.PersistentBag to java.util.List.
1 error
at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:380) ~[modelmapper-2.3.7.jar:na]
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:81) ~[modelmapper-2.3.7.jar:na]
at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:573) ~[modelmapper-2.3.7.jar:na]
at org.modelmapper.ModelMapper.map(ModelMapper.java:406) ~[modelmapper-2.3.7.jar:na]
at com.thao.wsapplication.service.impl.UserServiceImpl.getUser(UserServiceImpl.java:81) ~[main/:na]
at com.thao.wsapplication.security.AuthenticationFilter.successfulAuthentication(AuthenticationFilter.java:71) ~[main/:na]
功能getUser()
@Override
public UserDto getUser(String email) {
UserDto returnValue = new UserDto();
UserEntity userEntity = userRepository.findByEmail(email);
if (userEntity == null) throw new UsernameNotFoundException(email);
returnValue = new ModelMapper().map(userEntity, UserDto.class);
// BeanUtils.copyProperties(userEntity, returnValue);
return returnValue;
}
功能successfulAuthentication()
@Override
protected void successfulAuthentication(HttpServletRequest req,
HttpServletResponse res,
FilterChain chain,
Authentication auth) throws IOException, ServletException {
String userName = ((User) auth.getPrincipal()).getUsername();
String token = Jwts.builder()
.setSubject(userName)
.setExpiration(new Date(System.currentTimeMillis() + SecurityConstants.EXPIRATION_TIME))
.signWith(SignatureAlgorithm.HS512, SecurityConstants.getTokenSecret())
.compact();
UserService userService = (UserService)SpringApplicationContext.getBean("userServiceImpl");
UserDto userDto = userService.getUser(userName);
res.addHeader(SecurityConstants.HEADER_STRING, SecurityConstants.TOKEN_PREFIX + token);
res.addHeader("UserID", userDto.getUserId());
}
更新我的源代码:源代码
解决方案
推荐阅读
- python - 带有嵌套生成器的泡菜对象
- java - 如果我使用 SMTP 配置发送电子邮件(文本/html),SendGrid 不会发送电子邮件
- javascript - 无法从 json 对象数组访问 json 对象中的字段
- linux - 当我使用本地存储库时,Apt 更新失败
- indexing - 通过 faiss 中的 c++ 接口删除_ids
- ios - 使用 Alamofire 将个人资料图像发布到 Laravel 服务器
- regex - python regex:在数字和字符之间放置空格
- clojure - 将值累加到列表中
- azure - 是否可以在 iframe 上设置 cookie?
- php - 致命错误:在第 13 行的 ..loginc.php 中调用布尔值的成员函数 rowCount()