Java Spring Security: 401 Unauthorized for token OAuth2 end point

筅森魡賤 提交于 2019-12-05 03:46:58

Turns out I wasn't hitting the end point correctly. I was sending all of my data, client credentials included, via HTTP POST.

POST http://localhost:8080/oauth/token
...
client_id=client_id&secret=secret&scope=read&grant_type=password&username=user&password=password

I needed to use HTTP Basic Auth to send my client credentials rather than POSTing them:

POST http://localhost:8080/oauth/token
Authorization: Basic Y2xpZW50X2lkOnNlY3JldA==
...
scope=read&grant_type=password&username=user&password=password

try changing your password encoder from your AuthorizationServerConfig class with this simple encoder(it doesn't encrypt passwords).because you don't save your client secret in InMemory storage with encryption.

private PasswordEncoder getPasswordEncoder() {
    return new PasswordEncoder() {
        public String encode (CharSequence charSequence) {
            return charSequence.toString();
        }
        public boolean matches(CharSequence charSequence, String s) {
            return true;
        }
    };
}

hope it will work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!