Commit 8a2ca63e by tangyi

优化

parent 06a9c8fd
...@@ -138,5 +138,10 @@ public class CommonConstant { ...@@ -138,5 +138,10 @@ public class CommonConstant {
*/ */
public static final String DEFAULT_CODE_KEY = "DEFAULT_CODE_KEY"; public static final String DEFAULT_CODE_KEY = "DEFAULT_CODE_KEY";
/**
* Bearer
*/
public static final String AUTHORIZATION_BEARER = "Bearer ";
} }
...@@ -77,6 +77,7 @@ ignore: ...@@ -77,6 +77,7 @@ ignore:
- /actuator/** - /actuator/**
- /hystrix.sender - /hystrix.sender
- /v1/user/findUserByUsername/** - /v1/user/findUserByUsername/**
- /v1/menu/findMenuByRole/**
- /v1/code/** - /v1/code/**
- /v1/attachment/download - /v1/attachment/download
- /v1/log/** - /v1/log/**
......
...@@ -91,6 +91,7 @@ ignore: ...@@ -91,6 +91,7 @@ ignore:
- /actuator/** - /actuator/**
- /hystrix.sender - /hystrix.sender
- /v1/user/findUserByUsername/** - /v1/user/findUserByUsername/**
- /v1/menu/findMenuByRole/**
- /v1/code/** - /v1/code/**
- /v1/attachment/download - /v1/attachment/download
- /v1/log/** - /v1/log/**
......
...@@ -109,6 +109,7 @@ ignore: ...@@ -109,6 +109,7 @@ ignore:
- /actuator/** - /actuator/**
- /hystrix.sender - /hystrix.sender
- /v1/user/findUserByUsername/** - /v1/user/findUserByUsername/**
- /v1/menu/findMenuByRole/**
- /v1/user/register - /v1/user/register
- /v1/code/** - /v1/code/**
- /v1/attachment/download - /v1/attachment/download
......
package com.github.tangyi.auth.controller; package com.github.tangyi.auth.controller;
import com.github.tangyi.common.security.constant.SecurityConstant; import com.github.tangyi.common.core.constant.CommonConstant;
import com.github.tangyi.common.core.model.ResponseBean; import com.github.tangyi.common.core.model.ResponseBean;
import com.github.tangyi.common.core.web.BaseController; import com.github.tangyi.common.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices; import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -44,8 +44,9 @@ public class AuthenticationController extends BaseController { ...@@ -44,8 +44,9 @@ public class AuthenticationController extends BaseController {
* @return ReturnT * @return ReturnT
*/ */
@PostMapping("/removeToken") @PostMapping("/removeToken")
@CacheEvict(value = SecurityConstant.TOKEN_USER_DETAIL, key = "#accesstoken") public ResponseBean<Boolean> removeToken(@RequestHeader("Authorization") String accesstoken) {
public ResponseBean<Boolean> removeToken(String accesstoken) { if (accesstoken.startsWith(CommonConstant.AUTHORIZATION_BEARER))
accesstoken = accesstoken.split(CommonConstant.AUTHORIZATION_BEARER)[1];
return new ResponseBean<>(consumerTokenServices.revokeToken(accesstoken)); return new ResponseBean<>(consumerTokenServices.revokeToken(accesstoken));
} }
} }
...@@ -5,6 +5,7 @@ import com.github.tangyi.common.core.vo.UserVo; ...@@ -5,6 +5,7 @@ import com.github.tangyi.common.core.vo.UserVo;
import com.github.tangyi.common.security.core.GrantedAuthorityImpl; import com.github.tangyi.common.security.core.GrantedAuthorityImpl;
import com.github.tangyi.common.security.core.UserDetailsImpl; import com.github.tangyi.common.security.core.UserDetailsImpl;
import com.github.tangyi.user.api.feign.UserServiceClient; import com.github.tangyi.user.api.feign.UserServiceClient;
import com.github.tangyi.user.api.module.Menu;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -16,6 +17,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException; ...@@ -16,6 +17,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
...@@ -62,11 +64,22 @@ public class UserDetailsServiceImpl implements UserDetailsService { ...@@ -62,11 +64,22 @@ public class UserDetailsServiceImpl implements UserDetailsService {
* @date 2019/03/17 14:41 * @date 2019/03/17 14:41
*/ */
private Set<GrantedAuthority> getAuthority(UserVo userVo) { private Set<GrantedAuthority> getAuthority(UserVo userVo) {
// 权限集合
Set<GrantedAuthority> authorities = new HashSet<>(); Set<GrantedAuthority> authorities = new HashSet<>();
if (CollectionUtils.isNotEmpty(userVo.getRoleList())) { // 角色
for (Role role : userVo.getRoleList()) { List<Role> roleList = userVo.getRoleList();
if (CollectionUtils.isNotEmpty(roleList)) {
for (Role role : roleList) {
// 权限如果前缀是ROLE_,security就会认为这是个角色信息,而不是权限,例如ROLE_ADMIN就是ADMIN角色,MENU:ADD就是MENU:ADD权限 // 权限如果前缀是ROLE_,security就会认为这是个角色信息,而不是权限,例如ROLE_ADMIN就是ADMIN角色,MENU:ADD就是MENU:ADD权限
authorities.add(new GrantedAuthorityImpl(role.getRoleCode().toUpperCase())); authorities.add(new GrantedAuthorityImpl(role.getRoleCode().toUpperCase()));
// 根据角色查找菜单权限
List<Menu> menuList = userServiceClient.findMenuByRole(role.getRoleCode());
if (CollectionUtils.isNotEmpty(menuList)) {
for (Menu menu : menuList) {
// 菜单权限
authorities.add(new GrantedAuthorityImpl(menu.getPermission()));
}
}
} }
} }
return authorities; return authorities;
......
...@@ -173,8 +173,7 @@ public class UserService extends CrudService<UserMapper, User> { ...@@ -173,8 +173,7 @@ public class UserService extends CrudService<UserMapper, User> {
*/ */
@Cacheable(value = "user", key = "#username") @Cacheable(value = "user", key = "#username")
public UserVo selectUserVoByUsername(String username) { public UserVo selectUserVoByUsername(String username) {
UserVo userVo = userMapper.selectUserVoByUsername(username); return userMapper.selectUserVoByUsername(username);
return userVo;
} }
/** /**
......
...@@ -9,6 +9,7 @@ import com.github.tangyi.common.core.vo.UserVo; ...@@ -9,6 +9,7 @@ import com.github.tangyi.common.core.vo.UserVo;
import com.github.tangyi.common.feign.config.CustomFeignConfig; import com.github.tangyi.common.feign.config.CustomFeignConfig;
import com.github.tangyi.user.api.dto.UserInfoDto; import com.github.tangyi.user.api.dto.UserInfoDto;
import com.github.tangyi.user.api.feign.factory.UserServiceClientFallbackFactory; import com.github.tangyi.user.api.feign.factory.UserServiceClientFallbackFactory;
import com.github.tangyi.user.api.module.Menu;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -92,4 +93,15 @@ public interface UserServiceClient { ...@@ -92,4 +93,15 @@ public interface UserServiceClient {
*/ */
@PostMapping("/v1/log") @PostMapping("/v1/log")
ResponseBean<Boolean> saveLog(@RequestBody Log log); ResponseBean<Boolean> saveLog(@RequestBody Log log);
/**
* 根据角色查找菜单
*
* @param role 角色
* @return List
* @author tangyi
* @date 2019/04/08 20:42
*/
@GetMapping("/v1/menu/findMenuByRole/{role}")
List<Menu> findMenuByRole(@PathVariable("role") String role);
} }
...@@ -7,6 +7,7 @@ import com.github.tangyi.common.core.vo.DeptVo; ...@@ -7,6 +7,7 @@ import com.github.tangyi.common.core.vo.DeptVo;
import com.github.tangyi.common.core.vo.UserVo; import com.github.tangyi.common.core.vo.UserVo;
import com.github.tangyi.user.api.dto.UserInfoDto; import com.github.tangyi.user.api.dto.UserInfoDto;
import com.github.tangyi.user.api.feign.UserServiceClient; import com.github.tangyi.user.api.feign.UserServiceClient;
import com.github.tangyi.user.api.module.Menu;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -111,6 +112,18 @@ public class UserServiceClientFallbackImpl implements UserServiceClient { ...@@ -111,6 +112,18 @@ public class UserServiceClientFallbackImpl implements UserServiceClient {
return null; return null;
} }
/**
* 根据角色查找菜单
*
* @param role 角色
* @return List
*/
@Override
public List<Menu> findMenuByRole(String role) {
logger.error("feign 获取角色菜单失败,{}", throwable);
return new ArrayList<>();
}
public Throwable getThrowable() { public Throwable getThrowable() {
return throwable; return throwable;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment