0
votes

I am setting up a spring security using OAuth2.0 using spring boot application. The authentication page is coming with already logged in google user but after selecting one it is again loading the same authentication page.

HomeController.java

import java.security.Principal;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class HomeController {


    @RequestMapping("/")
    public ModelAndView home() {
        System.out.println("inside home");
        ModelAndView model = new ModelAndView("home");
        return model;
    }

    @RequestMapping("/login")
    public ModelAndView loginPage() {
        System.out.println("inside login");
        ModelAndView model = new ModelAndView("login");
        return model;
    }

    @RequestMapping("user")
    @ResponseBody
    public Principal user(Principal principal) {
        System.out.println("inside userrrrrrrrr");
        return principal;
    }
}

AppSecurityConfig.java

@Configuration
@EnableWebSecurity  
@EnableOAuth2Sso
public class AppSecurityConfig extends WebSecurityConfigurerAdapter{

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {

       // System.out.println(httpSecurity.);
        try {
            System.out.println("inside configure");
            httpSecurity
            .csrf().disable()
            .authorizeRequests().antMatchers("/login").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().disable();
        }catch(Exception e) {
            System.err.println("exception caught:"+e);
        }   

    }
}

This is my application.properties file for the OAuth configuration : I am changing client-id and client-secret code.

security.oauth2.client.client-id = 434559791042-642qk4agcs32g1rajsssss62ilrd86s4.apps.googleusercontent.com
security.oauth2.client.client-secret = 1l4jqw7lBailpuVgWobvcxwo 
security.oauth2.client.access-token-uri = https://www.googleapis.com/oauth2/v3/token
security.oauth2.client.user-authorization-uri = https://accounts.google.com/o/oauth2/auth
security.oauth2.client.tokenName = oauth_token
security.oauth2.client.authenticationScheme = query
security.oauth2.client.clientAuthenticationScheme = form
security.oauth2.client.scope = profile email

security.oauth2.resource.user-info-uri = https://www.googleapis.com/userinfo/v2/me
security.oauth2.resource.preferTokenInfo = false

in my google api app i have given below redirects .

Authorised JavaScript origins : http://localhost:8080 Authorised redirect URIs: http://localhost:8080/login

The google authentication frame is coming with my already logged in account but when i am selecting it, again it is showing the same authentication frame. It should be redirecting to my native app after authentication.

1

1 Answers

0
votes

Check the redirect_uri Spring is sending to Google. You can do this in your browsers by right clicking and selecting inspect, then select the network tab. So when you select sign in to google in your webapp, you should see spring send a redirect response to your browser, it will be to Google, and it will include a parameter redirect_uri, this is were Google will send the redirect to. Make sure it is correct. And make sure it matches the URI you provided Google in the API page, as Google will block any it doesn't know about.