diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/IApiConsts.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/IApiConsts.java index d7aaf48..cf2897a 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/IApiConsts.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/IApiConsts.java @@ -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获取部门列表 */ diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/converter/ClientUserAuthConverter.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/converter/ClientUserAuthConverter.java index 908ed21..b7f6d03 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/converter/ClientUserAuthConverter.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/converter/ClientUserAuthConverter.java @@ -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 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("权限不足,无法获取角色权限信息"); diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/menu/MenuController.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/menu/MenuController.java index deae6f3..6f551e0 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/menu/MenuController.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/menu/MenuController.java @@ -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(); + } + } diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/IMenuService.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/IMenuService.java index fbaacc0..4594e86 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/IMenuService.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/IMenuService.java @@ -22,6 +22,13 @@ public interface IMenuService { * @param params * @return */ - JSONArray listMenu(Map params) throws AccessTokenException, AccessTokenException, SearchException; + JSONArray listMenu(Map params) throws AccessTokenException, SearchException; + /** + * 通过token获取我的菜单 + * @return + * @throws AccessTokenException + * @throws SearchException + */ + JSONArray listMenuOfMine() throws AccessTokenException, SearchException; } diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/impl/MenuServiceImpl.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/impl/MenuServiceImpl.java index b4a43b3..9d518cf 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/impl/MenuServiceImpl.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/impl/MenuServiceImpl.java @@ -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 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 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"); + } + + }