3

I am following a pluralsight tutorial, but even copying the code from the source it still gives HttpClientErrorException$Unauthorized: 401 error.

The application that acts as server has in application.properties

server.port=9001
server.servlet.context-path=/services
security.oauth2.resource.user-info-uri=https://api.github.com/user

The thing is that, if am running with PostMan and use Header Authorization Bearer and the token string it is working, but from the browser it gives error. Note that the @Qualifiers are placed by me because intellij told me that OAuth2ClientContext cannot be wired because there are already 2 beans and that fixed the error.

I have tried also to comment the security dependency ans leave just the OAuth dependency, but the same result appeared.

Here is my Controller Class:

@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();
}

Also the configuration class:

@Configuration
public class ServiceConfig {

@Autowired
@Qualifier("oauth2ClientContext")
private OAuth2ClientContext clientContext;

@Bean
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ProtectedResourceDetails resource) {
    return new OAuth2RestTemplate(resource, clientContext);
}

}

And the 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>

0 Answers0