Spring Boot 2.0 / OAuth2 / JWT / Zuul /微服务

时间:2018-07-16 18:41:23

标签: java spring-boot spring-security spring-security-oauth2 netflix-zuul

我正在尝试构建微服务架构。但是自与OAuth2和Zuul 两天以来,我一直在苦苦挣扎。我设法在同一服务上运行Auth / Resource-Server,但没有使用Zuul。目前,我正在使用其他服务,授权不再起作用。我拜访了许多指南(例如Baeldung),但没有人对我有用。可能是因为我使用的是 Spring Boot 2.0.3 大多数使用Spring Boot 1.5.x的指南

我认为这是一个配置问题。我正在使用Eureka进行服务发现,Zuul是网关和入口点。当用户请求受保护的服务时,应将其重定向到我的身份验证服务(OAuth2 / JWT)。 他登录后获得的令牌应由Zuul存储(对吗?)。实际上, Zuul没有获得令牌或没有存储令牌。我是否必须自己执行此操作,还是应该由Zuul和OAuth管理此操作,而我的配置却很糟糕?有人可以告诉我您如何配置这种体系结构,或者 Spring Boot 2.0.3的新功能/工作指南吗?我真的很沮丧,需要帮助。我是Spring的新手,但必须学习它才能上班。但是此刻,我只是太紧张了。

其他信息:

我现在没有创建任何视图。我刚刚定义了一些默认控制器,它们返回String并由@PreAuthorize保护。

网关服务:

  

GatewayServiceApplication.java

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
@Configuration
public class GatewayServiceApplication {

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

SecurityConfig.java

@Configuration
@EnableOAuth2Sso
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**")
                .httpBasic().disable()
                .authorizeRequests()
                .antMatchers("/", "/core/", "/core/login**", "/oauth/authorize", 
                 "/core/oauth/authorize", "/login")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin().permitAll();
    }
}

这里我有很多不同的antMatchers

  

application.properties

server.port=8000
spring.application.name=gateway-service
eureka.client.service-url.defaultZone=http://localhost:8001/eureka/
security.oauth2.sso.login-path=http://localhost:8000/core/login
security.oauth2.client.client-id=zuul
security.oauth2.client.client-secret=zuul
security.oauth2.client.access-token-uri=http://localhost:8000/core/oauth/token
security.oauth2.client.user-authorization-uri=http://localhost:8000/core/oauth/authorize
#security.oauth2.resource.user-info-uri=http://localhost:8000/core/user/me
security.oauth2.resource.user-info-uri=http://localhost:8000/core/secured
spring.thymeleaf.cache=false

我认为这是一个失败。

核心服务

  

CoreApplication.java

@SpringBootApplication
@EnableDiscoveryClient
@EnableResourceServer
public class CoreApplication {

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

AuthServerConfig

@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void configure(
            AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("zuul")
                .secret(passwordEncoder.encode("zuul"))
                .authorizedGrantTypes("authorization_code")
                .scopes("user_info")
                .autoApprove(true)
                .redirectUris("http://localhost:8000/core/secured");
    }
}
  

SecurityConfig

@Configuration
@Order(1)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("john")
                .password(passwordEncoder().encode("123"))
                .roles("USER");
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
  

application.properties

server.port=8003
spring.application.name=core
eureka.client.service-url.defaultZone=http://localhost:8001/eureka

好吧,尤其是在这里,我改变了很多东西。因此,大概我从较旧的指南中销毁了很多东西。 (对不起,我的英语不好!)

0 个答案:

没有答案
相关问题