SpringCloud微服务——基于security+oauth2的安全保护(三):资源服务
在实现方式一文中,已经说明了资源服务的代码结构。现在开始具体的实现。加入jar包在公用的pom文件中,引入jar包:<!-- security oauth2 start --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring...
在 实现方式一文中,已经说明了资源服务的代码结构。现在开始具体的实现。
加入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.**" }, excludeFilters = {
@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的,完全可以不用加这个地址的(事实证明,不加会报错)。因此可能是某个配置没有配上,试过一些方法,但是没有实现,继续探索!!!
更多推荐
所有评论(0)