-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.wang.util; | ||
|
||
import com.wang.model.UserDto; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.subject.PrincipalCollection; | ||
import org.springframework.stereotype.Component; | ||
import java.util.Set; | ||
|
||
/** | ||
* 获取当前登录用户工具类 | ||
* @author Wang926454 | ||
* @date 2019/3/15 11:45 | ||
*/ | ||
@Component | ||
public class UserUtil { | ||
|
||
/** | ||
* 获取当前登录用户 | ||
* @param | ||
* @return com.wang.model.UserDto | ||
* @author Wang926454 | ||
* @date 2019/3/15 11:48 | ||
*/ | ||
public UserDto getUser() { | ||
return (UserDto) SecurityUtils.getSubject().getPrincipal(); | ||
} | ||
|
||
/** | ||
* 获取当前登录用户Id | ||
* @param | ||
* @return com.wang.model.UserDto | ||
* @author Wang926454 | ||
* @date 2019/3/15 11:48 | ||
*/ | ||
public Integer getUserId() { | ||
return getUser().getId(); | ||
} | ||
|
||
/** | ||
* 获取当前登录用户Token | ||
* @param | ||
* @return com.wang.model.UserDto | ||
* @author Wang926454 | ||
* @date 2019/3/15 11:48 | ||
*/ | ||
public String getToken() { | ||
PrincipalCollection principalCollection = SecurityUtils.getSubject().getPrincipals(); | ||
if (principalCollection != null) { | ||
Set<String> realmNames = principalCollection.getRealmNames(); | ||
for (String realmName : realmNames) { | ||
return realmName; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 获取当前登录用户Account | ||
* @param | ||
* @return com.wang.model.UserDto | ||
* @author Wang926454 | ||
* @date 2019/3/15 11:48 | ||
*/ | ||
public String getAccount() { | ||
return getUser().getAccount(); | ||
} | ||
} |