【问题标题】:Unauthorized OPTIONS-request未经授权的 OPTIONS 请求
【发布时间】:2017-11-21 13:12:21
【问题描述】:

我尝试使用带有 angular2 的 Oauth2-Authentication 访问我的 Spring-Boot-application。当我使用我的基本身份验证(包括用户名和密码)向 “oauth/token” 发送后请求以获取令牌时,这在邮递员中工作正常,我得到一个 401 Unauthorized。我知道我的浏览器使用 OPTIONS 方法发送了一个预检请求,并且我已经实现了我的 security-configuration 以便它应该忽略并允许选项请求,但它不起作用。

这是我的安全配置:

@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 并获取令牌。

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

【问题讨论】:

  • stackoverflow.com/questions/30366405/… 尝试删除'anonymous().disable()'
  • 很遗憾,它没有用
  • 感谢您的帮助。无论有无anonymous().disable(),它都可以工作。我刚刚启动了项目副本的服务器。一切正常

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


【解决方案1】:

我发现了问题....问题出在我身上。

这段代码没有错。我刚刚启动了错误的服务器(复制项目)。一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2012-12-04
    • 2021-05-12
    • 2021-12-29
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多