spring security oauth2不能内部调用/oauth/token接口

求大神解答,我使用springcloud 整合 spring security oauth2,现在已经可以通过/oauth/token接口拿到token,能成功访问资源服务。然后我想自己定义一个接口,在接口里调用/oauth/token。先是使用RestTemplate去调用/oauth/token,但是直接抛出了401的异常
restTemplate.getForEntity("http://localhost:8008/oauth/token", Object.class, map)
然后我试着使用请求转发来访问/oauth/token

    @GetMapping("/login")
    public void login(@RequestParam String client_id,
                      @RequestParam String client_secret,
                      @RequestParam String grant_type,
                      @RequestParam String username,
                      @RequestParam String password,
                        HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/oauth/token").forward(request, response);
    }

但是返回
{"error":"unauthorized","error_description":"There is no client authentication. Try adding an appropriate authentication filter."}
那怎么才能在内部调用/oauth/token拿到token呢

完整代码可见github

https://www.jianshu.com/p/d9a35facff6f

你好解决了吗,我也遇到相同的问题了。需要内部调用登录

模拟一个外部请求就行 你看对你有用不

    @Override
    @PostMapping("/login/apptoken")
//    @AddIdemepotentToken
    public Map getLoginAppToken(@RequestParam(value = "account", required = true) String account,
                                @RequestParam(value = "password", required = true) String password,
                                @RequestParam(value = "accountType", required = true) AccountTypeEnum accountType,
                                @RequestParam(value = "iddCode", required = false) String iddCode, @RequestHeader HttpHeaders headers) {
        // 使用oauth2密码模式登录.
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        postParameters.add("username", account);
        postParameters.add("password", password);
        postParameters.add("client_id", resourceDetails.getClientId());
        postParameters.add("client_secret", resourceDetails.getClientSecret());
        postParameters.add("grant_type", "password");
        postParameters.add("accountType", accountType.getMessage());
        // 使用客户端的请求头,发起请求
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        if (accountType.getMessage().equals(AccountTypeEnum.USR_MOBILE_VERIFYCODE.getMessage())) {
            // 添加请求头区分,手机登录
            headers.add(AuthConstants.HEADER_X_MOBILE_LOGIN, AccountTypeEnum.USR_MOBILE_VERIFYCODE.getMessage());
            postParameters.add("iddCode", iddCode);
        }