Server๐Ÿงค/SpringSecurity

[SpringSecurity] SecurityConfig ์•Œ์•„๋ณด๊ธฐ

yujindonut 2023. 7. 17. 12:04
728x90

 

WebSecurityConfigurerAdapter

๊ฐœ๋ฐœ์ž๊ฐ€ SpringSecurity ์„ค์ •์„ ๋” ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•˜๋„๋ก ๊ตฌํ˜„๋˜์–ด์žˆ๋‹ค.

์œ„๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉด class์— @EnableWebSecurity ์„ค์ •ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

URL Matchers ๊ด€๋ จ ๊ธฐ๋Šฅ

  • antMatchers()
    • antMatchers("/signup").permitAll()
    • "/signup" ์š”์ฒญ์„ ๋ชจ๋‘์—๊ฒŒ ํ—ˆ์šฉ
  • mvcMatchers()
    • mvcMatchers("/signup").permitAll()
    • "/signup", "/signup/", "/signup.html" ์™€ ๊ฐ™์€ ์œ ์‚ฌ signup ์š”์ฒญ์„ ๋ชจ๋‘์—๊ฒŒ ํ—ˆ์šฉํ•œ๋‹ค.
  • regexMatchers()
    • ์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ ๋งค์นญํ•œ๋‹ค
  • requestMatchers
    • ์œ„์˜ ์„ธ๊ฐœ ๋ชจ๋‘ requestMatchers๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
    • ๋ช…ํ™•ํ•˜๊ฒŒ ์š”์ฒญ ๋Œ€์ƒ์„ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ์— requestMatchers๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

์ธ๊ฐ€ ๊ด€๋ จ ์„ค์ •

  • http.authorizeRequests()
    • ์ธ๊ฐ€๋ฅผ ์„ค์ •
  • permitAll()
    • http.authorizeRequests().antMatchers("/signup").permitAll()
    • "/signup" ์š”์ฒญ์„ ๋ชจ๋‘์—๊ฒŒ ํ—ˆ์šฉํ•œ๋‹ค.
  • hasRole()
    • http.authorizeRequests().antMatchers(HttpMethod.POST, "/notice").hasRole("ADMIN")
    • ๊ถŒํ•œ์„ ๊ฒ€์ฆ
  • authenticated()
    • ์ธ์ฆ์ด ๋˜์—ˆ๋Š”์ง€๋ฅผ ๊ฒ€์ฆํ•œ๋‹ค.
  • ignoring()
    • ํŠน์ • ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•ด์„œ SpringSecurity ์ž์ฒด๋ฅผ ์ ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„๋•Œ
    • ignoring์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋Š” permitAll๊ณผ ๋‹ค๋ฅด๊ฒŒ SpringSecurity์˜ ๋Œ€์ƒ์— ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.
    • ์–ด๋–ค ํ•„ํ„ฐ๋„ ์‹คํ–‰๋˜์ง€ ์•Š์•„ ์„ฑ๋Šฅ์ ์œผ๋กœ ์šฐ์ˆ˜ํ•จ
@Override
public void configure(WebSecurity web) {
	// ์ •์  ๋ฆฌ์†Œ์Šค spring security ๋Œ€์ƒ์—์„œ ์ œ์™ธ
	web.ignoring().antMatchers("/images/**", "/css/**"); // ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์€ ์ฝ”๋“œ
	web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}

 


์˜ˆ์ œ

/**
 * Security ์„ค์ • Config
 */
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    private final UserService userService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // basic authentication
        http.httpBasic().disable(); // basic authentication filter ๋น„ํ™œ์„ฑํ™”
        // csrf
        http.csrf();
        // remember-me
        http.rememberMe();
        // authorization
        http.authorizeRequests()
                // /์™€ /home์€ ๋ชจ๋‘์—๊ฒŒ ํ—ˆ์šฉ
                .antMatchers("/", "/home", "/signup").permitAll()
                // hello ํŽ˜์ด์ง€๋Š” USER ๋กค์„ ๊ฐ€์ง„ ์œ ์ €์—๊ฒŒ๋งŒ ํ—ˆ์šฉ
                .antMatchers("/note").hasRole("USER")
                .antMatchers("/admin").hasRole("ADMIN")
                .antMatchers(HttpMethod.POST, "/notice").hasRole("ADMIN")
                .antMatchers(HttpMethod.DELETE, "/notice").hasRole("ADMIN")
                .anyRequest().authenticated();
        // login
        http.formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/")
                .permitAll(); // ๋ชจ๋‘ ํ—ˆ์šฉ
        // logout
        http.logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/");
    }

    @Override
    public void configure(WebSecurity web) {
        // ์ •์  ๋ฆฌ์†Œ์Šค spring security ๋Œ€์ƒ์—์„œ ์ œ์™ธ
//        web.ignoring().antMatchers("/images/**", "/css/**"); // ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์€ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
        web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
    }

    /**
     * UserDetailsService ๊ตฌํ˜„
     *
     * @return UserDetailsService
     */
    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        return username -> {
            User user = userService.findByUsername(username);
            if (user == null) {
                throw new UsernameNotFoundException(username);
            }
            return user;
        };
    }
}
728x90