`
GodJohnny
  • 浏览: 92253 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

基于SpringSecurityOAuth2构建SSO

 
阅读更多
参考资料:
  • http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html
  • http://projects.spring.io/spring-security-oauth/docs/oauth2.html
  • https://tools.ietf.org/html/rfc6749
  • https://tools.ietf.org/html/rfc6750
  • http://tutorials.jenkov.com/oauth2/index.html
  • http://stackoverflow.com/questions/12296017/how-to-validate-an-oauth-2-0-access-token-for-a-resource-server
  • http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/
  • http://blog.leapoahead.com/2015/09/07/user-authentication-with-jwt/
  • http://www.slideshare.net/rcandidosilva/javaone-2014-securing-restful-resources-with-oauth2


项目背景
   改造一个老的遗留系统,拆分出各个功能模块,以微服务的方式进行整合。根据项目状况,只能将登录系统放在最后进行重构。各个拆分出的微服务模块,需要进行SSO安全校验。为避免存在老系统、SSO服务器两次登录的情况。计划对老的登录系统进行少量的修改,对用户名、口令的验证转给SSO进行登录验证。SSO验证成功后,返回access_token,在向各个微服务请求时,需要带上该token。
方案验证
   之前并未接触过OAuth2.0、Spring Security相关的技术,在查询一些资料,尤其是官方的RFC6749,RFC6750。经过详细阅读后,认为方案理论上是可行的。只是OAuth的授权模式,需要改为Resource Owner Password Credentials或者Client Credentials。而现在绝大部分的demo,介绍都是Authorization Code方式的。另外access_token改为JSON Web Token的方式,可以避免各个微服务频繁地到SSO处进行验证。http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/中的例子,稍做修改,将TOKEN改为JWT的形式。在微服务中设置jwt的public key与SSO中的public key一致即可。
总结
   在进行demo演示该SSO方案时,花了不少时间。先是了解Spring Security的验证逻辑以及如何验证JSON形式的登录。然后研究OAUTH2.0时,花的时间稍多。现在回头看,首先,需要仔细研究下OAUTH2.0的规范,了解各个名词的准确含义,对理解整个流程规范是很有帮助的。正因为没有准确理解名词的含义、流程规范。浪费不少时间在研究如何用Authorization Code的方式去实现我们的方案上。后来回头又看了看方案,最终才锁定以Password或Client Credentials的形式进行验证。并以此为方向查找相关资料,并最终完成demo。
----------------------------------------------------------------------------
    authorization code流程:
    1、浏览器登录,登录页面A。
    2、Client(即页面A所在的服务器)判断未登录,跳转至Client默认的登录页Login。
    3、重定向至Auth Server的授权页authorize,并附带上client_id=acme&redirect_uri=http://localhost:8080/login&response_type=code&state=CjajQx参数
    4、再次重定向至AuthServer的登录页AuthLogin
    4、登录成功,跳转至授权页authorize,要求确认授权。
    5、确认授权后。返回至Client跳转过来时的页面Login,并返回code,state
    6、如果是初次登录,则Client将再次跳转至用户登录时默认输入的页面A。

    7.1、如果这时,直接访问AuthServer的授权页authorize,则直接弹出确认授权页面,因为已经登录过。
    7.2、如果没有登录过,则跳转至AuthServer的登录页

====================   补充  ========================================
基于Spring boot的OAuth2.0的实现JWT验证的细节备忘
    1、Gradle中添加包引用:
compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.0.10.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.4.RELEASE'
     2、在Spring boot的启动处,添加@EnableResourceServer注解,标识该服务为资源服务器
     3、在application.yml或application.properties中添加public key。如果是YAML格式,需要注意一下公钥的写法。参见:https://symfony.com/doc/current/components/yaml/yaml_format.html

    

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics