簡體   English   中英

訪問特定於應用程序的 url 時在 Spring security oauth 中出現 403 錯誤(access_token 有效且 csrf 被禁用)

[英]Getting 403 error in Spring security oauth while accessing application specific urls (access_token is valid and csrf is disabled)

更新

Chids 的回答是針對我之前發布的問題,該問題是/oauth/token出現 403 錯誤。該錯誤已解決並卡在下一步中。我已相應地修改了問題。

問題:

我正在嘗試使用 Spring 安全性實現 OAuth 2.0。 我通過向/oauth/token發出發布請求成功獲得了access_token 但是當我使用這個訪問令牌來使用任何其他安全的 url 時,我得到了 403。

我已經關注了多個關於 SO 的問題,但他們都建議為我的問題禁用 csrf。 問題是我已經禁用了它,但仍然出錯。

有人可以指導我是否以錯誤的方式構建發布請求或是否缺少某些配置。

我通過郵遞員的帖子請求看起來像:

在此處輸入圖片說明

谷歌配置:

在此處輸入圖片說明

資源服務器

@Configuration
@EnableResourceServer
@Order(3)
public class Oauth2ResourceServerConfig extends ResourceServerConfigurerAdapter {     

    @Override
    public void configure(HttpSecurity http) throws Exception {     
        http.csrf().disable();

            http.requestMatchers().antMatchers("/auth/**")
            .and()
            .authorizeRequests()                    
            .antMatchers("/auth/**").authenticated();   

    }
}

授權服務器

@Configuration
@EnableAuthorizationServer  
public class Oauth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private UserApprovalHandler handler;

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



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

        clients.inMemory()
            .withClient("568176070083-1lc20949a0q58l0rhmq93n95kvu8s5o6.apps.googleusercontent.com")
            .secret("lNfK3wOaVibgu96il6WLrkTh")         
            .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
            .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
            .scopes("read", "write", "trust")
            .accessTokenValiditySeconds(120)
            .refreshTokenValiditySeconds(600);              

    }   


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

}

安全配置

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
@ComponentScan(basePackages = "com.saml.demo")
public class SecurityConfig extends WebSecurityConfigurerAdapter {    

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http
    .csrf().disable()
    .anonymous().disable()
    .authorizeRequests()
    .antMatchers("/oauth/token").permitAll()
    .anyRequest().authenticated();
    }

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


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

    @Bean
    @Autowired
    public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
        TokenApprovalStore store = new TokenApprovalStore();
        store.setTokenStore(tokenStore);
        return store;
    }
}

這應該是因為,您在配置塊中禁用了所有匿名訪問。 您可以將其更改為以下內容

@Override
    protected void configure(final HttpSecurity http) throws Exception {
        // @formatter:off
        http.authorizeRequests().antMatchers("/login").permitAll().antMatchers("/oauth/token/revokeById/**").permitAll()
                .antMatchers("/tokens/**").permitAll().anyRequest().authenticated().and().formLogin().permitAll().and()
                .csrf().disable();
        // @formatter:on
    }

我們對此有什么解決方案嗎? 甚至我也面臨着同樣的問題403。

暫無
暫無

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

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