RestTemplate实现远程调用

This commit is contained in:
YoVinchen 2023-08-21 20:22:50 +08:00
parent 4d461f43bb
commit 31acc77362
12 changed files with 61 additions and 6 deletions

View File

@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="BorrowApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="OAuth2" nameIsGenerated="true">
<module name="borrow-service" />
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.BorrowApplication" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.test.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

17
.run/Nacos.run.xml Normal file
View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Nacos" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="$PROJECT_DIR$/nacos/bin/startup.sh" />
<option name="SCRIPT_OPTIONS" value="-m standalone" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$/nacos" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="/bin/bash" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="true" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="UserApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="OAuth2" nameIsGenerated="true">
<module name="user-service" />
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.UserApplication" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.test.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View File

@ -5,3 +5,6 @@ server:
#这里修改一下context-path这样保存的Cookie会使用指定的路径就不会和其他服务打架了 #这里修改一下context-path这样保存的Cookie会使用指定的路径就不会和其他服务打架了
#但是注意之后的请求都得在最前面加上这个路径 #但是注意之后的请求都得在最前面加上这个路径
context-path: /sso context-path: /sso
spring:
application:
name: authservice

View File

@ -2,7 +2,6 @@ package com.test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
/** /**

View File

@ -5,11 +5,11 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
/** /**
* ClassName: con * ClassName: cong
* Package: com.test.config * Package: com.test.config
* *
* @author yovinchen * @author yovinchen
* @Create 2023/8/21 10:23 * @Create 2023/8/21 20:10
*/ */
@Configuration @Configuration
public class ResourceConfiguration extends ResourceServerConfigurerAdapter { //继承此类进行高度自定义 public class ResourceConfiguration extends ResourceServerConfigurerAdapter { //继承此类进行高度自定义
@ -18,7 +18,7 @@ public class ResourceConfiguration extends ResourceServerConfigurerAdapter { //
public void configure(HttpSecurity http) throws Exception { //这里也有HttpSecurity对象方便我们配置SpringSecurity public void configure(HttpSecurity http) throws Exception { //这里也有HttpSecurity对象方便我们配置SpringSecurity
http http
.authorizeRequests() .authorizeRequests()
.anyRequest().access("#oauth2.hasScope('lbwnb')"); //添加自定义规则 .anyRequest().access("#oauth2.hasScope('book')"); //添加自定义规则
//Token必须要有我们自定义scope授权才可以访问此资源 //Token必须要有我们自定义scope授权才可以访问此资源
} }
} }

View File

@ -1,11 +1,11 @@
package com.test.service.impl; package com.test.service.impl;
import com.test.entity.Book;
import com.test.mapper.BookMapper; import com.test.mapper.BookMapper;
import com.test.service.BookService; import com.test.service.BookService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import com.test.entity.Book;
/** /**
* ClassName: BookServiceImpl * ClassName: BookServiceImpl

View File

@ -6,6 +6,8 @@ spring:
url: jdbc:mysql://43.143.164.194:3306/mac url: jdbc:mysql://43.143.164.194:3306/mac
username: mac username: mac
password: mactest password: mactest
application:
name: bookservice
security: security:
oauth2: oauth2:
client: client:

View File

@ -1,5 +1,6 @@
package com.test.config; package com.test.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2ClientContext;

View File

@ -6,6 +6,8 @@ spring:
url: jdbc:mysql://43.143.164.194:3306/mac url: jdbc:mysql://43.143.164.194:3306/mac
username: mac username: mac
password: mactest password: mactest
application:
name: borrowservice
security: security:
oauth2: oauth2:
client: client:

View File

@ -2,7 +2,6 @@ package com.test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
/** /**

View File

@ -6,6 +6,8 @@ spring:
url: jdbc:mysql://43.143.164.194:3306/mac url: jdbc:mysql://43.143.164.194:3306/mac
username: mac username: mac
password: mactest password: mactest
application:
name: userservice
security: security:
oauth2: oauth2:
client: client: