SpringCloud微服务——基于security+oauth2的安全保护(三):资源服务
文章目录
在实现方式一文中,已经说明了资源服务的代码结构。现在开始具体的实现。
加入jar包
在公用的pom文件中,引入jar包:
<!-- security oauth2 start --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> <!-- security oauth2 end -->
一般来说所有的微服务都是资源服务,所以都需要这个jar包。但是像zuul网关服务,在我这里是不需要这种资源服务的(它根本不需要security oauth2来保护),所以对于这种服务(整个应用中,这种服务毕竟是少数),可以将该jar包排除:
<dependency> <groupId>fyk-plat</groupId> <artifactId>fyk-core</artifactId> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </exclusion> </exclusions> </dependency>
配置类
配置类都写在公用的代码中,一般来说,我的所有微服务都需要这个资源服务配置。当然也存在极个别不需要这个配置的服务(比如zuul网关服务),这个时候,可以在启动类中,使用@Filter将该配置排除:
@EnableEurekaClient @SpringBootApplication @ComponentScan(value = { "com.boco.fyk.auth.**", "com.boco.fyk.core.**" }, excludeFil 3ff7 ters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = { OAuth2ResourceConfig.class, OAuth2ClientConfig.class }) }) @MapperScan("com.boco.fyk.auth.business.dao") public class ZuulApplication { ...... }
现在逐步来讲解着两个配置类。
配置资源服务:OAuth2ResourceConfig
@Configuration @EnableResourceServer @EnableGlobalMethodSecurity(prePostEnabled = true) public class OAuth2ResourceConfig extends ResourceServerConfigurerAdapter { @Autowired private SecuritySettings settings; @Override public void configure(HttpSecurity http) throws Exception { // @formatter:off if(settings.getPermitAll()) { // 所有资源可访问 http.authorizeRequests().anyRequest().permitAll(); }else { http .authorizeRequests() // 允许一些资源可以访问 .antMatchers(settings.getWhiteResources().split(",")).permitAll() // 允许一些URL可以访问 .antMatchers(settings.getPermital().split(",")).permitAll() // 跨站请求伪造,这是一个放置跨站请求伪造的攻击的策略设置 .and().csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER) // 设置一个拒绝访问的提示 .and().exceptionHandling().authenticationEntryPoint( (request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED)) .and().authorizeRequests().anyRequest().authenticated(); } // @formatter:on } }
资源配置类最终是要实现ResourceServerConfigurer接口的,这里使用的是继承ResourceServerConfigurerAdapter ,它是ResourceServerConfigurer接口的一个简单实现。
- @EnableResourceServer:开启了Resource Server功能;
- @EnableGlobalMethodSecurity(prePostEnabled = true):开启了方法级别的保护
这里的SecuritySettings配置类里配置的是一些项目的安全信息:比如有哪些资源是不需要保护的。可以根据实际情况添加,他也是在公共配置模块中。
配置资源服务客户端:OAuth2ClientConfig
在我的应用中,存在应用与应用之间的相互调用(通过Feign来实现)。比如:A资源服务调用B资源服务的方法,而B也可能抵用A。所以,这里在资源服务中配置客户端信息,以便可以访问被OAuth2保护起来的资源:
@Configuration @EnableOAuth2Client @EnableConfigurationProperties public class OAuth2ClientConfig { /** * 受保护资源的配置信息(来源于application.properties) * @author FYK * @return */ @Bean @ConfigurationProperties(prefix = "security.oauth2.client") public ClientCredentialsResourceDetails clientCredentialsResourceDetails() { return new ClientCredentialsResourceDetails(); } /** * 在feign调用的时候,也加入认证信息 * @author FYK * @return */ @Bean public RequestInterceptor oauth2FeignRequestlnterceptor() { return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), this.clientCredentialsResourceDetails()); } /** * 在Request域内,创建AccessTokenRequest类型的Bean * @author FYK * @return */ @Bean public OAuth2RestTemplate clientCredentialsRestTemplate() { return new OAuth2RestTemplate(clientCredentialsResourceDetails()); } }
@EnableOAuth2Client:开启了OAuth2 Client功能;并配置了一个ClientCredentialsResourceDetials类型的Bean,从该Bean的源码中可以知道,它是读取配置文件中前缀为security.oauth2.client的配置来获取bean的配置属性的。因此,需要加入一些配置。
资源服务配置
由于整个项目中都引入了配置中心,所有,只需要在数据库中执行一个sql就完事。现在给出这个在application.properties中的配置信息:
spring.zuul.ip.address=192.168.13.193:8080 security.oauth2.resource.token-info-uri=http://${spring.zuul.ip.address}/fyk-oauth/oauth/check_token security.oauth2.client.client-id=xxxxxx security.oauth2.client.client-secret=xxx security.oauth2.client.grant-type=refresh_token,password,client_credentials security.oauth2.client.scope=server security.oauth2.client.access-token-uri=http://${spring.zuul.ip.address}/fyk-oauth/oauth/token security.oauth2.client.user-authorization-uri=http://${spring.zuul.ip.address}/fyk-oauth/oauth/authorize
到这里为止,就算是完成了,重新对整个项目重新打包,上服务,就OK了。
问题
这里有个问题没有解决:
- 在进行资源服务配置的时候,有个配置spring.zuul.ip.address=192.168.13.193:8080。该配置是网关的地址(也就是前端访问服务的地址),然后之后的配置,在访问fyk-oauth认证服务的时候都要加上这个地址。按理说这些服务都是注册到同一个注册中心eureka的,完全可以不用加这个地址的(事实证明,不加会报错)。因此可能是某个配置没有配上,试过一些方法,但是没有实现,继续探索!!!
- SpringCloud微服务——基于security+oauth2的安全保护(四):授权服务之redis存储token
- SpringCloud微服务——基于security+oauth2的安全保护(六):JWT方式下获取登录人信息
- SpringCloud微服务——基于security+oauth2的安全保护(五):授权服务之JWT
- SpringCloud Finchley 实战入门(基于springBoot 2.0.3)【五 Hystrix 服务容错保护】
- spring cloud (二、服务注册安全demo_eureka)
- 服务容错保护:Spring cloud hystrix
- 基于spring-cloud实现eureka注册服务小案例
- 高可用服务架构设计(16) - 基于timeout机制来为商品服务接口的调用超时提供安全保护
- Spring Cloud构建微服务架构:服务容错保护(Hystrix依赖隔离)
- Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)【Dalston版】
- SpringCloud之服务容错保护Spring Cloud Hystrix实例
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十三)使用Spring Security安全控制
- 笔记:Spring Cloud Hystrix 服务容错保护
- 十七、Spring Cloud服务容错保护:Spring Cloud Hystrix
- SpringCloud系列三:SpringSecurity 安全访问(配置安全验证、服务消费端处理、无状态 Session 配置、定义公共安全配置程序类)
- Spring cloud微服务实战——基于OAUTH2.0统一认证授权的微服务基础架构
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(七)服务容错保护(Hystrix服务降级)【Dalston版】
- Spring Cloud构建微服务架构服务容错保护
- SpringCloud Finchley 实战入门(基于springBoot 2.0.3)【七 Fegin 声明式服务调用】
- SpringCloud Finchley 实战入门(基于springBoot 2.0.3)【九 zuul 微服务网关配置】