Skip to content

Commit

Permalink
动态权限
Browse files Browse the repository at this point in the history
  • Loading branch information
beiyoufx committed Oct 30, 2018
1 parent 4fef76b commit a416aeb
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
7 changes: 7 additions & 0 deletions soraka-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<jjwt.version>0.9.1</jjwt.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -74,6 +75,12 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--JWT-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jjwt.version}</version>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ public List<MenuDO> findRoleMenu(@NotBlank @RequestParam String[] roleKeys) {
@ApiOperation("查询用户路由树")
public R findUserMenu() {
R r = R.success();
// TODO 获取用户角色
List<String> roleKeys = new ArrayList<>();
roleKeys.add("admin");
roleKeys.add("tech");
roleKeys.add("editor");
List<String> roleKeys = getRole();
List<MenuDO> menus = menuService.getRoleMenu(roleKeys);
List<VueRouter> routers = new ArrayList<>();
menus.forEach(menuDO -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.soraka.admin.controller;

import com.soraka.admin.model.dto.UserInfo;
import com.soraka.admin.service.MenuService;
import com.soraka.common.model.domain.RoleDO;
import com.soraka.common.model.domain.UserDO;
import com.soraka.common.model.dto.Page;
Expand Down Expand Up @@ -30,6 +32,8 @@ public class UserController extends BaseController {
private UserService userService;
@Autowired
private RoleService roleService;
@Autowired
private MenuService menuService;

@GetMapping("{id}")
@ApiOperation("获取用户详情")
Expand Down Expand Up @@ -94,4 +98,15 @@ public UserDO getByEmail(@PathVariable("email") String email) {
public UserDO getByMobilephone(@PathVariable("mobilephone") String mobilephone) {
return userService.getByMobilephone(mobilephone);
}

@GetMapping("info")
@ApiOperation("个人信息")
public UserInfo info() {
String username = getUsername();
UserDO user = userService.getByUsername(username);
UserInfo userInfo = new UserInfo();
userInfo.setUser(user);
userInfo.setPermissions(menuService.getUserPermission(user.getId()));
return userInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
*/
@Data
public class UserInfo extends BaseDTO {
/**
* 用户凭证
*/
private String token;

/**
* 用户信息
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public List<String> getUserPermission(Long userId) {
@Transactional(readOnly = true, rollbackFor = {RuntimeException.class})
@Override
public List<MenuDO> findAll() {
return menuDAO.find(new QueryParam());
return menuDAO.find(null);
}

/**
Expand Down

0 comments on commit a416aeb

Please sign in to comment.