【发布时间】:2019-04-26 07:52:15
【问题描述】:
我有一个使用 OAuth2.0 保护的 REST API
我可以使用 http://localhost:8085/auth/token?grant_type=password&username=22@gmail.com&password=mypass(连同用户名通过基本身份验证)获取访问令牌。
但是当我尝试访问 http://localhost:8085/api/v1/signup 时,API 会返回一个 401 unauthorized 错误。
虽然我使用了antMatchers("/signup").permitAll(),但为什么API 期望access-token 访问该资源?将access-token 与此请求一起传递将注册一个用户。
这是我的资源服务器配置
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
//require beans and methods here
@Autowired
public void configureGlobal(final AuthenticationManagerBuilder auth) {
auth.authenticationProvider(authProvider());
}
@Override
public void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/signup").permitAll()
.anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.csrf().disable();
}
}
更新:正如this 线程所建议的,我在 `` 处忽略了/signup,但这也没有用。
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@ComponentScan(basePackages = { "com.sample.rest.security" })
@Order(2)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//other Beans & methods
@Override
protected void configure(HttpSecurity http) throws Exception {
List<RequestMatcher> requestMatchers = new ArrayList<RequestMatcher>();
requestMatchers.add(new AntPathRequestMatcher("/signup/**"));
http.
requestMatcher(new OrRequestMatcher(requestMatchers)).
authorizeRequests().antMatchers("/signup/**")
.permitAll();
}
}
【问题讨论】:
标签: java spring spring-boot spring-security spring-security-oauth2