Spring Cloud Security - 允许请求而无需身份验证

时间:2016-12-24 19:15:21

标签: spring-security spring-cloud spring-cloud-security

我有一个允许用户注册帐户的应用程序。我们的身份验证和用户服务是UAA,因此我需要能够在没有用户实际存在的情况下与其安全端点进行通信。

如何设置Spring Cloud Security以允许从1个微服务调用到另一个微服务,然后与UAA通信以创建用户?

因此,有两个主要的微服务正在发挥作用。第一个托管Web应用程序并使用Zuul将呼叫转发到第二个微服务。此微服务与UAA通信并处理任何其他特定于应用程序的用户请求。

我在第一个微服务(LandingPage)上有这个WebSecurityConfigurerAdapter

@SpringBootApplication
@EnableZuulProxy
@EnableOAuth2Sso
@EnableEurekaClient
@EnableAutoConfiguration
public class LandingPageUiApplication extends WebSecurityConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(LandingPageUiApplication.class, args);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().anyRequest().permitAll();
    }
}

这在第二个微服务(UserInformation)中:

@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients
public class UserInformationServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserInformationServiceApplication.class, args);
    }

    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }
}

不幸的是,我很难在第一个微服务上访问REST端点,也无法将任何内容转发到第二个微服务器上。我通常收到401响应代码。他们各自的application.yaml文件被设置为与UAA作为客户端和资源服务器进行通信

LandingPage Application.yaml

spring:
  application:
    name: Landing Page
  aop:
    proxy-target-class: true

security:
  oauth2:
    client:
      accessTokenUri: http://localhost:8080/uaa/oauth/token
      userAuthorizationUri: http://localhost:8080/uaa/oauth/authorize
      clientId: landing-page
      clientSecret: landing-page-secret
      scope: openid,uaa.admin,uaa.user
    resource:
      userInfoUri: http://localhost:8080/uaa/userinfo

zuul:
  routes:
    users:
      serviceId: USER-INFO-SERVICE
      path: /users/**

server:
  port: 8081

eureka:
  instance:
    hostname: 127.0.0.1
    nonSecurePort: ${server.port}
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${spring.application.name}:${server.port}
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
    region: default
    registryFetchIntervalSeconds: 5

和UserInfoSerevice Application.yaml

server:
  port: 0

security:
  oauth2:
    client:
      clientId: user-info-service
      clientSecret: app-secret
    resource:
      jwt:
        keyUri: http://localhost:8080/uaa/token_key


spring:
  application:
    name: user-info-service
  profiles: development,default
  datasource:
    url: jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    driverClassName: org.h2.Driver
    username: sa
    password:
    database-platform: org.hibernate.dialect.H2Dialect


eureka:
  instance:
    hostname: 127.0.0.1
    nonSecurePort: ${server.port}
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${spring.application.name}:${server.port}
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
    region: default
    registryFetchIntervalSeconds: 5

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

答案是将此WebConfigAdapter设置放在父MS中:

    @Configuration
    @EnableOAuth2Sso
    @EnableAutoConfiguration
    protected static class TestConfiguration extends WebSecurityConfigurerAdapter {


        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.csrf().disable().antMatcher("/**")
                .authorizeRequests()
                .anyRequest().permitAll();
        }

    }

以及孩子MS中的以下内容:

    @Configuration
    @Order(-10)
    @EnableOAuth2Client
    @EnableAutoConfiguration
    protected static class TestConfiguration extends WebSecurityConfigurerAdapter {


        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.csrf().disable().anonymous().authenticationFilter(new AnonymousAuthenticationFilter("HALLO")) //allow anonymous access
                    .and()
                    .authorizeRequests()
                    .antMatchers("/**")
                    .permitAll();
        }
    }
相关问题