登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
登录
注册
代码拉取完成,页面将自动刷新
开源项目
>
其他开源
>
图书/手册/教程
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
65
Star
233
Fork
171
LengLeng
/
pig-cloud
代码
Issues
6
Pull Requests
0
Wiki
统计
流水线
服务
JavaDoc
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
【springcloud】oauth2.0 实现spring cloud nosession
待办的
#IERPT
lengleng
拥有者
创建于
2017-08-26 21:31
 上一篇博客使用[自定义jwt实现spring cloud nosession](https://my.oschina.net/giegie/blog/1518957),过程稍微比较复杂,依赖的是我们自己控制token生成、校验。 那么这一篇文章将使用**spring cloud 和 spring-security-oauth2** 做一个无缝集成,实现nosession,和权限控制。 为了快速的实现目标效果,拓扑结构如上图,我们将采用 InMemory的形式实现,相关换成JDBC实现位置,我会在文章中说明,主要看代码的注释。 ## auth-server 实现 认证实现 ``` @Configuration @EnableAuthorizationServer class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private UserDetailsService userDetailsService; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore()).userDetailsService(userDetailsService); } @Override public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.tokenKeyAccess("permitAll()"); } //可以改成JDBC从库里读或者其他方式。 @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("browser") .authorizedGrantTypes("authorization_code", "refresh_token", "password") .scopes("ui") .and() .withClient("resource-server") .secret("root") .authorizedGrantTypes("client_credentials", "refresh_token") .scopes("server"); } //这里配置的配置可以改成 jdbc redis 等其他方式 @Bean public InMemoryTokenStore tokenStore() { return new InMemoryTokenStore(); } } // security配置主要是userDetailsService @Configuration @EnableWebSecurity class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception { http.anonymous().disable() .authorizeRequests() .anyRequest().authenticated(); } } ```` ## resource-server 关于为什么要配置loadBalancerInterceptor这个bean 我会在这篇文章总结部分说明 ``` @SpringBootApplication @EnableEurekaClient @EnableResourceServer public class ResourceApplication { public static void main(String[] args) { SpringApplication.run(ResourceApplication.class, args); } @Bean LoadBalancerInterceptor loadBalancerInterceptor(LoadBalancerClient loadBalance) { return new LoadBalancerInterceptor(loadBalance); } } ``` 还有一个最重要的,就是我们受保护的资源。。 ``` @RestController public class ResourceController { @GetMapping("/getResource") public String getResource(Principal principal) { return "SUCCESS,授权成功拿到资源啦.当前用户:" + principal.getName(); } } ``` 配置application.yml ``` security: sessions: stateless oauth2: resource: loadBalanced: true user-info-uri: http://gateway-server/uaa/user prefer-token-info: false service-id: resource-server ``` ## gateway 配置 ``` zuul: ignored-services: '*' routes: auth-server: path: /uaa/** serviceId: auth-server sensitiveHeaders: strip-prefix: false resource-server: path: /demo/** serviceId: resource-server sensitiveHeaders: strip-prefix: false ``` ## 运行使用 服务 | 端口 ---|--- gateway-server | 6666 auth-server | 7777 resource-server | 8888 eureka-server | http://eureka.didispace.com/eureka/ ### 通过网关访问auth-server 获取access-token ``` //curl 不会用的可以参考文章总结里面 Rest Client 那个报文工具。 // 这里的账户是在 auth-server里面写死的,service实现里面。 curl -H "Authorization:Basic YnJvd3Nlcjo=" -d "grant_type=password&scope=ui&username=123&password=456" localhost:6666/uaa/oauth/token {"access_token":"c8df8942-f032-4403-92fc-f23019819da9","token_type":"bearer","refresh_token":"993a94b4-5335-4f0a-9981-e1ad4e4776a8","expires_in":43199,"scope":"ui"} ``` ### 通过access-token 访问受保护的资源 ``` curl -H "Authorization:Bearer c8df8942-f032-4403-92fc-f23019819da9" localhost:6666/demo/getResource SUCCESS,授权成功拿到资源啦.当前用户:123 ``` ## 片尾总结 1. 坑。自定义对象的时候,默认使用的加密是PlaintextPasswordEncoder。如果使用其他,记得用对应的工具类处理一下密码再插入。 2. 关于oauth server 认证的loadBalanced 设置。 ``` security: sessions: stateless oauth2: resource: loadBalanced: true #设置这个才可以使用eureka的服务名,配合loadBalancerInterceptor bean。 ``` 3.关于CURL 工具 可以用 Git Bash ,提供了curl 工具的。或者chrome里面这个插件挺好用。 
 上一篇博客使用[自定义jwt实现spring cloud nosession](https://my.oschina.net/giegie/blog/1518957),过程稍微比较复杂,依赖的是我们自己控制token生成、校验。 那么这一篇文章将使用**spring cloud 和 spring-security-oauth2** 做一个无缝集成,实现nosession,和权限控制。 为了快速的实现目标效果,拓扑结构如上图,我们将采用 InMemory的形式实现,相关换成JDBC实现位置,我会在文章中说明,主要看代码的注释。 ## auth-server 实现 认证实现 ``` @Configuration @EnableAuthorizationServer class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private UserDetailsService userDetailsService; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore()).userDetailsService(userDetailsService); } @Override public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.tokenKeyAccess("permitAll()"); } //可以改成JDBC从库里读或者其他方式。 @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("browser") .authorizedGrantTypes("authorization_code", "refresh_token", "password") .scopes("ui") .and() .withClient("resource-server") .secret("root") .authorizedGrantTypes("client_credentials", "refresh_token") .scopes("server"); } //这里配置的配置可以改成 jdbc redis 等其他方式 @Bean public InMemoryTokenStore tokenStore() { return new InMemoryTokenStore(); } } // security配置主要是userDetailsService @Configuration @EnableWebSecurity class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception { http.anonymous().disable() .authorizeRequests() .anyRequest().authenticated(); } } ```` ## resource-server 关于为什么要配置loadBalancerInterceptor这个bean 我会在这篇文章总结部分说明 ``` @SpringBootApplication @EnableEurekaClient @EnableResourceServer public class ResourceApplication { public static void main(String[] args) { SpringApplication.run(ResourceApplication.class, args); } @Bean LoadBalancerInterceptor loadBalancerInterceptor(LoadBalancerClient loadBalance) { return new LoadBalancerInterceptor(loadBalance); } } ``` 还有一个最重要的,就是我们受保护的资源。。 ``` @RestController public class ResourceController { @GetMapping("/getResource") public String getResource(Principal principal) { return "SUCCESS,授权成功拿到资源啦.当前用户:" + principal.getName(); } } ``` 配置application.yml ``` security: sessions: stateless oauth2: resource: loadBalanced: true user-info-uri: http://gateway-server/uaa/user prefer-token-info: false service-id: resource-server ``` ## gateway 配置 ``` zuul: ignored-services: '*' routes: auth-server: path: /uaa/** serviceId: auth-server sensitiveHeaders: strip-prefix: false resource-server: path: /demo/** serviceId: resource-server sensitiveHeaders: strip-prefix: false ``` ## 运行使用 服务 | 端口 ---|--- gateway-server | 6666 auth-server | 7777 resource-server | 8888 eureka-server | http://eureka.didispace.com/eureka/ ### 通过网关访问auth-server 获取access-token ``` //curl 不会用的可以参考文章总结里面 Rest Client 那个报文工具。 // 这里的账户是在 auth-server里面写死的,service实现里面。 curl -H "Authorization:Basic YnJvd3Nlcjo=" -d "grant_type=password&scope=ui&username=123&password=456" localhost:6666/uaa/oauth/token {"access_token":"c8df8942-f032-4403-92fc-f23019819da9","token_type":"bearer","refresh_token":"993a94b4-5335-4f0a-9981-e1ad4e4776a8","expires_in":43199,"scope":"ui"} ``` ### 通过access-token 访问受保护的资源 ``` curl -H "Authorization:Bearer c8df8942-f032-4403-92fc-f23019819da9" localhost:6666/demo/getResource SUCCESS,授权成功拿到资源啦.当前用户:123 ``` ## 片尾总结 1. 坑。自定义对象的时候,默认使用的加密是PlaintextPasswordEncoder。如果使用其他,记得用对应的工具类处理一下密码再插入。 2. 关于oauth server 认证的loadBalanced 设置。 ``` security: sessions: stateless oauth2: resource: loadBalanced: true #设置这个才可以使用eureka的服务名,配合loadBalancerInterceptor bean。 ``` 3.关于CURL 工具 可以用 Git Bash ,提供了curl 工具的。或者chrome里面这个插件挺好用。 
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
未关联
master
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
Java
1
https://gitee.com/boding1/pig-cloud.git
git@gitee.com:boding1/pig-cloud.git
boding1
pig-cloud
pig-cloud
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册