SpringSecurityでユーザー認証機能を簡単につくる (original) (raw)

72

Go to list of users who liked

80

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SpringBootでWebアプリケーションを作る際、
SpringSecurityを使ってあれこれカスタマイズしながらユーザー認証の仕組みを作ったのでまとめます

SpringConfigクラスを作る

SpringConfig.javaの例


@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    // ポイント1
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**", "/image/**", "/js/**");
    }

    // ポイント2
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/signin").permitAll()
                                .antMatchers("/admin/**").hasRole("ADMIN")
                                .anyRequest().authenticated();
        http.formLogin().loginProcessingUrl("/login").loginPage("/signin")
                        .failureUrl("?error").defaultSuccessUrl("/", false)
                        .usernameParameter("loginId").passwordParameter("password")
                        .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("signout"))
                        .logoutSuccessUrl("/signin")
                        .deleteCookies("JSESSIONID")
                        .invalidateHttpSession(true).permitAll();
        http.sessionManagement().invalidSessionUrl("/signin");
    }

    // ポイント3
    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery(
                        "select mail_address as username, password, enabled from accounts where mail_address = ?")
                .authoritiesByUsernameQuery(
                        "select mail_address, role from accounts where mail_address = ?")
                .passwordEncoder(new ShaPasswordEncoder(256));
    }
}

ポイント1 の部分

ポイント2の部分

http.authorizeRequests().antMatchers("/signin").permitAll()
                        .antMatchers("/admin/**").hasRole("ADMIN")
                        .anyRequest().authenticated(); 
http.formLogin().loginProcessingUrl("/login").loginPage("/signin")
                .failureUrl("?error").defaultSuccessUrl("/", false)
                .usernameParameter("loginId").passwordParameter("password")
                .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("signout"))
                .logoutSuccessUrl("/signin")
                .deleteCookies("JSESSIONID")
                .invalidateHttpSession(true).permitAll();

ポイント3の部分

    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery(
                        "select mail_address as username, password, enabled from accounts where mail_address = ?")
                .authoritiesByUsernameQuery(
                        "select mail_address, role from accounts where mail_address = ?")
                .passwordEncoder(new ShaPasswordEncoder(256));
    }

まとめ

72

Go to list of users who liked

80

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

72

Go to list of users who liked

80

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?