2017-06-19 132 views
0

我試圖通過使用angular2來訪問我的Spring-Boot應用程序和Oauth2身份驗證。當我發送後請求「oauth /令牌」與我的基本身份驗證,包括用戶名和密碼,以獲得一個令牌,這在郵遞員工作正常,我得到一個401未授權。我知道我的瀏覽器使用OPTIONS方法發送預檢請求,並且我已經實現了我的安全配置,以便它應該忽略並允許選項請求,但它不起作用。未經授權的選項請求

這裏是我的安全配置:

@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@EnableWebSecurity 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private ClientDetailsService clientDetailsService; 

    @Autowired 
    DataSource dataSource; 

    @Autowired 
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception { 
    auth.jdbcAuthentication().dataSource(dataSource) 
     .usersByUsernameQuery(
     "select username, password, 1 from users where username = ?") 
     .authoritiesByUsernameQuery(
     "select u.username, r.name from users u, roles r, role_users ru " 
     + "where u.username = ? and u.id = ru.users_id and ru.roles_id = r.id "); 
     auth.inMemoryAuthentication() 
     .withUser("admin").password("admin").roles("ADMIN"); 
    } 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
     .csrf().disable() 
     .anonymous().disable() 
     .authorizeRequests() 
     .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll(); 

    } 
    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers(HttpMethod.OPTIONS,"/oauth/token"); 
    } 

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

尤其是最後配置法應該允許我訪問API,並獲得令牌。

可能是什麼問題?感謝所有幫助。

+1

https://stackoverflow.com/questions/30366405/how-to-disable-spring-security-for-particular-url嘗試刪除'anonymous()。disable()' – StanislavL

+0

可悲的是,它沒有工作 –

+0

感謝您的幫助。它可以使用或不使用匿名()。disable()。我剛啓動了項目副本的服務器。一切正常 –

回答

0

我發現問題....問題是我。

這段代碼沒有錯。我剛開始了錯誤的服務器(複製項目)。一切正常。

相關問題