#Spring security cannot access oauth login page using google.
1 messages · Page 1 of 1 (latest)
<@&1004656351647117403> please have a look, thanks.
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();
}
}
Detected code, here are some useful tools:
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
hey there!
Hello!
it seems you're trying to do oauth2 auth with google
right ?
my 1st question: why your client and secret values are empty ?!
🤔
I didn't post them here :D
And yes!
alright
Sorry I didn't mention that.
my 2nd question: did you follow some guide to do that impl ?
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 :)
alright, so you now you get to login page right ?
and the problem is to navigate to another one
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.
id highly suggest to follow this official sample
it's well updated
try it separately
to see how it works properly, then compare it to your own
I did do it but I'll try again and let you know. Thanks 
@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 👍
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.
In that case check your google setup
🤷♂️
i read up more about oauth from the official docs https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/oauth2/core/OAuth2AuthenticatedPrincipal.html
declaration: package: org.springframework.security.oauth2.core, interface: OAuth2AuthenticatedPrincipal
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
go step by step pls
first fix your original problem
make it work then go to something else
I gave up on google, i will use github for oauth and maybe in future change it. thanks for the help :D