【问题标题】:How to expose endpoints with spring boot and oauth2如何使用 spring boot 和 oauth2 暴露端点
【发布时间】:2019-07-09 01:12:45
【问题描述】:

我需要在我的 Spring Boot 应用程序上公开几个端点。我使用 oauth2 来实现使用令牌的安全性,但需要几个端点是公共的并且不需要授权令牌。

我已经尝试过(从我找到的几篇文章中)实现这样的 WebSecurityConfigurerAdapter 配置类:

@Configuration
@EnableWebSecurity
class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity httpSecurity) {
    httpSecurity
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers('/actuator/jolokia', '/graphiql', '/voyager')
            .permitAll()
            .anyRequest()
            .authenticated()
}

} ,但无济于事,端点不断要求访问令牌

我用于启用 oauth 的 pom.xml 依赖项是这样的: <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>${spring-security-oauth2.version}</version> </dependency>

另外,这是 oauth 的授权服务器的配置类:

@Component
@EnableResourceServer
@EnableAuthorizationServer
class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Value('${application.oauth.clientId}')
String clientId

@Value('${application.oauth.secret}')
String clientSecret

@Value('${application.oauth.accessTokenExpirationSeconds}')
Integer accessTokenExpirationSeconds

@Value('${application.jwt.key}')
String jwtKey

AuthenticationManager authenticationManager

AuthorizationServerConfiguration(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        this.authenticationManager =     authenticationConfiguration.getAuthenticationManager()
}

@Override
void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder()
    String encoded = passwordEncoder.encode(clientSecret)

    clients.inMemory()
            .withClient(clientId)
            .secret(encoded)
            .authorizedGrantTypes("client_credentials")
            .scopes("all")
            .accessTokenValiditySeconds(accessTokenExpirationSeconds)
}

@Override
void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManager)
            .accessTokenConverter(accessTokenConverter())
}

@Bean
JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter()
    converter.setSigningKey(jwtKey)
    converter.setVerifierKey(jwtKey)

    converter.afterPropertiesSet()
    converter
}

@Bean
TokenStore tokenStore() {
    new JwtTokenStore(accessTokenConverter())
}

【问题讨论】:

  • 你试过没有.antMatcher("/**")吗?
  • 是的,仍在要求令牌

标签: java spring-boot spring-security oauth


【解决方案1】:

在您的 SecurityConfig 中,您需要将单独的语句与 .and() 连接在一起,否则它们将在一个语句中连接在一起。

试试这个:

httpSecurity
  .antMatcher("/**")
  .authorizeRequests()
  .and()
  .authorizeRequests().antMatchers('/actuator/jolokia', '/graphiql', '/voyager').permitAll()
  .and()
  .authorizeRequests().anyRequest().authenticated();

【讨论】:

    猜你喜欢
    • 2018-10-14
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2012-04-26
    • 2020-02-20
    • 2017-04-29
    • 2018-07-10
    相关资源
    最近更新 更多