-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
67 additions
and
11 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
43 changes: 43 additions & 0 deletions
43
soraka-admin/src/main/java/com/soraka/admin/controller/BaseController.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,12 +1,55 @@ | ||
package com.soraka.admin.controller; | ||
|
||
import com.soraka.common.constant.Constants; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
/** | ||
* @author yongjie.teng | ||
* @date 2018/8/17 | ||
* @package com.soraka.admin.controller | ||
*/ | ||
@RestController | ||
public class BaseController { | ||
@Autowired | ||
private HttpServletRequest request; | ||
|
||
/** | ||
* 根据JWT中的角色 | ||
* | ||
* @return 角色名 | ||
*/ | ||
public List<String> getRole() { | ||
List<String> roleKeys = (List<String>) getJwtClaims().get("authorities"); | ||
return roleKeys; | ||
} | ||
|
||
|
||
/** | ||
* 根据JWT中的角色 | ||
* | ||
* @return 角色名 | ||
*/ | ||
public String getUsername() { | ||
return (String) getJwtClaims().get("user_name"); | ||
} | ||
|
||
/** | ||
* 从请求头中分离出token | ||
* | ||
* @return token | ||
*/ | ||
private Claims getJwtClaims() { | ||
String authorization = request.getHeader(Constants.TOKEN_HEADER); | ||
String token = StringUtils.substringAfter(authorization, Constants.TOKEN_BEARER); | ||
String key = Base64.getEncoder().encodeToString(Constants.JWT_SIGN_KEY.getBytes()); | ||
return Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -14,11 +14,6 @@ | |
*/ | ||
@Data | ||
public class UserInfo extends BaseDTO { | ||
/** | ||
* 用户凭证 | ||
*/ | ||
private String token; | ||
|
||
/** | ||
* 用户信息 | ||
*/ | ||
|
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