统一授权中心微服务器,SpringCloud微服务实战(一)基于OAUTH2.0统一认证授权的微服务基础架构...

1.架构图

技术团队通过一段时间的积累后,我们打算对往后的一些新项目采用Spring Cloud技术栈来实现。大概微服务的架构如下:

b2bf674fe7774386e49dafa19fa030d4.png

Euraka注册中心集群

Zuul网关集群

各模块微服务集群

Nginx实现负载均衡

Spring Cloud Config 统一配置中心

Monitor微服务监控

2.注册中心

注册中心很简单,这里主要说一下注册中心的高可用配置‘

6eebeb58f8b90a4c692c026d71500a67.png

这里看到我设置了node-1,node-2两个配置文件,就是在启动应用的时候,分别启动不同的配置。

node-1的端口为9010,并向node-2注册,配置如下:

server:

port: 9010

spring:

application:

name: register ##name必须一样,不然高可用会导致unavailable-replicas

eureka:

instance:

hostname: register1

client:

register-with-eureka: true

fetch-registry: true

service-url:

defaultZone: http://register2:9011/eureka/

node-2的端口为9011,并向node-1注册,配置如下:

server:

port: 9011

spring:

application:

name: register ##name必须一样,不然高可用会导致unavailable-replicas

eureka:

instance:

hostname: register2

client:

register-with-eureka: true

fetch-registry: true

service-url:

defaultZone: http://register2:9010/eureka/

这里注意一下:spring.application.name需要一致,不然会出现unavailable-replicas的情况

3.OAUTH2认证服务器

我这里采用认证服务器与资源服务器分离的方式。

3.1 oauth2 server 配置

我采取了数据库和redis两种方式来存储token,可以方便切换,生成环境下建议使用redis方式。

AuthorizationServer:

@Configuration

@EnableAuthorizationServer

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

[email protected]

private AuthenticationManager authenticationManager;

[email protected]

private DataSource dataSource;

[email protected]

private UserDetailsServiceImpl userDetailsService;

[email protected]

private RedisConnectionFactory redisConnectionFactory;

[email protected]

RedisTokenStore redisTokenStore(){

return new RedisTokenStore(redisConnectionFactory);

}

//token存储数据库

// @Bean

// public JdbcTokenStore jdbcTokenStore(){

//   return new JdbcTokenStore(dataSource);

// }

@Override

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

clients.withClientDetails(clientDetails());

}

@Bean

public ClientDetailsService clientDetails() {

return new JdbcClientDetailsService(dataSource);

}

@Override

public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.tokenStore(redisTokenStore())

.userDetailsService(userDetailsService)

.authenticationManager(authenticationManager);

endpoints.tokenServices(defaultTokenServices());

}

/**

*

注意,自定义TokenServices的时候,[email protected],否则报错,

* @return

*/

@Primary

@Bean

public DefaultTokenServices defaultTokenServices(){

DefaultTokenServices tokenServices = new DefaultTokenServices();

tokenServices.setTokenStore(redisTokenStore());

tokenServices.setSupportRefreshToken(true);

tokenServices.setClientDetailsService(clientDetails());

tokenServices.setAccessTokenValiditySeconds(60*60*12); // token有效期自定义设置,默认12小时

tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 7);//默认30天,这里修改

return tokenServices;

}

@Override

public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

security.tokenKeyAccess("permitAll()");

security .checkTokenAccess("isAuthenticated()");

security.allowFormAuthenticationForClients();

}

}

WebSecurityConfig:

@Configuration

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

private UserDetailsServiceImpl userDetailsService;

@Bean

public PasswordEncoder passwordEncoder() {

return new BCryptPasswordEncoder();

}

@Override

@Bean

public AuthenticationManager authenticationManagerBean() throws Exception {

return super.authenticationManagerBean();

}

@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.userDetailsService(userDetailsService)

.passwordEncoder(passwordEncoder());

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http

.authorizeRequests()

.anyRequest().authenticated()

.and()

.formLogin().and()

.csrf().disable()

.httpBasic();

}

@Override

public void configure(WebSecurity web) throws Exception {

web.ignoring().antMatchers("/favor.ioc");

}

}

3.2 ResourceServer

因为我们认证中心会提供User信息,所以也是资源服务器。

@Configuration

@EnableResourceServer

public class ResourceServerConfig extends ResourceServerConfigurerAdapter{

[email protected]

public void configure(HttpSecurity http) throws Exception {

http.csrf().disable()

.exceptionHandling()

.authenticationEntryPoint(new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\""))

.and()

.authorizeRequests().anyRequest().authenticated()

.and()

.httpBasic();

}

}

4.资源服务器 Resource

ResourceServer:

@Configuration

@EnableResourceServer

public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

@Override

public void configure(HttpSecurity http) throws Exception {

http.

csrf().disable()

.exceptionHandling()

.authenticationEntryPoint(new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\""))

.and()

.authorizeRequests().anyRequest().authenticated()

.and()

.httpBasic();

}

}

主要是application.yml的配置

security:

oauth2:

resource:

id: resource

user-info-uri: http://10.10.8.2:9030/uaa/user

prefer-token-info: false

5.Zuul网关

5.1开启支持Sso

@Configuration

@EnableOAuth2Sso

public class SecurityConfig extends WebSecurityConfigurerAdapter{

@Override

protected void configure(HttpSecurity http) throws Exception {

http.csrf().disable();

}

}

5.2配置

spring:

application:

name: Gateway

zipkin:

base-url: http://10.10.8.2:9050

server:

port: 9030

eureka:

instance:

prefer-ip-address: true #使用IP注册

instance-id: ${spring.cloud.client.ipAddress}:${server.port}

client:

service-url:

defaultZone: http://register1:9010/eureka/,http://register2:9011/eureka/

###actuator监控点 start####

endpoints:

health:

sensitive: false

enabled: true

##默认情况下很多端点是不允许访问的,会返回401:Unauthorized

management:

security:

enabled: false

###actuator监控点 end####

zuul:

host:

connect-timeout-millis: 10000

socket-timeout-millis: 60000

routes:

uaa:

path: /uaa/**

strip-prefix: true

sensitiveHeaders:

serviceId: auth2.0-center

security:

basic:

enabled: false

oauth2:

client:

access-token-uri: http://10.10.8.2:9030/uaa/oauth/token ##网关的地址

user-authorization-uri: http://10.10.8.2:9030/uaa/oauth/authorize

resource:

user-info-uri: http://10.10.8.2:9030/uaa/user

prefer-token-info: false

##############end#####################

####超时配置####

ribbon:

ReadTimeout: 10000

ConnectTimeout: 10000

MaxAutoRetries: 1

MaxAutoRetriesNextServer: 2

eureka:

enabled: true

hystrix:

command:

default:

execution:

timeout:

enabled: true

isolation:

thread:

timeoutInMilliseconds: 600000

###超时配置###

6.展示

分别启动register、auth-center、gateway、resource。效果如下:

81287a9b9f1b7b374acfd351733bd5db.png

6.1获取access_token

85f55d68c188122fd4c941358626d358.png

6.2刷新token

9e88a6b31159233685dc503cfef91026.png

6.3用access_token获取资源

decb9b33a93958d998dcf800517b07a0.png

04767d8f084c12dee2e381d2ba8c9908.png

7.结束

到这里Spring Cloud OAUTH2.0统一认证的骨架就完成了,具体项目拿过来修改就可以满足项目的需求了。

原文:https://www.cnblogs.com/liboware/p/12535327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值