spring security & oauth2 安全认证机制
2018-02-24 17:42
465 查看
基本概念:(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
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
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
模式:client-credentials 。该模式不存在用户的概念,仅仅设计到客户端,授权服务器,资源服务器概念。
授权服务器配置和资源服务器配置:
使用client 模式需要配置授权服务和资源服务:
授权服务器配置:
@Configuration @EnableAuthorizationServer protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { @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) throws Exception { clients.withClientDetails(clientDetailsService); } @Bean public TokenStore tokenStore() { return new RedisTokenStore(); } }
资源服务器配置: @Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
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
相关文章推荐
- SCWCD(Servlet&Jsp learning)第三问-------J2EE 安全认证机制
- Spring Web Flow 2简化页面流的开发,结合Spring MVC更俊,Spirng Security 3添加安全机制
- 第十二篇 Spring Web Flow 2简化页面流的开发,结合Spring MVC更俊,Spirng Security 3添加安全机制
- Spring Web Flow 2简化页面流的开发,结合Spring MVC更俊,Spirng Security 3添加安全机制
- Spring Web Flow 2简化页面流的开发,结合Spring MVC更俊,Spirng Security 3添加安全机制
- [安全] 使用 SpringBoot + SpringSecurity 做登录认证
- 权限解决方案:Spring Security3.0.…
- Spring 3之MVC & Security简单整合开发(二)
- web安全认证机制知多少
- web.xml中<security-constraint>和四种认证类型
- web.xml中<security-constraint>和四种认证类型
- Spring 3之MVC & Security简单整合开发(一)
- spring boot 入门之security oauth2 jwt完美整合例子-java编程
- spring&nbsp;security+cas&nbsp;中…
- Linux下的高级安全认证机制你用了吗?!
- Security_MQ的安全机制
- 权限解决方案:Spring security3.0.…
- WCF传输安全(Transfer Security)的基本概念和原理:认证(Authentication)[上篇]
- spring-security中的csrf防御机制
- Spring 3之MVC & Security简单整合开发(一)