簡體   English   中英

Spring security 的 @EnableWebSecurity 與 oauth 的 @EnableResourceServer

[英]Spring security's @EnableWebSecurity vs oauth's @EnableResourceServer

我有一個使用 Spring Boot、Angular 2、Spring OAuth 2 的系統,其中我使用@EnableWebSecurity實現了安全性,並在同一應用程序中使用@EnableResourceServer@EnableAuthorizationServer實現了 oauth。

以下是已實現的類:

安全配置文件

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private ClientDetailsService clientDetailsService;

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user").password("pass").roles("USER").and()
                .withUser("username").password("password").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/private/**").hasRole("USER")
                .antMatchers("/public/**").permitAll();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

授權服務器配置文件

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()
                .withClient("my-trusted-client")
                .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
                .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT","USER")
                .scopes("read", "write", "trust")
                .secret("secret")
                .accessTokenValiditySeconds(1200).
                refreshTokenValiditySeconds(6000);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.checkTokenAccess("hasAuthority('USER')");
    }

}

資源服務器配置文件

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter{

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/public/**").permitAll();
        http.authorizeRequests().antMatchers("/private/**").hasRole("USER");
    }
}

任何用戶都可以訪問/public之后的所有 url; 這是正確的。 /private/后面的 url 由ResourceServerConfigSecurityConfig ,因此匿名用戶無法訪問它。

當我要求access_token從授權服務器使用grant_type=password ,我拿到access_token我使用通過附加訪問受保護資源access_token作為參數。 但是資源仍然不可用,我得到如下響應:

localhost:8080/private/user/test/?access_token=92f9d86f-83c4-4896-a203-e21976d4cfa2    

{
    "timestamp": 1495961323209,
    "status": 403,
   "error": "Forbidden",
   "message": "Access Denied",
   "path": "/private/user/test/"
}

但是當我從SecurityConfig.configure(HttpSecurity)刪除antMatchers時,即使ResourceServerConfig.configure(HttpSecurity)正在保護模式,資源也不再受到保護。

我的問題:

  • 是否需要在ResourceServerConfig執行任何操作才能從資源服務器向授權用戶授予訪問權限?
  • @EnableResourceServer@EnableWebSecurity什么區別? 我是否需要在此應用程序中同時實現兩者? (我找不到這個問題的任何好的答案)

您的私有資源得到了很好的保護,但是獲取的access_token沒有以正確的方式傳遞給服務器。

您必須將其作為請求的標頭傳遞

 Authorization: Bearer 92f9d86f-83c4-4896-a203-e21976d4cfa2

或作為 curl 命令:

 curl -H "Authorization: Bearer 92f9d86f-83c4-4896-a203-e21976d4cfa2"

暫無
暫無

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

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