久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

訪問資源服務器控制器內的 Spring OAuth 2 JWT 有效負

Accessing a Spring OAuth 2 JWT payload inside the Resource Server controller?(訪問資源服務器控制器內的 Spring OAuth 2 JWT 有效負載?)
本文介紹了訪問資源服務器控制器內的 Spring OAuth 2 JWT 有效負載?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在通過 本教程了解如何設置 Spring Boot oauth與 jwt.它涵蓋了使用 Angular 解碼 JWT 令牌,但我們如何解碼它并訪問資源服務器控制器內的自定義聲明?

I'm going through this tutorial on how to setup spring boot oauth with jwt. It covers decoding the JWT token using Angular, but how do we decode it and get access to custom claims inside the Resource Server controller?

例如,使用 JJWT 可以這樣做(基于這篇文章):

For example with JJWT it can be done like this (Based on this article):

    String subject = "HACKER";
    try {
        Jws jwtClaims = 
            Jwts.parser().setSigningKey(key).parseClaimsJws(jwt);

        subject = claims.getBody().getSubject();

        //OK, we can trust this JWT

    } catch (SignatureException e) {

        //don't trust the JWT!
    }

Spring 有一個 JWTAccessTokenConverter.decode() 方法,但是缺少javadoc,它是受保護的.

And Spring has a JWTAccessTokenConverter.decode() method, but the javadoc is lacking, and it is protected.

推薦答案

這是我在 Spring Boot 中訪問自定義 JWT 聲明的方式:

Here is how I am accessing custom JWT claims in Spring Boot:

1) 讓 Spring 將 JWT 內容復制到 Authentication 中:

1) Get Spring to copy JWT content into Authentication:

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends ResourceServerConfigurerAdapter{

    @Override
    public void configure(ResourceServerSecurityConfigurer config) {
        config.tokenServices( createTokenServices() );
    }

    @Bean
    public DefaultTokenServices createTokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore( createTokenStore() );
        return defaultTokenServices;
    }

    @Bean
    public TokenStore createTokenStore() {               
        return new JwtTokenStore( createJwtAccessTokenConverter() );
    }

    @Bean
    public JwtAccessTokenConverter createJwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();     
        converter.setAccessTokenConverter( new JwtConverter() );
        return converter;
    }

    public static class JwtConverter extends DefaultAccessTokenConverter implements JwtAccessTokenConverterConfigurer {

        @Override
        public void configure(JwtAccessTokenConverter converter) {
            converter.setAccessTokenConverter(this);
        }

        @Override
        public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
            OAuth2Authentication auth = super.extractAuthentication(map);
            auth.setDetails(map); //this will get spring to copy JWT content into Authentication
            return auth;
        }
    }
}

2) 在代碼中的任何位置訪問令牌內容:

2) Access token content anywhere in your code:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();        
Object details = authentication.getDetails();        
if ( details instanceof OAuth2AuthenticationDetails ){
    OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails)details;

    Map<String, Object> decodedDetails = (Map<String, Object>)oAuth2AuthenticationDetails.getDecodedDetails();

    System.out.println( "My custom claim value: " + decodedDetails.get("MyClaim") );
}  

這篇關于訪問資源服務器控制器內的 Spring OAuth 2 JWT 有效負載?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 久久免费精品 | 超碰成人免费观看 | 国内精品视频 | 国产精品九九九 | 婷婷桃色网 | 久久久高清 | 天堂综合网久久 | 亚洲aⅴ一区二区 | 免费亚洲婷婷 | 激情毛片 | 99亚洲综合| 在线视频成人 | 国产网站在线播放 | 国产日韩免费观看 | 成人三级视频在线观看 | 999久久久精品 | 亚洲一区二区三区 | 中文字幕在线一 | 日韩国产中文字幕 | av中文字幕网 | 成人精品福利 | 成人免费在线视频 | 视频一区在线观看 | 亚洲a在线视频 | 中文字幕在线一区二区三区 | 一区二区三区四区在线 | 在线免费观看视频你懂的 | 国产精品3区 | 毛片一级片 | 亚洲视频国产 | 成人一区二区三区在线观看 | 国产精品视频免费观看 | 午夜在线| 国产在线一区二区 | 99久久日韩精品免费热麻豆美女 | 欧美三级不卡 | a级片在线观看 | 国产激情偷乱视频一区二区三区 | 成人黄色电影在线播放 | 日韩在线中文 | 97色免费视频 |