인프런 커뮤니티 질문&답변
antMacher url 관련해서 질문드립니다.
작성
·
231
0
안녕하세요. 유익한 강의 항상 감사드립니다. 그런데 실습과정을 진행하던 중 문제가 생겨 질문드립니다ㅠㅠ
http://localhost/postList라는 게시판에 ADMIN Role을 가진 사용자만 접근하게 하고 싶은데 일반 user로 로그인해도 게시판 페이지에 원활하게 진입이 됩니다ㅠㅠ
기존 Vue+Spring으로 진행하던 프로젝트에 Security를 적용해 게시판 접근 권한을 주기 위해 다음과 같이 코드를 작성하였습니다. Vue 프로젝트는 스프링 프로젝트 안에 빌드해서 넣어놓은 상태이며 기존의 회원관리 기능은 없습니다. csrf는 게시판 CRUD 허용을 위해 적어놓았습니다...
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@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");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.csrf().disable().cors().disable()
.authorizeRequests()
.antMatchers("/").permitAll() //얘에다가 admin 걸면 user로 로그인해도 접근 못함 제대로 돌아감
.antMatchers("/postList/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/css/**")
.antMatchers("/js/**")
.antMatchers("/img/**");
}
}
http://localhost/postList라는 게시판에 ADMIN Role을 가진 사용자만 접근하게 하고 싶은데 일반 user로 로그인해도 게시판 페이지에 원활하게 진입이 됩니다ㅠㅠ
주소는 /postList가 확실하며 콘솔에 아무런 에러 메세지도 나타나지 않고 정상 접근이 되는 상황입니다.
혹시 antMatcher 자체가 먹지 않나 해서 .antMatchers("/").hasRole("USER"); 로 코드를 변경해서 테스트 해 보니 이건 또 "/"로 접근하려는 admin에게 제대로 forbidden을 띄워줍니다... 기본 "/" 주소만 antMatcher로 권한 변경이 되고 하위 주소는 권한 변경을 못 하고 있는 것 같은데 혹시 제가 postList의 ant Pattern을 잘못 기입했거나 따로 주어야 할 설정이 있을까요?
하루종일 구글링하면서 오만 설정을 다 넣어봤는데 로그인한 사용자가 무슨 Role이나 Authority를 가지고 있든 postList 문을 그냥 활짝 열어버리길래 질문 드립니다ㅠㅠㅠㅠ 읽어주셔서 감사합니다.





https://github.com/gojung/gojung-web
페이지 url은 front/src/router/index.js에서 잡아주고 있으며 페이지 이동과 주소값은 제대로 되는 것을 확인했습니다. securityConfig는 root/src 안의 config/security 안에 작성했습니다.
루트 페이지에 hasRole()을 걸면 해당 Role을 가지지 않은 유저에 대한 권한을 제대로 막는데 다른 페이지는 하나도 제한이 안 걸리네요... 이런 경우는 어떻게 처리해야 할지 아직 해결하지 못했습니다ㅠㅠ