| 认证服务器和资源服务器是服务端,可以看成是微信; |
| 第三方客户端则是客户端,可以看成第三方的网站 |
| 用户在认证服务器认证成功后,再去资源服务器获取用户的相关资源 |
| |
| 资源服务器是提供给第三方客户端使用的 |
| 如果没有第三方客户端,所有子模块都是自己内部的子系统,则使用单点登录即可 |
- 新建一个demo06,在demo05的基础上开发,确保能够跑通
- 由于使用jdbc管理第三方应用,所以查看数据库

- 任意使用一种模式获取token

- 检查该令牌是否有效,查看拥有的权限

- 测试通过
资源服务器的使用
- 新建子模块resource
- 编写pom.xml
| <dependencies> |
| <dependency> |
| <artifactId>base</artifactId> |
| <groupId>com.ychen.oauth2</groupId> |
| <version>1.0-SNAPSHOT</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-web</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.cloud</groupId> |
| <artifactId>spring-cloud-starter-oauth2</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-test</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-devtools</artifactId> |
| </dependency> |
| |
| </dependencies> |
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-maven-plugin</artifactId> |
| </plugin> |
| </plugins> |
| </build> |
| @SpringBootApplication |
| public class ProductResourceApplication { |
| public static void main(String[] args) { |
| SpringApplication.run(ProductResourceApplication.class, args); |
| } |
| } |
| @RestController |
| @RequestMapping("/product") |
| public class ProductController { |
| @GetMapping("/list") |
| @PreAuthorize("hasAuthority('product')") |
| public MengxueguResult list() { |
| List<String> list = new ArrayList<>(); |
| list.add("眼镜"); |
| list.add("格子衬衣"); |
| list.add("双肩包"); |
| return MengxueguResult.ok(list); |
| } |
| } |
| @PreAuthorize("hasAuthority('product')") |
| |

| @Configuration |
| @EnableResourceServer |
| @EnableGlobalMethodSecurity(prePostEnabled = true) |
| public class ResourceServerConfig extends ResourceServerConfigurerAdapter { |
| public static final String RESOURCE_ID = "product-server"; |
| @Override |
| public void configure(ResourceServerSecurityConfigurer resources) throws Exception { |
| |
| resources.resourceId(RESOURCE_ID) |
| .tokenServices(tokenService()) |
| ; |
| } |
| public ResourceServerTokenServices tokenService(){ |
| |
| RemoteTokenServices service = new RemoteTokenServices(); |
| |
| service.setCheckTokenEndpointUrl("http://localhost:8090/auth/oauth/check_token"); |
| service.setClientId("mengxuegu-pc"); |
| service.setClientSecret("mengxuegu-secret"); |
| return service; |
| } |
| |
| } |
| RESOURCE_ID = "product-server" |
| |
| |

- 启动认证服务器模块和资源服务器模块进行测试
- 浏览器访问如下,获取授权码
| http://localhost:8090/auth/oauth/authorize?client_id=mengxuegu-pc&response_type=code |
-
输入用户名和密码,并同意授权,之后得到一个授权码


-
通过授权码模式得到一个token

-
检查令牌是否有效,有效则可以查看拥有的权限

-
拿到令牌去资源服务器获取资源,测试通过

| # 当用户在认证服务器中使用授权码模式,获取授权码的时候,会指定第三方的客户端可以访问服务端的哪些资源 |
| # 而资源服务器的权限配置,则是用于限制服务端的不同资源需要不同的权限 |
| |
| # 第1层权限:RESOURCE_ID = "product-server";表示访问当前资源服务器需要的权限 |
| # 第2层权限:scope=all,表示访问该资源服务器中的某些资源所需的权限 |
| # 第3层权限:.antMatchers("/product/*").hasAuthority("product");表示具体某个接口所需的权限 |
| # ResourceServerConfig配置类下添加如下 |
| @Override |
| public void configure(HttpSecurity http) throws Exception { |
| http.sessionManagement() |
| |
| .sessionCreationPolicy(SessionCreationPolicy.STATELESS) |
| .and() |
| .authorizeRequests() |
| |
| .antMatchers("/product/*").hasAuthority("product") |
| |
| .antMatchers("/**").access("#oauth2.hasScope('all')") |
| |
| |
| ; |
| } |
| .antMatchers("/product/*").hasAuthority("product") |
| |
| |
| .antMatchers("/**").access("#oauth2.hasScope('all')") |
| |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术