我有一个充当资源服务器的Spring Boot(1.3.x)应用程序,我可以在Authorization标头中传递来自Keycloak的JWT令牌,并且被清除以访问某些端点。我遇到的问题是,我无法获取Authorization对象中我的JWT令牌中的信息。Spring OAuth2和JWT身份验证信息
SecurityContext sc = SecurityContextHolder.getContext();
Authentication a = sc.getAuthentication(); //OR OAuth2Authentication oa = (OAuth2Authentication)a;
Object p = a.getPrincipal();
和P将举行CLIENT_ID的名字,如果我有IDP把它的令牌。
isClientOnly()返回true。为什么?
我希望能够得到我的用户名和赠款,但没有发现。 如果我没有client_id,它实际上是空的。
我试着了解,如果我不知怎么配置了一些关于如何处理JWT令牌后进行验证,所以正确的信息进入认证对象,但我完全困惑。我怎样才能做到这一点?
春天启动的巨大的你如何能做到这么少拿东西运行,但在同一时间,我不知所措我哪里开始,或什么是真正甚至在第一位置设置...
我application.yml:
server:
servlet-path: /*
security:
oauth2:
resource:
jwt:
keyValue:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmFbcU2Q6WZwUjEBvsZP8kIiE5mmctTGq1SOrPN5U4MkaTVs/zs2cWf+fhIE4WQ+dF9xTPxrJCGkwCMMXEhReFadOvzW8S3VdKdhng/j2GUbleU1QLSOGLhXu2+ODxeToZGpXIxtJiK2wE0JI9T6UwAe9j/stp4pMUzQubkG9acA+lpA5+zaCylLHysNFlSO1cTgzTdIZpXQGu5nkmyz7xT6vq8n2qAsD5GG5JRpBAac6L10Hnvl1YbwnPfi1T+IZSq7FdSpGJmYeOiPhJF0j7qYOMcaQgOfzoQGBhXslIHZeeaBIuUM6QFgZacJsVxdQoRvMronuuacnS+bngMEVDQIDAQAB
-----END PUBLIC KEY-----
logging:
level:
org:
springframework:
security: DEBUG
我SecurityConfig.java:
package com.something.me;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
@Configuration
@EnableOAuth2Sso
@EnableResourceServer
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
//TODO add scope and role restrictions...
http.authorizeRequests().anyRequest().authenticated();
}