하나의 프로젝트에서 폼로그인도 되고 Http Login도 되도록 설정을 하려면 다음과 같이 설정을 변경해 주면 된다.
Reference 참조
https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#multiple-httpsecurity
Spring Security Reference
The authenticator is also responsible for retrieving any required user attributes. This is because the permissions on the attributes may depend on the type of authentication being used. For example, if binding as the user, it may be necessary to read them
docs.spring.io
@Configuration
@EnableWebSecurity
public class MultiHttpSecurityConfig {
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Autowired
UserService userService;
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/api/**").authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}
}
@Configuration
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired
UserService userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests() //
.antMatchers("/", "/main").permitAll().antMatchers("/static/**").permitAll() //
.antMatchers("/img/**", "/js/**", "/css/**", "/fonts/**").permitAll() //
.antMatchers("/member/join", "/member/register").permitAll() //
.anyRequest().authenticated() //
.and() //
.formLogin() //
.loginPage("/login") //
.loginProcessingUrl("/login_process") //
.usernameParameter("id") //
.passwordParameter("pw") //
.defaultSuccessUrl("/main", true) //
.failureUrl("/login?error=true").permitAll().and() //
.logout() //
.logoutSuccessUrl("/main") //
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}
}
}
'spring-project' 카테고리의 다른 글
spring boot 에서 소스 수정 후 자동으로 반영이 안될 경우 (0) | 2020.04.28 |
---|---|
RestTemplate으로 basic auth 를 통한 rest api 사용 (0) | 2020.01.19 |
스프링에서 form submit 으로 가변(멀티) 데이터 받기 (0) | 2020.01.10 |
Lombok @Slf4j Annotation (0) | 2020.01.05 |
lombok (롬복) 설치하기. (0) | 2020.01.05 |