首页 > 解决方案 > Spring Boot 2.0 / OAuth2 / JWT / 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 新手,但必须学习它才能工作。但目前我只是过度紧张。

附加信息:

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

网关服务:

GatewayServiceApplication.java

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

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

安全配置.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.

应用程序属性

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

我认为这是一个失败。

核心服务

核心应用程序.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");
    }
}

安全配置

@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();
    }
}

应用程序属性

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

好的,尤其是在这里我改变了很多东西。所以可能我从旧指南中破坏了很多。(对不起,我的英语不好!)

标签: javaspring-bootspring-securityspring-security-oauth2netflix-zuul

解决方案


推荐阅读