@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/favicon.ico");
}
@Bean
@Override
protected UserDetailsService userDetailsService(){
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("user").password("password").roles("USER").authorities("USER").build());
manager.createUser(User.withUsername("admin").password("password").roles("USER", "ADMIN", "TRUSTED_CLIENT").authorities("USER").build());
return manager;
}
这两种方式有差别,牵扯到UsernamePasswordAuthenticationFilter和ClientCredentialsTokenEndpointFilter
security:
oauth2:
client:
client-id: confidential
client-secret: secret
access-token-uri: http://localhost:8080/oauth/token
user-authorization-uri: http://localhost:8080/oauth/authorize
use-current-uri: true
resource:
jwt:
key-uri: http://localhost:8080/oauth/token_key
filter-order: 3
client里面的配置最终是用来生成OAuth2ProtectedResourceDetails的bean的,参看OAuth2ProtectedResourceDetailsConfiguration
@EnableOAuth2Sso or @EnableOAuth2Client
1. EnableOAuth2Sso
OAuth2ClientAuthenticationProcessingFilter
ResourceServer配置
2. EnableOAuth2Client
OAuth2ClientContextFilter
Authorization Server
security.oauth2.client.client-id和security.oauth2.client.client-secret,用于设置一对ResourceServer 用来认证的Authorization Server的clientid和client secret
security.oauth2.client.client-authentication-scheme:form/header/none/query
ResourceServer
security.oauth2.resource.user-info-uri:userinfo的url地址
security.oauth2.resource.token-info-uri:check token的url地址
security.oauth2.resource.token-type: 设置token type ,比如Bearer
如果token类型是jwt的话:
security.oauth2.resource.jwt.key-value:symmetric secret or PEM-encoded RSA public key
security.oauth2.resource.jwt.key-uri:获取key的地址
sso:
security.oauth2.sso.login-path
security.oauth2.sso.filter-order
Spring Cloud Security一共有两个starter:
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-oauth2artifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-securityartifactId>
dependency>
这两个starter牵扯到三个具体的实现:
<dependency>
<groupId>org.springframework.security.oauthgroupId>
<artifactId>spring-security-oauth2artifactId>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-jwtartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-securityartifactId>
<version>${project.version}version>
dependency>
前两个属于Spring Security的体系架构,第三个是对feign和zuul做了封装。
spring cloud Security主要的任务是为spring cloud 中的各个组件提供token的中继功能,zuul,feign,RestTemplate
token 中继有几种类型,请求产生新的请求时,将access_token的值传递过去,这样被请求的被保护的资源就不用再去authorize server上获取新的access_token了;
OAuth2TokenRelayFilter,一个pre类型的ZuulFilter,extracts an access token from the currently authenticated user, and puts it in a request header for the downstream requests.
AuthenticationHeaderFilter,一个pre类型的ZuulFilter。
ZuulFilter将自己收到的请求里的access_token提取出来,放到请求service时的请求的header里
proxy:
auth:
routes:
customers: oauth2
stores: passthru
recommendations: none
可以为每一个代理设置授权方式。
OAuth2FeignRequestInterceptor,在header里添加token,token是从AuthorizationServer获取的。
ResourceServer中继成功:
被调用的ResourceServer上的OAuth2AuthenticationProcessingFilter,抽取请求里的header的bear access_token信息,并转换为Authentication成功Authentication authentication = tokenExtractor.extract(request);
Zuul+ResourceServer中继成功
代理了两个微服务,调用其中一个服务授权流程走完,调用另一个服务时,已经授权,直接返回调用信息
Client中继成功
可以走完授权流程,调用成功
问题1
Possible CSRF detected - state parameter was required but no state could be found
答:code重复使用,也就是不停刷新授权后的界面导致的
https://github.com/spring-projects/spring-security-oauth/issues/322 这个issue解决了问题
使用OAuth2的SSO分析
使用JWT的OAuth2的SSO分析
Spring Cloud Security
使用 OAuth 2 和 JWT 为微服务提供安全保障
OAuth 2和JWT - 如何设计安全的API?
How to use JWT and OAuth with Spring Boot
Advanced Microservices Security with Spring and OAuth2
使用zuul及oauth2构建api网关实践之路
zuul 做权限服务的时候大家是用自定义的ZuulFilter + jwt 去做还是用spring-security-oauth2 去做的
spring cloud单点登录
OAuth 2 Developers Guide
Spring Cloud:Eureka,Zuul和OAuth2 - 扩展授权服务器
FeignClient 在 oauth2 中与 hystrix 线程策略冲突问题造成的权限问题
feign-oauth2-spring-cloud-starter