bug--同步之前修改的获取用户报错和用户中心搜索报错

This commit is contained in:
taozi
2020-06-28 10:39:53 +08:00
parent 023811347f
commit 2734473c56
3 changed files with 19 additions and 10 deletions

View File

@ -150,7 +150,7 @@ public class OnlineUserService {
for (OnlineUser user : all) { for (OnlineUser user : all) {
Map<String,Object> map = new LinkedHashMap<>(); Map<String,Object> map = new LinkedHashMap<>();
map.put("用户名", user.getUserName()); map.put("用户名", user.getUserName());
map.put("岗位", user.getJob()); map.put("用户昵称", user.getNickName());
map.put("登录IP", user.getIp()); map.put("登录IP", user.getIp());
map.put("登录地点", user.getAddress()); map.put("登录地点", user.getAddress());
map.put("浏览器", user.getBrowser()); map.put("浏览器", user.getBrowser());

View File

@ -8,8 +8,10 @@ package co.yixiang.utils;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import co.yixiang.exception.BadRequestException; import co.yixiang.exception.BadRequestException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
/** /**
* 获取当前登录的用户 * 获取当前登录的用户
@ -19,13 +21,16 @@ import org.springframework.security.core.userdetails.UserDetails;
public class SecurityUtils { public class SecurityUtils {
public static UserDetails getUserDetails() { public static UserDetails getUserDetails() {
UserDetails userDetails; final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
try { if (authentication == null) {
userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
} catch (Exception e) {
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "登录状态过期");
} }
return userDetails; if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
return userDetailsService.loadUserByUsername(userDetails.getUsername());
}
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
} }
/** /**
@ -33,8 +38,12 @@ public class SecurityUtils {
* @return 系统用户名称 * @return 系统用户名称
*/ */
public static String getUsername(){ public static String getUsername(){
Object obj = getUserDetails(); final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return new JSONObject(obj).get("username", String.class); if (authentication == null) {
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
}
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return userDetails.getUsername();
} }
/** /**

View File

@ -81,7 +81,7 @@ public interface UserBillMapper extends CoreMapper<YxUserBill> {
"from yx_user_bill b left join yx_user u on u.uid=b.uid where 1=1 " + "from yx_user_bill b left join yx_user u on u.uid=b.uid where 1=1 " +
"<if test =\"category !=''\">and b.category=#{category}</if> " + "<if test =\"category !=''\">and b.category=#{category}</if> " +
"<if test =\"type !=''\">and b.type=#{type}</if> " + "<if test =\"type !=''\">and b.type=#{type}</if> " +
"<if test =\"nickname !=''\">and u.nickname= LIKE CONCAT('%',#{nickname},'%')</if> </script> ") "<if test =\"nickname !=''\">and u.nickname LIKE CONCAT('%',#{nickname},'%')</if> </script> ")
List<YxUserBillDto> findAllByQueryCriteria(@Param("category") String category, @Param("type") String type, List<YxUserBillDto> findAllByQueryCriteria(@Param("category") String category, @Param("type") String type,
@Param("nickname") String nickname); @Param("nickname") String nickname);
} }