forked from elunez/eladmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SecurityUtils 加入获取当前登录用户ID方法,Security 结构调整
- Loading branch information
Showing
25 changed files
with
159 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 36 additions & 10 deletions
46
eladmin-common/src/main/java/me/zhengjie/utils/SecurityUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,60 @@ | ||
package me.zhengjie.utils; | ||
|
||
import cn.hutool.json.JSONObject; | ||
import lombok.extern.slf4j.Slf4j; | ||
import me.zhengjie.exception.BadRequestException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
|
||
/** | ||
* 获取当前登录的用户 | ||
* @author Zheng Jie | ||
* @date 2019-01-17 | ||
*/ | ||
@Slf4j | ||
public class SecurityUtils { | ||
|
||
public static UserDetails getUserDetails() { | ||
UserDetails userDetails; | ||
try { | ||
userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
} catch (Exception e) { | ||
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "登录状态过期"); | ||
/** | ||
* 获取当前登录的用户 | ||
* @return UserDetails | ||
*/ | ||
public static UserDetails getCurrentUser() { | ||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null) { | ||
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, "找不到当前登录的信息"); | ||
} | ||
|
||
/** | ||
* 获取系统用户名称 | ||
* | ||
* @return 系统用户名称 | ||
*/ | ||
public static String getUsername(){ | ||
Object obj = getUserDetails(); | ||
return new JSONObject(obj).get("username", String.class); | ||
public static String getCurrentUsername() { | ||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null) { | ||
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期"); | ||
} | ||
UserDetails userDetails = (UserDetails) authentication.getPrincipal(); | ||
return userDetails.getUsername(); | ||
} | ||
|
||
/** | ||
* 获取系统用户ID | ||
* | ||
* @return 系统用户ID | ||
*/ | ||
public static Long getCurrentUserId() { | ||
UserDetails userDetails = getCurrentUser(); | ||
return new JSONObject(new JSONObject(userDetails).get("user")).get("id", Long.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.