java - 为什么 Spring Security permitAll() 不适用于 OAuth2.0?

标签 java spring spring-boot spring-security spring-security-oauth2

我有一个使用 OAuth2.0 保护的 REST API 我可以使用 http://localhost:8085/auth/token?grant_type=password&username=22@gmail.com&password=mypass 获取访问 token (与用户名一起通过基本身份验证)。
但是当我尝试访问 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();
    }

}

最佳答案

我明白了。这是导致问题的上下文路径。我有一个用映射 URL /api/v1/* 定义的调度程序 servlet,并且可以看到我的 signup 请求,它包含一个上下文路径,即 http ://localhost:8085/api/v1/signup

对于 Spring 中的 OAuth2 配置,我们需要特别注意上下文路径。首先应该在AuthorizationServer中定义

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  @Override
  public void configure(final AuthorizationServerEndpointsConfigurer endpoints) { 
        endpoints
        .prefix("/api/v1") //here
        .tokenStore(tokenStore())
        .accessTokenConverter(accessTokenConverter())
        .authenticationManager(authenticationManager)
        .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
  }

然后,上下文必须像这样添加到permitAll()路径

@Override
public void configure(final HttpSecurity http) throws Exception {
    http
    .authorizeRequests()
    .antMatchers("/api/v1/signup").permitAll()  //context path here
    .anyRequest().authenticated();
}

到目前为止,注册请求仍然需要传递一个访问 token 。为了从注册中删除 OAuth 安全性,我们需要在 WebSecurity 中删除安全性,这可以使用 WebSecurityConfigurerAdapter

来完成
@EnableWebSecurity
@EnableGlobalMethodSecurity
@ComponentScan(basePackages = { "com.sample.rest.security" })
public class SecurityConfig extends WebSecurityConfigurerAdapter {

     @Override
     public void configure(WebSecurity webSecurity) throws Exception {
        webSecurity.ignoring().antMatchers("/signup");
     }
 //////////// OR use below method ///////////
/*  @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
        authorizeRequests().antMatchers("/signup/**").permitAll();
    }
*/
}

请注意,将上下文路径添加到 WebSecurityConfigurerAdapter 配置是没有用的。

关于java - 为什么 Spring Security permitAll() 不适用于 OAuth2.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53451123/

相关文章:

java - 如何查找网站中页码的大小

java - classNotFoundException : org. springframework.core.io.Resource when intellij idea deploy maven spring mvc web project on tomcat7

java - Spring Boot嵌入式tomcat将日志附加到每个tomcat请求

java - 添加模块描述符时没有名为 'entityManagerFactory' 的 bean 可用

java - 每秒更新一次 MySQL 数据库

java - 在 Java 中解析 JSON 的问题

html - 如何在 Spring <form :input> tag on jsp page 中使用占位符 HTML

java - 不使用响应 bean 的 Rest API 消费

java - 如何配置gradle在不同的环境中使用不同的log4j.properties文件?

java - Android:启动新 Activity 时关闭 Activity ?