新增客户端通过系统access_token请求菜单

This commit is contained in:
wenc000 2019-11-13 22:26:15 +08:00
parent 53149939cd
commit 8ba9db01ea
5 changed files with 50 additions and 7 deletions

View File

@ -9,11 +9,21 @@ package com.cm.common.plugin;
**/
public interface IApiConsts {
/**
* access_token
*/
String ACCESS_TOKEN = "access_token";
/**
* 菜单列表
*/
String LIST_MENU = "%s/resource/menu/listmenubyclientid/%s";
/**
* 通过客户端和用户获取菜单列表
*/
String LIST_MENU_BY_USER = "%s/resource/menu/listmenubyclientid/%s/%s";
/**
* 父ID获取部门列表
*/

View File

@ -2,19 +2,18 @@ package com.cm.common.plugin.converter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.config.properties.OauthClientProperties;
import com.cm.common.config.properties.OauthProperties;
import com.cm.common.plugin.IApiConsts;
import com.cm.common.plugin.oauth.token.ClientTokenManager;
import com.cm.common.plugin.utils.RestTemplateUtil;
import com.cm.common.pojo.bos.*;
import com.cm.common.utils.authority.AuthorityUtil;
import com.cm.common.pojo.bos.RoleBO;
import com.cm.common.pojo.bos.RoleGrantedAuthority;
import com.cm.common.pojo.bos.UserInfoBO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter;
import org.springframework.util.StringUtils;
@ -77,7 +76,7 @@ public class ClientUserAuthConverter implements UserAuthenticationConverter {
return authorities;
}
Map<String, Object> params = new HashMap<>(1);
params.put("access_token", ClientTokenManager.getInstance().getClientToken().getAccessToken());
params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
String result = this.restTemplateUtil.doGetFormNormal(String.format("%s/resource/role/listrolebo/%s", oauthProperties.getOauthServer(), StringUtils.collectionToDelimitedString(authorities, "_")), params);
if (Objects.isNull(result)) {
throw new IllegalArgumentException("权限不足,无法获取角色权限信息");

View File

@ -38,4 +38,11 @@ public class MenuController extends AbstractController {
return menuService.listMenu(params);
}
@ApiOperation(value = "我的菜单列表", notes = "我的菜单列表接口")
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@GetMapping("listmenuofmine")
public JSONArray listMenuOfMine() throws AccessTokenException, SearchException {
return menuService.listMenuOfMine();
}
}

View File

@ -22,6 +22,13 @@ public interface IMenuService {
* @param params
* @return
*/
JSONArray listMenu(Map<String, Object> params) throws AccessTokenException, AccessTokenException, SearchException;
JSONArray listMenu(Map<String, Object> params) throws AccessTokenException, SearchException;
/**
* 通过token获取我的菜单
* @return
* @throws AccessTokenException
* @throws SearchException
*/
JSONArray listMenuOfMine() throws AccessTokenException, SearchException;
}

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractService;
import com.cm.common.component.OAuthRestTemplateComponent;
import com.cm.common.component.SecurityComponent;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.exception.AccessTokenException;
import com.cm.common.exception.SearchException;
@ -16,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
@ -36,6 +38,8 @@ public class MenuServiceImpl extends AbstractService implements IMenuService {
private OAuthRestTemplateComponent oAuthRestTemplateComponent;
@Autowired
private ApiPathProperties apiPathProperties;
@Autowired
private SecurityComponent securityComponent;
@Override
public JSONArray listMenu(Map<String, Object> params) throws AccessTokenException, SearchException {
@ -50,4 +54,20 @@ public class MenuServiceImpl extends AbstractService implements IMenuService {
return resultObj.getJSONArray("data");
}
@Override
public JSONArray listMenuOfMine() throws AccessTokenException, SearchException {
Map<String, Object> params = new HashMap<>(1);
params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_MENU_BY_USER, apiPathProperties.getUserCenter(), oAuth2ClientProperties.getClientId(), securityComponent.getCurrentUser().getUserId()), params);
if (result == null) {
throw new AccessTokenException("认证失败");
}
if (result.isEmpty()) {
throw new SearchException("获取列表失败");
}
JSONObject resultObj = JSONObject.parseObject(result);
return resultObj.getJSONArray("data");
}
}