SpringSceurity(7)--- Security+JWT搭建簡單的授權服務器和資源服務器


Security+JWT搭建簡單的授權服務器和資源服務器

之前有兩篇博客分別將來介紹了 OAuth 2.0JWT

1、OAuth 2.0詳解

2、SpringSceurity(6)---JWT詳解

這邊通過Security+JWT搭建簡單的授權服務器和資源服務器示例。

整體流程大概是這樣的

JWT 認證流程

1、用戶先訪問授權服務器。傳入用戶輸入用戶名/密碼登錄,授權服務器認證成功后,會返回給客戶端一個 JWT
2、用戶在訪問資源服務器。當用戶希望訪問一個受保護的路由或者資源的時候,需要請求頭的 Authorization 字段中使用Bearer 模式添加 JWT,其內容看起來是下面這樣
   Authorization: Bearer復制代碼
3、資源服務器保護路由將會檢查請求頭 Authorization 中的 JWT 信息,如果合法,則允許用戶的行為

下面開始搭建

一、搭建授權服務器

授權服務器配置類

/**
 * @Description: 授權服務器配置
 *   使用 @EnableAuthorizationServer 來配置授權服務機制,並繼承 AuthorizationServerConfigurerAdapter 該類重寫 configure 方法定義授權服務器策略
 */
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    /**
     * 使用同一個密鑰來編碼 JWT 中的  OAuth2 令牌,在資源服務器解析JWT的時候需要有一樣的密鑰
     */
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("dada");
        return converter;
    }

    @Bean
    public JwtTokenStore jwtTokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    /**
     * 告訴Spring Security Token的生成方式
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
            .authenticationManager(authenticationManager)
            .tokenStore(jwtTokenStore())
            .accessTokenConverter(accessTokenConverter());
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //添加客戶端信息 使用in-memory存儲客戶端信息
        clients.inMemory()
                //客戶端標識 ID
            .withClient("clientId")
                //客戶密鑰
            .secret("666666")
                //客戶端訪問范圍,默認為空則擁有全部范圍
            .scopes("read_userinfo")
                //客戶端使用的授權類型,默認為空
            .authorizedGrantTypes(
                "password",
                "authorization_code",
                "refresh_token");
    }
}

application.properties配置類

security.user.name=xuxiaoxiao
security.user.password=123456

這里設置 用戶名為: xuxiaoxiao 密碼為: 123456 端口號默認為: 8080

說明 有關其它代碼這里就不再貼出,最后會給出整個demo的github地址。


二、搭建資源服務器

資源服務器配置類

/**
 * @Description: 資源服務器通過 @EnableResourceServer 注解來開啟一個 OAuth2AuthenticationProcessingFilter 類型的過濾器
 * 通過繼承 ResourceServerConfigurerAdapter 類來配置資源服務器
 */
@Configuration
@EnableResourceServer
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .anyRequest().authenticated().and()
            .requestMatchers().antMatchers("/api/**");
    }

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * 與授權服務器使用共同的密鑰進行解析
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("dada");
        return converter;
    }
}

UserController

@Controller
public class UserController {

    @RequestMapping("/api/userinfo")
    public ResponseEntity<String> getUerInfo() {
        //這里會做解析jwt操作,獲取jwt中的用戶名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return ResponseEntity.ok(username);
    }

}

application.properties

server.port=8081

這里只是配置端口號為 8081


三、測試

1、整體測試流程

1.)啟動jwt-authserver(授權服務器),端口8080

2.) 啟動jwt-resourceserver(資源服務器),端口8081

3.) 請求授權服務器,獲取jwt令牌

curl -X POST --user clientId:666666 http://localhost:8080/oauth/token -H "accept: application/json" -H "content-type: application/x-www-formurlencoded" 
-d "grant_type=password&username=xuxiaoxiao&password=123456&scope=read_userinfo"

4.) 帶上jwt令牌請求資源服務器

curl -X GET http://localhost:8081/api/userinfo -H "authorization: Bearer 上面返回的jwt令牌

2、postman測試

先請求授權服務器

從請求中可以看出,授權服務器已經成功返回 access_token

再請求資源服務器

因為上面授權服務器已經返回access_token,那么我們在請求資源服務器的時候,就可以在請求頭中加入這個token。

可以看出資源服務器已經成功解析這個access_token,獲取到當前用戶名。

總結:這個demo實現了基於Security+JWT搭建簡單的授權服務器和資源服務器,也僅僅是搭建了簡單的demo。里面的數據都是寫死的並沒有通過配置走,對於配置類也沒有做詳細的說明。

Github地址 : 基於Security+JWT搭建簡單的授權服務器和資源服務器


參考

1、授權服務器支持 JWT 令牌 (楊波)

2、oauth2-demo(demo更加完整)

3、Spring Security 與 OAuth2



別人罵我胖,我會生氣,因為我心里承認了我胖。別人說我矮,我就會覺得好笑,因為我心里知道我不可能矮。這就是我們為什么會對別人的攻擊生氣。
攻我盾者,乃我內心之矛(25)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM