강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

ㅎㅎㅎ님의 프로필 이미지
ㅎㅎㅎ

작성한 질문수

스프링 시큐리티

1) 실전 프로젝트 생성

질문

작성

·

307

0

- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 안녕하세요

실전프로젝트 -인증 프로세스 Form 인증 구현 - 1강을 듣고 따라하고있는데

저는 user ID로 로그인했을때 /messages에도 접근가능한데 왜 이럴까요 ㅠㅠ

@Configuration
@EnableWebSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    //사용자 추가

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        String password = passwordEncoder().encode("1111");

        auth.inMemoryAuthentication().withUser("user").password(password).roles("USER");
        auth.inMemoryAuthentication().withUser("manager").password(password).roles("MANAGER");
        auth.inMemoryAuthentication().withUser("admin").password(password).roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").permitAll() //루트 페이지는 전부 접근 가능
                .antMatchers("/config").hasRole("ADMIN")
                .antMatchers("/messages").hasRole("MANAGER")
                .antMatchers("/mypage").hasRole("USER")
                .anyRequest().authenticated()

        .and()

                .formLogin();

    }
}@Configuration
@EnableWebSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    //사용자 추가

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        String password = passwordEncoder().encode("1111");

        auth.inMemoryAuthentication().withUser("user").password(password).roles("USER");
        auth.inMemoryAuthentication().withUser("manager").password(password).roles("MANAGER");
        auth.inMemoryAuthentication().withUser("admin").password(password).roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").permitAll() //루트 페이지는 전부 접근 가능
                .antMatchers("/config").hasRole("ADMIN")
                .antMatchers("/messages").hasRole("MANAGER")
                .antMatchers("/mypage").hasRole("USER")
                .anyRequest().authenticated()

        .and()

                .formLogin();

    }
}

있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.

답변 1

0

정수원님의 프로필 이미지
정수원
지식공유자

코드에는 특별한 문제가 없어 보입니다.

그리고 제가 위의 코드로 직접 테스트해 봐도 /messages 로 user 계정으로 접근하면 접근이 거부되고 있습니다

image즉 Forbidden, status=403 이 나오고 있습니다.

그래서 정상 동작하는 것 같습니다.

호출 url 이 정확한지 확인 해 보시기 바랍니다.

ㅎㅎㅎ님의 프로필 이미지
ㅎㅎㅎ

작성한 질문수

질문하기