2016-02-02 158 views
0

我有一个充当资源服务器的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(); 
} 

回答

0

令牌存储部分让我的一部分...看来我的问题在于,我使用Keycloak,它是返回openid连接令牌的字段名称大多不同。我对Keycloak标记中的某些字段进行了硬编码,例如'user_name',并且突然之间可以获得一个主体对象,尽管其中没有太多信息。

所以我错误地认为Spring的OAuth2 JWT的东西只会自动与任何JWT一起工作。开始使用keycloak适配器。

1

你可能得到的原则混淆....据我所知你可以注入像其他豆类或...访问我吨这样

@Controller 
class MyController { 
    @RequestMapping("/greeting") 
    public Principal greeting(Principal user) { 
    return user; 
    } 
} 

旁边的一个位指示行动,我没有看到任何JwtConfiguration以及令牌存储装置豆在你的代码....

您必须配置这样的事情(假设您的密钥保存在您的资源目录中的public.cert)

@Configuration 
public class JwtConfiguration { 
    @Autowired 
    JwtAccessTokenConverter jwtAccessTokenConverter; 


    @Bean 
    @Qualifier("tokenStore") 
    public TokenStore tokenStore() { 

     System.out.println("Created JwtTokenStore"); 
     return new JwtTokenStore(jwtAccessTokenConverter); 
    } 

    @Bean 
    protected JwtAccessTokenConverter jwtTokenEnhancer() { 
     JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 
     Resource resource = new ClassPathResource("public.cert"); 
     String publicKey = null; 
     try { 
      publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream())); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
     converter.setVerifierKey(publicKey); 
     return converter; 
    } 
} 

希望我能帮上忙。也许my article可以给一个更接近的解释:)