簡體   English   中英

Spring OAuth2和JWT認證信息

[英]Spring OAuth2 and JWT Authentication information

我有一個充當資源服務器的Spring Boot(1.3.x)應用程序,可以從授權標頭中的Keycloak傳遞JWT令牌,並被清除以訪問某些端點。 我遇到的問題是我無法獲得Authorization對象中JWT令牌中的信息。

SecurityContext sc = SecurityContextHolder.getContext();
Authentication a = sc.getAuthentication(); //OR OAuth2Authentication oa = (OAuth2Authentication)a;
Object p = a.getPrincipal();

如果我將IDP放在令牌中,則p將保留client_id的名稱。

isClientOnly()返回true。 為什么?

我很希望能夠獲得我的用戶名和授權,但是都沒有找到。 如果我沒有client_id,則實際上為null。

我試圖了解我是否必須對驗證后如何處理JWT令牌進行一些配置,以便正確的信息進入Authentication對象,但是我完全感到困惑。 我該如何實現?

Spring Boot非常有用,您幾乎無法做些什么來使某項運行,但是與此同時,我茫然不知所措,甚至一開始真正設置了什么。

我的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();
}

您可能對這個原理感到困惑...據我所知,您可以像注入其他bean一樣注入它...或通過這樣的contoller操作訪問它

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

除此之外,我在您的代碼中看不到任何JwtConfiguration以及令牌存儲bean。

您必須配置類似的內容(假設您的密鑰保存在資源目錄中的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;
    }
}

希望我能幫上忙。 也許我的文章可以給出更詳細的解釋:)

令牌存儲部分讓我有所作為...似乎我的問題在於我正在使用Keycloak,並且它返回的是openid連接令牌,其中字段名稱大多不同。 我在Keycloak令牌中硬編碼了諸如“ user_name”之類的某些字段,盡管其中沒有太多信息,但突然間可以得到主體對象。

因此,我誤以為Spring的OAuth2 JWT東西會自動與任何JWT一起工作。 開始使用Keycloak適配器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM