基本概念:
(1) Third-party application:第三方应用程序,本文中又称"客户端"(client),即上一节例子中的"云冲印"。
(2)HTTP service:HTTP服务提供商,本文中简称"服务提供商",即上一节例子中的Google。
(3)Resource Owner:资源所有者,本文中又称"用户"(user)。
(4)User Agent:用户代理,本文中就是指浏览器。
(5)Authorization server:授权(认证)服务器,即服务提供商专门用来处理认证的服务器。
(6)Resource server:资源服务器,即服务提供商存放用户生成的资源的服务器。它与认证服务器,可以是同一台服务器,也可以是不同的服务器。
模式:client-credentials 。该模式不存在用户的概念,仅仅设计到客户端,授权服务器,资源服务器概念。
授权服务器配置和资源服务器配置:
使用client 模式需要配置授权服务和资源服务:
授权服务器配置:
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extendsAuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private HcClientDetailsService clientDetailsService;
//配置AuthorizationServerEndpointsConfigurer众多相关类,包括配置身份认证器,配置认证方式,TokenStore,TokenGranter,OAuth2RequestFactory
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)throws Exception {
endpoints.tokenStore(tokenStore())
.reuseRefreshTokens(true)//刷新token不失效
.authenticationManager(this.authenticationManager)
.pathMapping("/oauth/token", "/app/appserver/token")
.allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST);
}//authenticationManager->authenticationProvider->userDetailService->clientDetailsUserDetailsService,身份信息已经得到了AuthenticationManager的验证。接着便到达了
TokenEndpoint
//配置AuthorizationServer安全认证的相关信息,创建ClientCredentialsTokenEndpointFilter核心过滤器
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer)throws Exception {
oauthServer.allowFormAuthenticationForClients();
}
//配置OAuth2的客户端相关信息
@Override
public void configure(ClientDetailsServiceConfigurer clients) throwsException {
clients.withClientDetails(clientDetailsService);
}
@Bean
public TokenStore tokenStore() {
return new RedisTokenStore();
}
}
资源服务器配置:
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extendsResourceServerConfigurerAdapter {
private String HC_RESOURCE_ID ="haiercash";
@Value("${common.app.checkAuth}")
private Boolean checkAuth;
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(HC_RESOURCE_ID);
}
@Override
public void configure(HttpSecurity http) throws Exception {
if (checkAuth == null || !checkAuth) {
http.authorizeRequests().antMatchers("/**").permitAll().anyRequest().authenticated();
} else {
http.authorizeRequests()
.antMatchers("/", "/appjs/**","/app/portal/mUser/**", "/app/uauth/**",
"/app/appserver/appmanage/citybean/checkCitySmrz"
)
.permitAll().anyRequest().authenticated().and().headers().frameOptions().sameOrigin();
}
}
}
http://blog.csdn.net/u013815546/article/details/76977239