Почему мой OAuth2 не работает с Spring Boot? - PullRequest
0 голосов
/ 17 марта 2020

Я пытаюсь настроить вход в Facebook с OAuth2 для Spring Boot.

Сначала у меня есть настройки безопасности Spring. Я хочу, чтобы каждая страница из www.localhost: 8080 / Intranet / ** блокировалась для людей, которые не были авторизованы Facebook.

@Configuration
@EnableOAuth2Client
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
                .antMatcher("/Intranet/**")
                .authorizeRequests()
                .antMatchers("/", "/Intranet")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .logout().logoutSuccessUrl("/").permitAll();
    }

}

Я создаю application.yml здесь:

  spring:
   application:
    name: spektrakonhemsida
  security:
    oauth2:
      client:
        registration:
          facebook:
            clientId: myID
            clientSecret: mySecret
            accessTokenUri: https://graph.facebook.com/oauth/access_token
            userAuthorizationUri: https://www.facebook.com/dialog/oauth
            tokenName: oauth_token
            authenticationScheme: query
            clientAuthenticationScheme: form
            resource:
              userInfoUri: https://graph.facebook.com/me
# Server configuration
server:
  port: 8080
  error:
    whitelabel:
       enabled: false

Тогда у меня есть свои зависимости для Spring Security и OAuth2:

        <dependency>


<groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-oauth2-client</artifactId>
    </dependency>

    <!-- Prevent /error to crash -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

Вот что происходит:

  1. Когда я получаю доступ к www.localhost: 8080 / Intr anet <- Works perfekt! </li>
  2. Когда я получу доступ к www.localhost: 8080 / Intranet / Bokning <- я попаду на / error, где мой текст показывает: «У вас нет прав здесь! Пожалуйста, войдите». </li>

Но я хочу, чтобы пользователи автоматически переходили на страницу входа в Facebook при входе в / Intranet / **

Почему этого не происходит?

1 Ответ

0 голосов
/ 19 марта 2020

Нашел решение сейчас. Это необходимо сделать, чтобы он работал с Facebook.

Безопасность:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {

        http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/Intranet/Bokning").authenticated() // Block this 
        .antMatchers("/**", "/Intranet**").permitAll() // Allow this for all
        .anyRequest().authenticated()
        .and().logout().logoutSuccessUrl("/").permitAll()
        .and()
        .oauth2Login();
    }
}

И appllication.yml

spring:
  security:
    oauth2:
      client:
        registration:
           facebook:
              clientId: myID
              clientSecret: mySecret
              accessTokenUri: https://graph.facebook.com/oauth/access_token
              userAuthorizationUri: https://www.facebook.com/dialog/oauth
              tokenName: oauth_token
              authenticationScheme: query
              clientAuthenticationScheme: form
              resource:
                 userInfoUri: https://graph.facebook.com/me

server:
  port: 8080

И pom. xml file:

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-oauth2-client</artifactId>
    </dependency>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...