spring security + oauth2.0 + jwt
2018.9.1
版本升到2.0-SNAPSHOT,欲使用sb1.5.x版本,请切换到TAG 1.0-RELEASE。by CANGWU
Spring Cloud Security 升级到Finchley.RELEASE,Spring Boot由1.5.X升级到2.0.X。
org.springframework.cloud
spring-cloud-dependencies
Finchley.RELEASE
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
本次对项目结构进行了更新,token的存储机制基于redis,当然存储方式可以自由切换,Spring Security提供了SPI的多种实现。
客户端的信息还是基于jdbc实现,所以需要导入项目中提供的表oauth_client_details 。
推荐首先阅读专栏文章:认证鉴权与API权限控制在微服务架构中的设计与实现
单独的整合项目地址为:
GitHub:https://github.com/keets2012/microservice-integration
或者 码云:https://gitee.com/keets/microservice-integration
项目克隆之后:
mvn clean installXXXX的地方,替换成自己的实际地址(redis和mysql)mvn clean spring-boot:run进行请求获取Token授权:
笔者自己运行了结果如下:
…ps: 登录的用户名密码要在表单里面写,内容随意,因为在代码中已经去掉了对user服务的校验。
本次更新添加了对授权码模式的使用
授权码模式需要用户登录,所以借助浏览器
首先给数据库表中的oauth_client_details表中client_id为frontend的行authorized_grant_types添加authorization_code,web_server_redirect_uri设置为http://localhost:8080。表示该客户端允许授权码模式以及授权码回调地址为http://localhost:8080
浏览器访问地址
http://localhost:9000/oauth/authorize?response_type=code&client_id=frontend&
scope=all&redirect_uri=http://localhost:8080进入登录授权页面并同意授权,从回调地址中获取授权码
http://localhost:8080/?code=xGjrTm通过授权码获取access_token
method: post
url: http://localhost:9000/oauth/token?grant_type=authorization_code
header:
{
Authorization: Basic ZnJvbnRlbmQ6ZnJvbnRlbmQ=,
Content-Type: application/x-www-form-urlencoded
}
body:
{
code: xGjrTm,
redirect_uri: http://localhost:8080
}项目整合如果遇到问题,可以加入qq群(649932629)交流。
ps: qq交流群已设置付费,可以过滤很多广告,营造高质量的技术交流群。
有问题联系 aoho002#gmail.com
Copyright (c) 2018 aoho
No open issues yet, or sync has not completed.