feat: 增加鉴权

This commit is contained in:
wangyu 2023-04-13 13:42:04 +08:00
parent ee31f328c1
commit cc1fc2b360
6 changed files with 50 additions and 5 deletions

View File

@ -24,7 +24,7 @@
<dependency> <dependency>
<groupId>group.flyfish</groupId> <groupId>group.flyfish</groupId>
<artifactId>rest-proxy-core</artifactId> <artifactId>rest-proxy-core</artifactId>
<version>1.0.2</version> <version>1.1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -3,6 +3,8 @@ package group.flyfish.demo.service.other;
import group.flyfish.rest.annotation.RestApi; import group.flyfish.rest.annotation.RestApi;
import group.flyfish.rest.annotation.RestBody; import group.flyfish.rest.annotation.RestBody;
import group.flyfish.rest.annotation.RestService; import group.flyfish.rest.annotation.RestService;
import group.flyfish.rest.annotation.methods.GET;
import group.flyfish.rest.annotation.methods.POST;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
/** /**
@ -20,6 +22,6 @@ public interface OtherTestService {
* @param body 请求体 * @param body 请求体
* @return 结果 * @return 结果
*/ */
@RestApi(uri = "/mcp/pc/pcsearch") @POST("/mcp/pc/pcsearch")
Object postPcSearch(@RestBody Object body); Object postPcSearch(@RestBody Object body);
} }

View File

@ -1,8 +1,8 @@
package group.flyfish.demo.service.weather; package group.flyfish.demo.service.weather;
import group.flyfish.rest.annotation.RestApi;
import group.flyfish.rest.annotation.RestParams; import group.flyfish.rest.annotation.RestParams;
import group.flyfish.rest.annotation.RestService; import group.flyfish.rest.annotation.RestService;
import group.flyfish.rest.annotation.methods.GET;
import java.util.Map; import java.util.Map;
@ -20,7 +20,7 @@ public interface YikeWeatherService {
* @return 结果 * @return 结果
* @apiNote 使用@RestParams框架会自动注入所有键值对作为query * @apiNote 使用@RestParams框架会自动注入所有键值对作为query
*/ */
@RestApi("/day") @GET("/day")
Map<String, Object> getDay(@RestParams Map<String, Object> params); Map<String, Object> getDay(@RestParams Map<String, Object> params);
/** /**
@ -29,6 +29,6 @@ public interface YikeWeatherService {
* @return 结果 * @return 结果
* @apiNote 框架会自动将参数解包成query * @apiNote 框架会自动将参数解包成query
*/ */
@RestApi("/day") @GET("/day")
Map<String, Object> getDayStraight(String appid, String appsecret, String city); Map<String, Object> getDayStraight(String appid, String appsecret, String city);
} }

View File

@ -0,0 +1,28 @@
package group.flyfish.demo.service.yapi;
import group.flyfish.rest.core.auth.RestAuthProvider;
import group.flyfish.rest.core.client.RestClientBuilder;
/**
* yapi调用需要的鉴权配置
*
* @author wangyu
*/
public class YapiAuthProvider implements RestAuthProvider {
// yapi控制token
private static final String token = "e5172a42e62e0497b79e3c7df7b4ec1429399558f9d9d28c0152bd39ba4c217a";
/**
* 通过入侵client提供鉴权
* yapi是使用query鉴权的所以增加query即可
*
* @param builder rest客户端构建器
*/
@Override
public void provide(RestClientBuilder builder) {
// 支持添加认证头的方式在此处也可以调用其他rest服务获取接口
// builder.addHeader("Authorization", "token")
builder.addParam("token", token);
}
}

View File

@ -0,0 +1,14 @@
package group.flyfish.demo.service.yapi;
import group.flyfish.rest.annotation.RestService;
/**
* yapi服务支持鉴权
*
* @author wangyu
*/
@RestService(value = "yapi", authProvider = YapiAuthProvider.class)
public interface YapiService {
}

View File

@ -3,4 +3,5 @@ rest:
always-trust: true always-trust: true
urls: urls:
other: https://ug.baidu.com other: https://ug.baidu.com
yapi: http://yapi.flyfish.group/api
connection-timeout: 10s connection-timeout: 10s