스프링부트 시큐리티

(。◠ ◠。)·2022년 9월 7일

국비 개발 훈련 일기

목록 보기
32/35

version '2.6.11'

그레이들

	implementation 'javax.servlet:jstl'
	implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
	implementation 'org.springframework.boot:spring-boot-devtools'

뷰단

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
		
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		//http	
		//.permitAll()모두
		//.hasRole이하 이용자에게만
		http.authorizeRequests()
			.antMatchers("/").permitAll()
			.antMatchers("/css/**","/js/**","/img/**").permitAll()
			.antMatchers("/guest/**").permitAll()
			.antMatchers("/member/**").hasAnyRole("USER","ADMIN")
			.antMatchers("/admin/**").hasRole("ADMIN")
			.anyRequest().authenticated();
		
		http.formLogin().permitAll();
		http.logout().permitAll();
		
		
			
	}
	
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		// AuthenticationManagerBuilder
		auth.inMemoryAuthentication()
		.withUser("user").password(passwordEncoder().encode("1234")).roles("USER")
		.and()
		.withUser("admin").password(passwordEncoder().encode("1234")).roles("ADMIN");	
	
	}
	
	//반환자료형을 스프링이 관리할거임.
	@Bean
	public BCryptPasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

}

내가만든 로그인 폼

<form action="<c:url value="j_spring_security_check"/>" method="post">
	ID:<input type="text" name="j_username">
	PW:<input type="text" name="j_password">
	<input type="submit" value="LOGIN"><br>
</form>
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
		
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		//http	
		//.permitAll()모두
		//.hasRole이하 이용자에게만
		http.authorizeRequests()
			.antMatchers("/").permitAll()
			.antMatchers("/css/**","/js/**","/img/**").permitAll()
			.antMatchers("/guest/**").permitAll()
			.antMatchers("/member/**").hasAnyRole("USER","ADMIN")
			.antMatchers("/admin/**").hasRole("ADMIN")
			.anyRequest().authenticated();
		
		http.formLogin()
		.loginPage("/loginForm")
		.loginProcessingUrl("/j_spring_security_check")
		.failureUrl("/loginError")
		.usernameParameter("j_username")
		.passwordParameter("j_password")
		.permitAll();
		
		
		http.logout()
		.logoutUrl("/logout")
		.logoutSuccessUrl("/")
		.permitAll();
		
		http.csrf().disable();
			
	}
	
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		// AuthenticationManagerBuilder
		auth.inMemoryAuthentication()
		.withUser("user").password(passwordEncoder().encode("1234")).roles("USER")
		.and()
		.withUser("admin").password(passwordEncoder().encode("1234")).roles("ADMIN");	
	
	}
	
	//반환자료형을 스프링이 관리할거임.
	@Bean
	public BCryptPasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

}
profile
화이탱!

0개의 댓글