[Spring Boot] Security Taglibs, Database

[Spring Boot] Security Taglibs, Database

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

package com.study.springboot.auth; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.authentication.AuthenticationFailureHandler; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired public AuthenticationFailureHandler authenticationFailureHandler; @Override protected void configure(HttpSecurity http) throws Exception { 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" ) // default : /login .loginProcessingUrl( "/j_spring_security_check" ) //.failureUrl("/loginForm?error") // default : /login?error //.defaultSuccessUrl("/") .failureHandler(authenticationFailureHandler) .usernameParameter( "j_username" ) // default : j_username .passwordParameter( "j_password" ) // default : j_password .permitAll(); http.logout() .logoutUrl( "/logout" ) // default .logoutSuccessUrl( "/" ) .permitAll(); // ssl을 사용하지 않으면 true로 사용 http.csrf().disable(); } // @Autowired // public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { // auth.inMemoryAuthentication() // .withUser("user").password(passwordEncoder().encode("1234")).roles("USER") // .and() // .withUser("admin").password(passwordEncoder().encode("1234")).roles("ADMIN"); // // ROLE_ADMIN 에서 ROLE_는 자동으로 붙음 // } @Autowired private DataSource dataSource; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { System . out . println (passwordEncoder().encode( "123" )); auth.jdbcAuthentication() .dataSource(dataSource) .usersByUsernameQuery( "select name as userName, password, enabled" + " from user_list where name = ?" ) .authoritiesByUsernameQuery( "select name as userName, authority " + " from user_list where name= ?" ) .passwordEncoder( new BCryptPasswordEncoder()); } @Bean public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } Colored by Color Scripter

from http://memories95.tistory.com/125 by ccl(A) rewrite - 2020-03-07 06:56:01

댓글

이 블로그의 인기 게시물

데이터 바인딩 추상화 - propertyEditor

[sts] spring boot groovy 적용 실행 하기

2020 LCK 롤챔스 Spring 경기 재개 및 일정