8
7

More than 3 years have passed since last update.

Spring Security設定の解説メモ(OAuth 2)

Last updated at Posted at 2020-08-17

SecurityConfigパッケージ作成

自分は以下のパッケージに作成しました。
image.png

SecurityConfigコード


package jojoidu.boot.springboot.config.auth;

import jojoidu.boot.springboot.domain.user.Role;
import lombok.RequiredArgsConstructor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final CustomOAuth2UserService customOAuth2UserService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .headers().frameOptions().disable()
                .and()
                    .authorizeRequests()
                    .antMatchers("/", "/css/**", "/images/**", "/js/**", "/h2-console/**", "/profile").permitAll()
                    .antMatchers("/api/v1/**").hasRole(Role.USER.name())
                    .anyRequest().authenticated()
                .and()
                    .logout()
                        .logoutSuccessUrl("/")
                .and()
                    .oauth2Login()
                        .userInfoEndpoint()
                            .userService(customOAuth2UserService);
    }
}

@EnableWebSecurity

  • Spring Security設定を活性化するため使います。

csrf().disable().headers().frameOptions().disable()

  • h2-console画面を使うために該当オプションをdisableします。 (プライベートプロジェクトH2データベースを使っているため。)

authorizeRequests()

  • URLごとの権限管理の設定オプションは「authorizeRequests()」から始まります。
  • authorizeRequestsを宣言してからantMatchers()を使うことができます。

antMatchers()

  • 権限管理対象を指定するオプションです。
  • URL、HTTPメソッドごとに管理が可能です。
  • "/"など指定されたURLは「permitAll()」オプションを使って全体観覧権限を与えました。
  • "/api/v1/**"のアドレスを持っているAPIはUSER権限を持っているユーザーのみ呼び出しができるように設定しました。

anyRequest()

  • 設定された値以外のURLを示します。
  • 今回は「authenticated()」を追加して残りURLに対してはすべて認証ユーザーのみ許可します。
  • 認証ユーザーはつまりログインされているユーザーのことです。

logout().logoutSuccessUrl("/")

  • ログアウト機能に関して様々な設定をします。
  • ログアウト成功時、「/」(TOP)に遷移させます。

oauth2Login()

  • Oauth 2ログイン機能に関連設定を行います。(始まり)

userInfoEndpoint()

  • Oauth 2ログイン成功後、ユーザー情報を持ってくるときの設定を行います。

userService(customOAuth2UserService)

  • ソーシャルログイン成功後、実行するUserService Interfaceを継承しているクラスを登録します。

@RequiredArgsConstructor
@Service
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
}
  • リソースサーバー(ソーシャルサービス)からユーザー情報を持ってきた状態で追加処理の機能を記述できます。(UserService Interfaceを継承しているクラス内で)

終わり

今回はOAuth2のSpringboot2のsecurity設定を行いましたが、
整理しながらもう一度勉強になりました。
内容はメモって感じなので、詳しい情報などはDOCや他のブログを参考にしたほうが良いかと思います。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7