我正在遵循一个复数教程,但是即使从源代码复制代码,它仍然会给出HttpClientErrorException $ Unauthorized:401错误。
充当服务器的应用程序位于application.properties中
server.port=9001
server.servlet.context-path=/services
security.oauth2.resource.user-info-uri=https://api.github.com/user
问题是,如果与PostMan一起运行并使用Header Authorization Bearer和令牌字符串,则它可以正常工作,但是从浏览器中它会出错。 请注意,@ Qualifiers是由我放置的,因为intellij告诉我,由于已经有2个bean且无法修复OAuth2ClientContext,所以无法连接。
我也曾试图评论安全性依赖关系,而仅留下OAuth依赖关系,但结果还是一样。
这是我的控制器类:
@Controller
@EnableOAuth2Sso
public class ReportController extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("oauth2ClientContext")
private OAuth2ClientContext clientContext;
@Autowired
private OAuth2RestTemplate oauth2RestTemplate;
@RequestMapping("/")
public String loadHome() {
return "home";
}
@RequestMapping("/reports")
public String loadReports(Model model) {
OAuth2AccessToken t = clientContext.getAccessToken();
System.out.println("Token: " + t.getValue());
ResponseEntity<ArrayList<TollUsage>> tolls = oauth2RestTemplate.exchange("http://localhost:9001/services/tolldata",
HttpMethod.GET, null, new ParameterizedTypeReference<ArrayList<TollUsage>>() {
});
model.addAttribute("tolls", tolls.getBody());
return "reports";
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/login**")
.permitAll()
.anyRequest().authenticated();
}
也是配置类:
@Configuration
public class ServiceConfig {
@Autowired
@Qualifier("oauth2ClientContext")
private OAuth2ClientContext clientContext;
@Bean
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ProtectedResourceDetails resource) {
return new OAuth2RestTemplate(resource, clientContext);
}
}
还有pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>