未经授权的OPTIONS-请求

时间:2017-06-19 10:48:11

标签: java spring spring-security oauth-2.0 cors

我尝试使用带有angular2的Oauth2-Authentication访问我的Spring-Boot应用程序。当我使用我的基本身份验证(包括用户名和密码)向“oauth / token”发送请求后获取令牌,这在邮递员中工作正常时,我得到 401 Unauthorized 。我知道我的浏览器使用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();
    }
}

特别是最后一个configure-method应该允许我访问api并获得一个令牌。

可能是什么问题?感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我发现了问题......问题在于我。

此代码没有任何问题。我刚开始错误的服务器(复制项目)。一切正常。

相关问题