您现在的位置是:首页 > 文章详情

java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security

日期:2018-12-10点击:314

一、SpringCloud Security简介
          Spring Cloud Security提供了一组原语,用于构建安全的应用程序和服务,而且操作简便。可以在外部(或集中)进行大量配置的声明性模型有助于实现大型协作的远程组件系统,通常具有中央身份管理服务。它也非常易于在Cloud Foundry等服务平台中使用。在Spring Boot和Spring Security OAuth2的基础上,可以快速创建实现常见模式的系统,如单点登录,令牌中继和令牌交换。愿意了解源码的朋友直接求求交流分享技术:二一四七七七五六三三

功能:

从Zuul代理中的前端到后端服务中继SSO令牌
资源服务器之间的中继令牌
使Feign客户端表现得像OAuth2RestTemplate(获取令牌等)的拦截器
在Zuul代理中配置下游身份验证

二、SpringCloud Security知识点

封装顺序是这样的:spring security及其各个模块=》spring cloud security=》spring boot autoconfigure的security部分,比如autoconfigure模块有个spring security的sso,是对spring security在oath2下的封装

spring oauth2 authorizeserver,resourceserver,认证服务器和资源服务器是怎么交互的,token_key,/oauth/token/ refresh/token

resourceserver就是业务系统,oauth client 就是边缘业务系统,比如直接面向用户的UI系统,或者UI系统直接调用的API接口这一层;

JWT和OAuth2的区别?看到好多人在比较这两个东西,现在终结这个问题:JWT只是OAuth2中的token的一种类型。

jwt client(Resource Server或者Zuul等),使用jwt的业务系统在解码acces_token的时候,需要一个toke_key,这个token-key就是JWT Client应用启动的时候从Auth Server拉取的,接口是token/token_key

单点登录这种功能,好多javaee容器都自带的,像websphere
spring Security完全将认证和授权分开了,资源只需要声明自己是需要认证的,需要什么样的权限,只管跟当前用户要access_token。

授权的四种方式任意,只要拿到token就可以去让资源去认证。

边缘服务器需要开启@EnableOAuth2Sso,但是边缘服务器也是一个ResourceServer,边缘服务器如果是zuul的话,就不是一个ResourceServer了,只需要添加@EnableOAuth2Sso注解就可以了;

client_credentials模式下spring boot不会帮助spring Security构建ClientCredentialsResourceDetails 对象,需要开发者自己创建

favicon.icon,记得把这个东西过滤掉,奶奶的

在其中一个边缘服务上,您可能需要启用单点登录。

@EnableOAuthSso,只需要在边缘服务器指定没用来引导未登录的用户登录系统。@EnableOAuthSso将允许您将未经认证的用户的自动重定向转向授权服务器,他们将能够登录
EnableOAuth2Client,在中间中继的时候用
ClientCredentialsTokenEndpointFilter,AS设置了allowFormAuthenticationForClients才会有,详情看这里面的AuthorizationServerSecurityConfigurer#configure(HttpSecurity http)逻辑,这点非常重要,ClientCredentialsTokenEndpointFilter是用来验证clientid和client_secret的,使用clientid和client_secret换取下一步的东西;

TokenGranter,AuthorizationCodeTokenGranter,ClientCredentialsTokenGranter,RefreshTokenGranter,ImplicitTokenGranter,ResourceOwnerPasswordTokenGranter

TokenServices分为两类,一个是用在AuthenticationServer端,AuthorizationServerTokenServices,ResourceServer端有自己的tokenServices接口,

BearerTokenExtractor,从其可以看出,token的获取顺序,Header,parameters(get/post)

spring security 保护自己的配置,作为ResourceServer的权限配置和作为AuthorizationServer的配置都是在不同的地方

An OAuth 2 authentication token can contain two authentications: one for the client(OAuth2 Client) and one for the user. Since some OAuth authorization grants don’t require user authentication, the user authentication may be null.

jwt是不需要存储access_toen的,jwt的机制就是将所有的信息都存在了token里面,从JwtTokenStore也可以看出来

OAuth2AuthenticationManager是密切与token认证相关的,而不是与获取token密切相关的。这是真正认证的地方,一会重点debug,resourceIds

每一个ResourceServer在配置的时候,

ResourceServerConfiguration,需要配置一个resourceID,一个ResourceServer只能配置一个

oauth/token = 先验证的是clientid和clientsecret,接着在验证username和userpassword,都是用的ProvideManager,两次验证是两个不同的请求,oauth2客户端会使用RestTemplate请求服务器的接口

ClientCredentialsTokenEndpointFilter用来验证clientId和clientsecret的: 

OAuth2ClientAuthenticationProcessingFilter:OAuth2客户端用来从OAuth2认证服务器获取access token,也可以从OAuth2认证服务器加载authentication对象到OAuth2客户端的SecurityContext对象中;里面调用OAuth2AuthenticationManager#authenticate()方法使用DefaultTokenServices ,DefaultTokenServices 使用JwtTokenStore,JwtTokenStore使用JwtAccessTokenConverter来将JWT解密成Auth对象。 来从AuthServer请求授权信息
accessToken被解密成如下的JWT对象: 

{“alg”:”RS256”,”typ”:”JWT”} {“exp”:1503758022,”user_name”:”admin”,”authorities”:[“ROLE_TRUSTED_CLIENT”,”ROLE_ADMIN”,”ROLE_USER”],”jti”:”d56f43d2-6c4a-46cf-85f3-050ee195a2bd”,”client_id”:”confidential”,”scope”:[“read”]} [128 crypto bytes]

AbstractSecurityInterceptor#befroeInvaction 是ResourceServer获取认证信息的地方

只要是需要验证token有效性的都需要jwt.key-uri的配置

AffirmativeBased值得debug
 
TIPS

@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

整体代码结构如下:资料和源码来源 
20181210091941557

原文链接:https://yq.aliyun.com/articles/676039
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章