#Spring security cannot access oauth login page using google.

1 messages · Page 1 of 1 (latest)

sharp thicket
#

I have a securityadapter class which defines what url a person can access and a main class

dusky baneBOT
#

<@&1004656351647117403> please have a look, thanks.

sharp thicket
#
package org.example.onlinebookstore;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;


@Configuration
public class SecurityAdapter {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        http.authorizeHttpRequests(a -> a
                        .requestMatchers( "/","/error").permitAll()
                        .requestMatchers("/user").authenticated()
                        .anyRequest().authenticated()
                )
                .exceptionHandling(e -> e
                        .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
                )
                .csrf(c -> c
                        .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) //random thing for angular
                )
                .logout(l -> l
                        .logoutSuccessUrl("/").permitAll()
                )
                .oauth2Login(o->o
                        .loginPage("/user")
                        .permitAll()
                ); // to permit login page
        return http.build();
    }
}
dusky baneBOT
sharp thicket
#

here is my main class

package org.example.onlinebookstore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@SpringBootApplication
@RestController
@RequestMapping("/")
public class OnlineBookstoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(OnlineBookstoreApplication.class, args);
    }

    @RequestMapping("/user")
    public String user(OAuth2AuthenticationToken oAuth2AuthenticationToken) {
        return oAuth2AuthenticationToken.getPrincipal().getAttribute("email");
    }
}
#

I have setup my yml file using docs

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: 
            client-secret: 
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
#

Expected: A login page through google
What I get: HTTP ERROR 401

#

I am accessing localhost:8080/user as I have mapped

#

For now I am printing out the users email but i dont get the login page

sharp thicket
#

Hello!

subtle granite
#

right ?

#

my 1st question: why your client and secret values are empty ?!

#

🤔

sharp thicket
#

I didn't post them here :D

subtle granite
sharp thicket
#

Sorry I didn't mention that.

subtle granite
sharp thicket
#

Yes. My first try was with GitHub auth which I took help from an old guide and changed deprecated methods.
This project I decided to go with Google and I used official spring docs and some YouTube tutorials.

#

The yt guides were old so had to swap them for newer methods :)

subtle granite
#

alright, so you now you get to login page right ?

#

and the problem is to navigate to another one

sharp thicket
#

I don't, I tried navigating to other pages (/) and had a callback error. I will try again in a while, in the meantime do you see any obvious issues here?I am still very new.

subtle granite
#

it's well updated

#

try it separately

#

to see how it works properly, then compare it to your own

sharp thicket
#

I did do it but I'll try again and let you know. Thanks peepo_heart

dusky baneBOT
#

@sharp thicket

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

sharp thicket
#

Hello! I tried the example shown in the docs but it still does not workie

#

I think my google codespace app settings are not setup properly because I can use github for oauth perfectly fine.

subtle granite
#

🤷‍♂️

sharp thicket
#

i see its a super and sub interface, is there an easier way i can visualize this through my ide instead of going through the docs?

#

I want to know what methods are available in OAuth2AuthenticatedPrincipal

#

what it impelements etc.

#

I know a way where I can make a class diagram but thats too typical and was wondering an easier way through my ide if its possible

subtle granite
#

first fix your original problem

#

make it work then go to something else

sharp thicket