JWT 存储 token 通过 Nacos OpenFeign 实现远程调用
This commit is contained in:
@@ -35,12 +35,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||
<version>2.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
@@ -2,7 +2,6 @@ package com.test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
|
||||
@@ -12,9 +11,13 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.E
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/8/14 16:25
|
||||
* <p>
|
||||
* 如果先使用 @EnableResourceServer 注解,再使用 @EnableFeignClients 注解,则 Feign 客户端可能无法正常工作。
|
||||
* 因为在启用资源服务器之后,Spring Security 会拦截所有请求并进行身份验证和授权处理。
|
||||
* 而 Feign 客户端发送请求时也需要经过 Spring Security 的处理流程,如果没有正确配置权限规则,则可能导致访问被拒绝或者出现其他错误。
|
||||
*/
|
||||
@EnableResourceServer
|
||||
@EnableFeignClients
|
||||
@EnableResourceServer
|
||||
@SpringBootApplication
|
||||
public class BorrowApplication {
|
||||
public static void main(String[] args) {
|
||||
|
@@ -12,7 +12,6 @@ import com.test.service.client.BookClient;
|
||||
import com.test.service.client.UserClient;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
@@ -37,15 +36,20 @@ public class BorrowServiceImpl implements BorrowService {
|
||||
@Resource
|
||||
UserClient userClient;
|
||||
|
||||
// @Resource
|
||||
// OAuth2RestTemplate template;
|
||||
|
||||
@Override
|
||||
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
|
||||
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
|
||||
//这里通过调用getForObject来请求其他服务,并将结果自动进行封装
|
||||
//获取User信息
|
||||
// User user = template.getForObject("http://userservice/user/" + uid, User.class);
|
||||
User user = userClient.getUserById(uid);
|
||||
//获取每一本书的详细信息
|
||||
List<Book> bookList = borrow.stream()
|
||||
.map(b -> bookClient.getBookById(b.getBid()))
|
||||
List<Book> bookList = borrow.stream().map(b ->
|
||||
// template.getForObject("http://bookservice/book/" + b.getBid(), Book.class))
|
||||
bookClient.getBookById(b.getBid()))
|
||||
.collect(Collectors.toList());
|
||||
return new UserBorrowDetail(user, bookList);
|
||||
}
|
||||
|
@@ -10,6 +10,13 @@ spring:
|
||||
name: borrowservice
|
||||
security:
|
||||
oauth2:
|
||||
client:
|
||||
client-id: web
|
||||
client-secret: 654321
|
||||
#Token获取地址
|
||||
access-token-uri: http://localhost:8500/sso/oauth/token
|
||||
#验证页面地址
|
||||
user-authorization-uri: http://localhost:8500/sso/oauth/authorize
|
||||
resource:
|
||||
jwt:
|
||||
key-value: lbwnb #注意这里要跟验证服务器的密钥一致,这样算出来的签名才会一致
|
||||
@@ -19,3 +26,5 @@ feign:
|
||||
enabled: true
|
||||
#同时开启负载均衡支持
|
||||
load-balanced: true
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user