小编典典

将WebSecurityConfigurerAdapter与Spring OAuth2和user-info-uri结合使用

spring-boot

我创建了一个授权服务,如下所示

@SpringBootApplication
@EnableAuthorizationServer
public class AuthorizationApplication {
   ...
}

有了这个application.properties

server.port=9000
security.oauth2.client.client-id=monederobingo
security.oauth2.client.client-secret=monederobingosecret
security.oauth2.client.authorized-grant-types=authorization_code,refresh_token,password,client_credentials
security.oauth2.client.scope=company,client

然后,在一个单独的spring boot项目中,我创建了一个资源服务器。

@SpringBootApplication
@EnableResourceServer
public class App {
   ...
}

有了这个application.properties

server.port=9090
spring.application.name=app
security.oauth2.resource.user-info-uri=http://localhost:9000/user

现在,如果我发送localhost:9090/api带有授权服务检索到的适当令牌的此类请求,一切正常。

但是,我不想在向发送请求时发送此令牌localhost:9090/login

为此,我在资源服务器spring boot应用程序中创建了此类。

@Configuration
public class SpringConfig extends WebSecurityConfigurerAdapter {
    @Override protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/login")
                .permitAll()
                .antMatchers("/api/**")
                .authenticated();
    }

}

现在,我无需发送任何令牌即可向发送请求/login

但是,当/api使用有效令牌向发送请求时,我现在收到以下消息。

{
  "timestamp": 1496027102659,
  "status": 403,
  "error": "Forbidden",
  "message": "Access Denied",
  "path": "/api/v1/points_configuration/314"
}

如何在Spring Security OAuth2中仅为少数几个URL模式配置安全性?


阅读 1091

收藏
2020-05-30

共1个答案

小编典典

请关注此以获取有关Spring OAuth安全性的更多信息: 使用OAuth保护Spring REST
Api

为了在Spring启动中实现OAuth安全性,您必须通过分别从AuthorizationServerConfigurerAdapter和扩展它们来创建授权和资源服务器ResourceServerConfigurerAdapter

授权服务器

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationApplication extends AuthorizationServerConfigurerAdapter{

    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
                throws Exception {
            endpoints
                    .userDetailsService(userDetailsService)
                    .authenticationManager(this.authenticationManager).tokenStore(tokenStore()).approvalStoreDisabled();
        }

       @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.withClientDetails(mongoClientDetailsService);
            /*inMemory()
                    .withClient(propertyResolver.getProperty(PROP_CLIENTID))
                    .scopes("read", "write")
                    .authorities("ROLE_CLIENT")
                    .authorizedGrantTypes("password", "refresh_token","client_credentials")
                    .secret(propertyResolver.getProperty(PROP_SECRET))
                    .accessTokenValiditySeconds(propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 18000));*/
        }

//Do others stuff
    }

资源服务器

此服务器配置中应提及您要使用OAuth保护的所有网址。它启用了一个Spring
Security过滤器,该过滤器使用传入的OAuth2令牌对请求进行身份验证。虽然大多数WebSecurityConfigurerAdapter扩展类用于基本安全配置,例如添加过滤器,允许使用不安全的url或实现会话策略等。

@Configuration
@EnableResourceServer
public class App extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
    http.requestMatchers().antMatchers("/api/**").and().authorizeRequests()
                .antMatchers("/api/**").authenticated();
}
  //Do others stuff
}
2020-05-30